浏览代码

feat(requireSpaceBeforeHeadingText): option to make space between `#` and header text mandatory

Credit: @nikz [Nik Wakelin](https://github.com/nikz)

Closes #277
Estevao Soares dos Santos 8 年之前
父节点
当前提交
5d19877590

+ 2 - 0
README.md

@@ -288,6 +288,8 @@ var defaultOptions = showdown.getDefaultOptions();
    wrapped in two</p>
    wrapped in two</p>
    ```
    ```
 
 
+ * **requireSpaceBeforeHeadingText**: (boolean) [default false] Makes adding a space between `#` and the header text mandatory (since v1.5.3)
+ 
 ## Flavors
 ## Flavors
 
 
 You can also use flavors or presets to set the correct options automatically, so that showdown behaves like popular markdown flavors.
 You can also use flavors or presets to set the correct options automatically, so that showdown behaves like popular markdown flavors.

+ 10 - 2
dist/showdown.js

@@ -92,6 +92,11 @@ function getDefaultOpts(simple) {
       defaultValue: false,
       defaultValue: false,
       description: 'Parses simple line breaks as <br> (GFM Style)',
       description: 'Parses simple line breaks as <br> (GFM Style)',
       type: 'boolean'
       type: 'boolean'
+    },
+    requireSpaceBeforeHeadingText: {
+      defaultValue: false,
+      description: 'Makes adding a space between `#` and the header text mandatory (GFM Style)',
+      type: 'boolean'
     }
     }
   };
   };
   if (simple === false) {
   if (simple === false) {
@@ -128,7 +133,8 @@ var showdown = {},
         ghCodeBlocks:                         true,
         ghCodeBlocks:                         true,
         tasklists:                            true,
         tasklists:                            true,
         disableForced4SpacesIndentedSublists: true,
         disableForced4SpacesIndentedSublists: true,
-        simpleLineBreaks:                     true
+        simpleLineBreaks:                     true,
+        requireSpaceBeforeHeadingText:        true
       },
       },
       vanilla: getDefaultOpts(true)
       vanilla: getDefaultOpts(true)
     };
     };
@@ -1802,7 +1808,9 @@ showdown.subParser('headers', function (text, options, globals) {
   //  ...
   //  ...
   //  ###### Header 6
   //  ###### Header 6
   //
   //
-  text = text.replace(/^(#{1,6})[ \t]*(.+?)[ \t]*#*\n+/gm, function (wholeMatch, m1, m2) {
+  var atxStyle = (options.requireSpaceBeforeHeadingText) ? /^(#{1,6})[ \t]+(.+?)[ \t]*#*\n+/gm : /^(#{1,6})[ \t]*(.+?)[ \t]*#*\n+/gm;
+
+  text = text.replace(atxStyle, function (wholeMatch, m1, m2) {
     var span = showdown.subParser('spanGamut')(m2, options, globals),
     var span = showdown.subParser('spanGamut')(m2, options, globals),
         hID = (options.noHeaderId) ? '' : ' id="' + headerId(m2) + '"',
         hID = (options.noHeaderId) ? '' : ' id="' + headerId(m2) + '"',
         hLevel = headerLevelStart - 1 + m1.length,
         hLevel = headerLevelStart - 1 + m1.length,

文件差异内容过多而无法显示
+ 0 - 0
dist/showdown.js.map


文件差异内容过多而无法显示
+ 0 - 0
dist/showdown.min.js


文件差异内容过多而无法显示
+ 0 - 0
dist/showdown.min.js.map


+ 5 - 0
src/options.js

@@ -90,6 +90,11 @@ function getDefaultOpts(simple) {
       defaultValue: false,
       defaultValue: false,
       description: 'Parses simple line breaks as <br> (GFM Style)',
       description: 'Parses simple line breaks as <br> (GFM Style)',
       type: 'boolean'
       type: 'boolean'
+    },
+    requireSpaceBeforeHeadingText: {
+      defaultValue: false,
+      description: 'Makes adding a space between `#` and the header text mandatory (GFM Style)',
+      type: 'boolean'
     }
     }
   };
   };
   if (simple === false) {
   if (simple === false) {

+ 2 - 1
src/showdown.js

@@ -20,7 +20,8 @@ var showdown = {},
         ghCodeBlocks:                         true,
         ghCodeBlocks:                         true,
         tasklists:                            true,
         tasklists:                            true,
         disableForced4SpacesIndentedSublists: true,
         disableForced4SpacesIndentedSublists: true,
-        simpleLineBreaks:                     true
+        simpleLineBreaks:                     true,
+        requireSpaceBeforeHeadingText:        true
       },
       },
       vanilla: getDefaultOpts(true)
       vanilla: getDefaultOpts(true)
     };
     };

+ 3 - 1
src/subParsers/headers.js

@@ -40,7 +40,9 @@ showdown.subParser('headers', function (text, options, globals) {
   //  ...
   //  ...
   //  ###### Header 6
   //  ###### Header 6
   //
   //
-  text = text.replace(/^(#{1,6})[ \t]*(.+?)[ \t]*#*\n+/gm, function (wholeMatch, m1, m2) {
+  var atxStyle = (options.requireSpaceBeforeHeadingText) ? /^(#{1,6})[ \t]+(.+?)[ \t]*#*\n+/gm : /^(#{1,6})[ \t]*(.+?)[ \t]*#*\n+/gm;
+
+  text = text.replace(atxStyle, function (wholeMatch, m1, m2) {
     var span = showdown.subParser('spanGamut')(m2, options, globals),
     var span = showdown.subParser('spanGamut')(m2, options, globals),
         hID = (options.noHeaderId) ? '' : ' id="' + headerId(m2) + '"',
         hID = (options.noHeaderId) ? '' : ' id="' + headerId(m2) + '"',
         hLevel = headerLevelStart - 1 + m1.length,
         hLevel = headerLevelStart - 1 + m1.length,

+ 2 - 0
test/features/requireSpaceBeforeHeadingText.html

@@ -0,0 +1,2 @@
+<h1 id="header">header</h1>
+<p>#header</p>

+ 3 - 0
test/features/requireSpaceBeforeHeadingText.md

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

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

@@ -39,6 +39,8 @@ describe('makeHtml() features testsuite', function () {
       converter = new showdown.Converter({simpleLineBreaks: true});
       converter = new showdown.Converter({simpleLineBreaks: true});
     } else if (testsuite[i].name === 'excludeTrailingPunctuationFromURLs-option') {
     } else if (testsuite[i].name === 'excludeTrailingPunctuationFromURLs-option') {
       converter = new showdown.Converter({simplifiedAutoLink: true, excludeTrailingPunctuationFromURLs: true});
       converter = new showdown.Converter({simplifiedAutoLink: true, excludeTrailingPunctuationFromURLs: true});
+    } else if (testsuite[i].name === 'requireSpaceBeforeHeadingText') {
+      converter = new showdown.Converter({requireSpaceBeforeHeadingText: true});
     } else {
     } else {
       converter = new showdown.Converter();
       converter = new showdown.Converter();
     }
     }

部分文件因为文件数量过多而无法显示