Browse Source

fix(prefixHeaderId): make `prefixHeaderId` string be parsed along the generated id

If an invalid prefix was passed (a string with spaces, for instance),
the string would be added as is. This ould generate invalid ids.
Also, this makes `prefixHeaderId` option play nicely with `ghCompatibleHeaderId`, since they will
follow the same escaping rules when both options are enabled.
Estevao Soares dos Santos 8 năm trước cách đây
mục cha
commit
f641a7de90

+ 17 - 18
dist/showdown.js

@@ -1954,8 +1954,7 @@ showdown.subParser('headers', function (text, options, globals) {
 
   text = globals.converter._dispatch('headers.before', text, options, globals);
 
-  var prefixHeader = options.prefixHeaderId,
-      headerLevelStart = (isNaN(parseInt(options.headerLevelStart))) ? 1 : parseInt(options.headerLevelStart),
+  var headerLevelStart = (isNaN(parseInt(options.headerLevelStart))) ? 1 : parseInt(options.headerLevelStart),
       ghHeaderId = options.ghCompatibleHeaderId,
 
   // Set text-style headers:
@@ -2004,10 +2003,18 @@ showdown.subParser('headers', function (text, options, globals) {
   });
 
   function headerId (m) {
-    var title, escapedId;
+    var title;
+    // Prefix id to prevent causing inadvertent pre-existing style matches.
+    if (showdown.helper.isString(options.prefixHeaderId)) {
+      title = options.prefixHeaderId + m;
+    } else if (options.prefixHeaderId === true) {
+      title = 'section ' + m;
+    } else {
+      title = m;
+    }
 
     if (ghHeaderId) {
-      escapedId = m
+      title = title
         .replace(/ /g, '-')
         // replace previously escaped chars (&, ¨ and $)
         .replace(/&/g, '')
@@ -2018,23 +2025,15 @@ showdown.subParser('headers', function (text, options, globals) {
         .replace(/[&+$,\/:;=?@"#{}|^¨~\[\]`\\*)(%.!'<>]/g, '')
         .toLowerCase();
     } else {
-      escapedId = m.replace(/[^\w]/g, '').toLowerCase();
+      title = title
+        .replace(/[^\w]/g, '')
+        .toLowerCase();
     }
 
-    if (globals.hashLinkCounts[escapedId]) {
-      title = escapedId + '-' + (globals.hashLinkCounts[escapedId]++);
+    if (globals.hashLinkCounts[title]) {
+      title = title + '-' + (globals.hashLinkCounts[title]++);
     } else {
-      title = escapedId;
-      globals.hashLinkCounts[escapedId] = 1;
-    }
-
-    // Prefix id to prevent causing inadvertent pre-existing style matches.
-    if (prefixHeader === true) {
-      prefixHeader = 'section';
-    }
-
-    if (showdown.helper.isString(prefixHeader)) {
-      return prefixHeader + title;
+      globals.hashLinkCounts[title] = 1;
     }
     return title;
   }

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 0 - 0
dist/showdown.js.map


Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 0 - 0
dist/showdown.min.js


Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 0 - 0
dist/showdown.min.js.map


+ 17 - 18
src/subParsers/headers.js

@@ -3,8 +3,7 @@ showdown.subParser('headers', function (text, options, globals) {
 
   text = globals.converter._dispatch('headers.before', text, options, globals);
 
-  var prefixHeader = options.prefixHeaderId,
-      headerLevelStart = (isNaN(parseInt(options.headerLevelStart))) ? 1 : parseInt(options.headerLevelStart),
+  var headerLevelStart = (isNaN(parseInt(options.headerLevelStart))) ? 1 : parseInt(options.headerLevelStart),
       ghHeaderId = options.ghCompatibleHeaderId,
 
   // Set text-style headers:
@@ -53,10 +52,18 @@ showdown.subParser('headers', function (text, options, globals) {
   });
 
   function headerId (m) {
-    var title, escapedId;
+    var title;
+    // Prefix id to prevent causing inadvertent pre-existing style matches.
+    if (showdown.helper.isString(options.prefixHeaderId)) {
+      title = options.prefixHeaderId + m;
+    } else if (options.prefixHeaderId === true) {
+      title = 'section ' + m;
+    } else {
+      title = m;
+    }
 
     if (ghHeaderId) {
-      escapedId = m
+      title = title
         .replace(/ /g, '-')
         // replace previously escaped chars (&, ¨ and $)
         .replace(/&amp;/g, '')
@@ -67,23 +74,15 @@ showdown.subParser('headers', function (text, options, globals) {
         .replace(/[&+$,\/:;=?@"#{}|^¨~\[\]`\\*)(%.!'<>]/g, '')
         .toLowerCase();
     } else {
-      escapedId = m.replace(/[^\w]/g, '').toLowerCase();
+      title = title
+        .replace(/[^\w]/g, '')
+        .toLowerCase();
     }
 
-    if (globals.hashLinkCounts[escapedId]) {
-      title = escapedId + '-' + (globals.hashLinkCounts[escapedId]++);
+    if (globals.hashLinkCounts[title]) {
+      title = title + '-' + (globals.hashLinkCounts[title]++);
     } else {
-      title = escapedId;
-      globals.hashLinkCounts[escapedId] = 1;
-    }
-
-    // Prefix id to prevent causing inadvertent pre-existing style matches.
-    if (prefixHeader === true) {
-      prefixHeader = 'section';
-    }
-
-    if (showdown.helper.isString(prefixHeader)) {
-      return prefixHeader + title;
+      globals.hashLinkCounts[title] = 1;
     }
     return title;
   }

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

@@ -0,0 +1 @@
+<h1 id="sectionfooheader">foo header</h1>

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

@@ -0,0 +1 @@
+# foo header

+ 1 - 0
test/features/prefixHeaderId-string-and-ghCompatibleHeaderId.html

@@ -0,0 +1 @@
+<h1 id="my-prefix-foo-header">foo header</h1>

+ 1 - 0
test/features/prefixHeaderId-string-and-ghCompatibleHeaderId.md

@@ -0,0 +1 @@
+# foo header

+ 1 - 0
test/features/prefixHeaderId-string-and-ghCompatibleHeaderId2.html

@@ -0,0 +1 @@
+<h1 id="my-prefix-foo-header">foo header</h1>

+ 1 - 0
test/features/prefixHeaderId-string-and-ghCompatibleHeaderId2.md

@@ -0,0 +1 @@
+# foo header

+ 1 - 0
test/features/prefixHeaderId-string.html

@@ -0,0 +1 @@
+<h1 id="myprefixfooheader">foo header</h1>

+ 1 - 0
test/features/prefixHeaderId-string.md

@@ -0,0 +1 @@
+# foo header

+ 8 - 0
test/node/testsuite.features.js

@@ -61,6 +61,14 @@ describe('makeHtml() features testsuite', function () {
       converter = new showdown.Converter({encodeEmails: false, simplifiedAutoLink: true});
     } else if (testsuite[i].name === '#331.allow-escaping-of-tilde') {
       converter = new showdown.Converter({strikethrough: true});
+    } else if (testsuite[i].name === 'prefixHeaderId-simple') {
+      converter = new showdown.Converter({prefixHeaderId: true});
+    } else if (testsuite[i].name === 'prefixHeaderId-string') {
+      converter = new showdown.Converter({prefixHeaderId: 'my-prefix-'});
+    } else if (testsuite[i].name === 'prefixHeaderId-string-and-ghCompatibleHeaderId') {
+      converter = new showdown.Converter({prefixHeaderId: 'my-prefix-', ghCompatibleHeaderId: true});
+    } else if (testsuite[i].name === 'prefixHeaderId-string-and-ghCompatibleHeaderId2') {
+      converter = new showdown.Converter({prefixHeaderId: 'my prefix ', ghCompatibleHeaderId: true});
     } else {
       converter = new showdown.Converter();
     }

Một số tệp đã không được hiển thị bởi vì quá nhiều tập tin thay đổi trong này khác