Преглед на файлове

feat(literalMidWordUnderscores): add support for GFM literal midword underscores

Github Flavored Markdown does not parse underscores in the middle of a word as emphasis/bold.
This commit adds this feature to showdown through an option called "literalMidWordUnderscores".

Related to #164
Estevão Soares dos Santos преди 10 години
родител
ревизия
0c0cd7db99

+ 22 - 15
dist/showdown.js

@@ -9,12 +9,13 @@ var showdown = {},
     parsers = {},
     extensions = {},
     defaultOptions = {
-      omitExtraWLInCodeBlocks: false,
-      prefixHeaderId:          false,
-      noHeaderId:              false,
-      headerLevelStart:        1,
-      parseImgDimensions:      false,
-      simplifiedAutoLink:      false
+      omitExtraWLInCodeBlocks:   false,
+      prefixHeaderId:            false,
+      noHeaderId:                false,
+      headerLevelStart:          1,
+      parseImgDimensions:        false,
+      simplifiedAutoLink:        false,
+      literalMidWordUnderscores: false
     },
     globalOptions = JSON.parse(JSON.stringify(defaultOptions)); //clone default options out of laziness =P
 
@@ -432,11 +433,7 @@ showdown.Converter = function (converterOptions) {
        * @private
        * @type {{}}
        */
-      options = {
-        omitExtraWLInCodeBlocks: false,
-        prefixHeaderId:          false,
-        noHeaderId:              false
-      },
+      options = {},
 
       /**
        * Language extensions used by this converter
@@ -1596,13 +1593,23 @@ showdown.subParser('images', function (text, options, globals) {
   return text;
 });
 
-showdown.subParser('italicsAndBold', function (text) {
+showdown.subParser('italicsAndBold', function (text, options) {
   'use strict';
-  // <strong> must go first:
-  text = text.replace(/(\*\*|__)(?=\S)([^\r]*?\S[*_]*)\1/g, '<strong>$2</strong>');
 
-  text = text.replace(/(\*|_)(?=\S)([^\r]*?\S)\1/g, '<em>$2</em>');
+  if (options.literalMidWordUnderscores) {
+    //underscores
+    // Since we are consuming a \s character, we need to add it
+    text = text.replace(/(\s)__(?=\S)([^]+?)__(?=\s)/g, '$1<strong>$2</strong>');
+    text = text.replace(/(\s)_(?=\S)([^]+?)_(?=\s)/g, '$1<em>$2</em>');
+    //asterisks
+    text = text.replace(/\*\*(?=\S)([^]+?)\*\*/g, '<strong>$1</strong>');
+    text = text.replace(/\*(?=\S)([^]+?)\*/g, '<em>$1</em>');
 
+  } else {
+    // <strong> must go first:
+    text = text.replace(/(\*\*|__)(?=\S)([^\r]*?\S[*_]*)\1/g, '<strong>$2</strong>');
+    text = text.replace(/(\*|_)(?=\S)([^\r]*?\S)\1/g, '<em>$2</em>');
+  }
   return text;
 });
 

Файловите разлики са ограничени, защото са твърде много
+ 0 - 0
dist/showdown.js.map


Файловите разлики са ограничени, защото са твърде много
+ 0 - 0
dist/showdown.min.js


Файловите разлики са ограничени, защото са твърде много
+ 0 - 0
dist/showdown.min.js.map


+ 1 - 5
src/converter.js

@@ -22,11 +22,7 @@ showdown.Converter = function (converterOptions) {
        * @private
        * @type {{}}
        */
-      options = {
-        omitExtraWLInCodeBlocks: false,
-        prefixHeaderId:          false,
-        noHeaderId:              false
-      },
+      options = {},
 
       /**
        * Language extensions used by this converter

+ 7 - 6
src/showdown.js

@@ -7,12 +7,13 @@ var showdown = {},
     parsers = {},
     extensions = {},
     defaultOptions = {
-      omitExtraWLInCodeBlocks: false,
-      prefixHeaderId:          false,
-      noHeaderId:              false,
-      headerLevelStart:        1,
-      parseImgDimensions:      false,
-      simplifiedAutoLink:      false
+      omitExtraWLInCodeBlocks:   false,
+      prefixHeaderId:            false,
+      noHeaderId:                false,
+      headerLevelStart:          1,
+      parseImgDimensions:        false,
+      simplifiedAutoLink:        false,
+      literalMidWordUnderscores: false
     },
     globalOptions = JSON.parse(JSON.stringify(defaultOptions)); //clone default options out of laziness =P
 

+ 14 - 4
src/subParsers/italicsAndBold.js

@@ -1,9 +1,19 @@
-showdown.subParser('italicsAndBold', function (text) {
+showdown.subParser('italicsAndBold', function (text, options) {
   'use strict';
-  // <strong> must go first:
-  text = text.replace(/(\*\*|__)(?=\S)([^\r]*?\S[*_]*)\1/g, '<strong>$2</strong>');
 
-  text = text.replace(/(\*|_)(?=\S)([^\r]*?\S)\1/g, '<em>$2</em>');
+  if (options.literalMidWordUnderscores) {
+    //underscores
+    // Since we are consuming a \s character, we need to add it
+    text = text.replace(/(\s)__(?=\S)([^]+?)__(?=\s)/g, '$1<strong>$2</strong>');
+    text = text.replace(/(\s)_(?=\S)([^]+?)_(?=\s)/g, '$1<em>$2</em>');
+    //asterisks
+    text = text.replace(/\*\*(?=\S)([^]+?)\*\*/g, '<strong>$1</strong>');
+    text = text.replace(/\*(?=\S)([^]+?)\*/g, '<em>$1</em>');
 
+  } else {
+    // <strong> must go first:
+    text = text.replace(/(\*\*|__)(?=\S)([^\r]*?\S[*_]*)\1/g, '<strong>$2</strong>');
+    text = text.replace(/(\*|_)(?=\S)([^\r]*?\S)\1/g, '<em>$2</em>');
+  }
   return text;
 });

+ 11 - 0
test/features/#164.2.disallow_underscore_emphasis_mid_word.html

@@ -0,0 +1,11 @@
+<p>this is a sentence_with_mid underscores</p>
+
+<p>this is a sentence with just_one underscore</p>
+
+<p>this <em>should be parsed</em> as emphasis</p>
+
+<p>this is double__underscore__mid word</p>
+
+<p>this has just__one double underscore</p>
+
+<p>this <strong>should be parsed</strong> as bold</p>

+ 11 - 0
test/features/#164.2.disallow_underscore_emphasis_mid_word.md

@@ -0,0 +1,11 @@
+this is a sentence_with_mid underscores
+
+this is a sentence with just_one underscore
+
+this _should be parsed_ as emphasis
+
+this is double__underscore__mid word
+
+this has just__one double underscore
+
+this __should be parsed__ as bold

+ 7 - 6
test/node/showdown.js

@@ -18,12 +18,13 @@ describe('showdown.options', function () {
   describe('getDefaultOptions()', function () {
     it('should get default options', function () {
       var opts = {
-        omitExtraWLInCodeBlocks: false,
-        prefixHeaderId:          false,
-        noHeaderId:              false,
-        headerLevelStart:        1,
-        parseImgDimensions:      false,
-        simplifiedAutoLink:      false
+        omitExtraWLInCodeBlocks:   false,
+        prefixHeaderId:            false,
+        noHeaderId:                false,
+        headerLevelStart:          1,
+        parseImgDimensions:        false,
+        simplifiedAutoLink:        false,
+        literalMidWordUnderscores: false
       };
       expect(showdown.getDefaultOptions()).to.be.eql(opts);
     });

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

@@ -16,6 +16,8 @@ describe('makeHtml() features testsuite', function () {
       converter = new showdown.Converter({headerLevelStart: 3});
     } else if (testsuite[i].name === '#164.1.simple_autolink') {
       converter = new showdown.Converter({simplifiedAutoLink: true});
+    } else if (testsuite[i].name === '#164.2.disallow_underscore_emphasis_mid_word') {
+      converter = new showdown.Converter({literalMidWordUnderscores: true});
     } else {
       converter = new showdown.Converter();
     }

Някои файлове не бяха показани, защото твърде много файлове са промени