Przeglądaj źródła

feat(rawHeaderId): Remove only spaces, ' and " from generated header ids

This option removes only spaces, ' and " from generated Header IDs,
replacing them with dashes. This might generate malformed IDs.

Closes #409
Estevao Soares dos Santos 8 lat temu
rodzic
commit
1791cf0ebf

+ 17 - 3
dist/showdown.js

@@ -1,4 +1,4 @@
-;/*! showdown 05-08-2017 */
+;/*! showdown 06-08-2017 */
 (function(){
 /**
  * Created by Tivie on 13-07-2015.
@@ -28,6 +28,11 @@ function getDefaultOpts (simple) {
       describe: 'Generate header ids compatible with github style (spaces are replaced with dashes, a bunch of non alphanumeric chars are removed)',
       type: 'boolean'
     },
+    rawHeaderId: {
+      defaultValue: false,
+      describe: 'Remove only spaces, \' and " from generated header ids (including prefixes), replacing them with dashes (-). WARNING: This might result in malformed ids',
+      type: 'boolean'
+    },
     headerLevelStart: {
       defaultValue: false,
       describe: 'The header blocks level start',
@@ -2088,7 +2093,6 @@ showdown.subParser('headers', function (text, options, globals) {
   text = globals.converter._dispatch('headers.before', text, options, globals);
 
   var headerLevelStart = (isNaN(parseInt(options.headerLevelStart))) ? 1 : parseInt(options.headerLevelStart),
-      ghHeaderId = options.ghCompatibleHeaderId,
 
   // Set text-style headers:
   //	Header 1
@@ -2160,7 +2164,7 @@ showdown.subParser('headers', function (text, options, globals) {
       title = m;
     }
 
-    if (ghHeaderId) {
+    if (options.ghCompatibleHeaderId) {
       title = title
         .replace(/ /g, '-')
         // replace previously escaped chars (&, ¨ and $)
@@ -2171,6 +2175,16 @@ showdown.subParser('headers', function (text, options, globals) {
         // borrowed from github's redcarpet (some they should produce similar results)
         .replace(/[&+$,\/:;=?@"#{}|^¨~\[\]`\\*)(%.!'<>]/g, '')
         .toLowerCase();
+    } else if (options.rawHeaderId) {
+      title = title
+        .replace(/ /g, '-')
+        // replace previously escaped chars (&, ¨ and $)
+        .replace(/&amp;/g, '&')
+        .replace(/¨T/g, '¨')
+        .replace(/¨D/g, '$')
+        // replace " and '
+        .replace(/["']/g, '-')
+        .toLowerCase();
     } else {
       title = title
         .replace(/[^\w]/g, '')

Plik diff jest za duży
+ 0 - 0
dist/showdown.js.map


Plik diff jest za duży
+ 1 - 1
dist/showdown.min.js


Plik diff jest za duży
+ 0 - 0
dist/showdown.min.js.map


+ 5 - 0
src/options.js

@@ -26,6 +26,11 @@ function getDefaultOpts (simple) {
       describe: 'Generate header ids compatible with github style (spaces are replaced with dashes, a bunch of non alphanumeric chars are removed)',
       type: 'boolean'
     },
+    rawHeaderId: {
+      defaultValue: false,
+      describe: 'Remove only spaces, \' and " from generated header ids (including prefixes), replacing them with dashes (-). WARNING: This might result in malformed ids',
+      type: 'boolean'
+    },
     headerLevelStart: {
       defaultValue: false,
       describe: 'The header blocks level start',

+ 11 - 2
src/subParsers/headers.js

@@ -4,7 +4,6 @@ showdown.subParser('headers', function (text, options, globals) {
   text = globals.converter._dispatch('headers.before', text, options, globals);
 
   var headerLevelStart = (isNaN(parseInt(options.headerLevelStart))) ? 1 : parseInt(options.headerLevelStart),
-      ghHeaderId = options.ghCompatibleHeaderId,
 
   // Set text-style headers:
   //	Header 1
@@ -76,7 +75,7 @@ showdown.subParser('headers', function (text, options, globals) {
       title = m;
     }
 
-    if (ghHeaderId) {
+    if (options.ghCompatibleHeaderId) {
       title = title
         .replace(/ /g, '-')
         // replace previously escaped chars (&, ¨ and $)
@@ -87,6 +86,16 @@ showdown.subParser('headers', function (text, options, globals) {
         // borrowed from github's redcarpet (some they should produce similar results)
         .replace(/[&+$,\/:;=?@"#{}|^¨~\[\]`\\*)(%.!'<>]/g, '')
         .toLowerCase();
+    } else if (options.rawHeaderId) {
+      title = title
+        .replace(/ /g, '-')
+        // replace previously escaped chars (&, ¨ and $)
+        .replace(/&amp;/g, '&')
+        .replace(/¨T/g, '¨')
+        .replace(/¨D/g, '$')
+        // replace " and '
+        .replace(/["']/g, '-')
+        .toLowerCase();
     } else {
       title = title
         .replace(/[^\w]/g, '')

+ 1 - 0
test/features/rawHeaderId/simple.html

@@ -0,0 +1 @@
+<h1 id="123-my#very/-strange-\header*`^ªº-_.,-yeah">123 My#very/ strange \header*`^ªº-_., yeah</h1>

+ 1 - 0
test/features/rawHeaderId/simple.md

@@ -0,0 +1 @@
+# 123 My#very/ strange \header*`^ªº-_., yeah

+ 2 - 0
test/features/rawHeaderId/with-prefixHeaderId.html

@@ -0,0 +1,2 @@
+<h1 id="/prefix/some-header">some header</h1>
+<h1 id="/prefix/another-!-#$%&/()=?»@£§{[]}«--header">another !"#$%&amp;/()=?»@£§{[]}«' header</h1>

+ 3 - 0
test/features/rawHeaderId/with-prefixHeaderId.md

@@ -0,0 +1,3 @@
+# some header
+
+# another !"#$%&/()=?»@£§{[]}«' header

+ 16 - 1
test/node/testsuite.features.js

@@ -8,7 +8,8 @@ var bootstrap = require('../bootstrap.js'),
     tableSuite = bootstrap.getTestSuite('test/features/tables/'),
     simplifiedAutoLinkSuite = bootstrap.getTestSuite('test/features/simplifiedAutoLink/'),
     openLinksInNewWindowSuite = bootstrap.getTestSuite('test/features/openLinksInNewWindow/'),
-    disableForced4SpacesIndentedSublistsSuite = bootstrap.getTestSuite('test/features/disableForced4SpacesIndentedSublists/');
+    disableForced4SpacesIndentedSublistsSuite = bootstrap.getTestSuite('test/features/disableForced4SpacesIndentedSublists/'),
+    html5CompatibleHeaderIdSuite = bootstrap.getTestSuite('test/features/rawHeaderId/');
 
 describe('makeHtml() features testsuite', function () {
   'use strict';
@@ -152,4 +153,18 @@ describe('makeHtml() features testsuite', function () {
       it(suite[i].name.replace(/-/g, ' '), assertion(suite[i], converter));
     }
   });
+
+  // test rawHeaderId support
+  describe('rawHeaderId support', function () {
+    var converter,
+        suite = html5CompatibleHeaderIdSuite;
+    for (var i = 0; i < suite.length; ++i) {
+      if (suite[i].name === 'with-prefixHeaderId') {
+        converter = new showdown.Converter({rawHeaderId: true, prefixHeaderId: '/prefix/'});
+      } else {
+        converter = new showdown.Converter({rawHeaderId: true});
+      }
+      it(suite[i].name.replace(/-/g, ' '), assertion(suite[i], converter));
+    }
+  });
 });

Niektóre pliki nie zostały wyświetlone z powodu dużej ilości zmienionych plików