Browse Source

fix(literalMidWordUnderscores): Inconsistent behavior of emphasis and strong with literalMidWordUndescores

Closes #333
Estevao Soares dos Santos 8 years ago
parent
commit
0292ae0dcb

+ 12 - 15
dist/showdown.js

@@ -2092,34 +2092,31 @@ showdown.subParser('italicsAndBold', function (text, options, globals) {
   // it's faster to have 2 separate regexes for each case than have just one
   // because of backtracing, in some cases, it could lead to an exponential effect
   // called "catastrophic backtrace". Ominous!
+
+  // Parse underscores
   if (options.literalMidWordUnderscores) {
-    //underscores
-    // Since we are consuming a \s character, we need to add it
     text = text.replace(/\b__(\S[\s\S]*?)__\b/gm, '<strong>$1</strong>');
     text = text.replace(/\b_(\S[\s\S]*?)_\b/gm, '<em>$1</em>');
-    //asterisks
-    text = text.replace(/\*\*(?=\S)([\s\S]*?\S[*]*)\*\*/g, '<strong>$1</strong>');
-    text = text.replace(/\*(?=\S)([\s\S]*?\S)\*/g, '<em>$1</em>');
-
   } else {
-    // <strong> must go first:
     text = text.replace(/__(\S[\s\S]*?)__/g, function (wm, m) {
       return (/\S$/.test(m)) ? '<strong>' + m + '</strong>' : wm;
     });
-    text = text.replace(/\*\*(\S[\s\S]*?)\*\*/g, function (wm, m) {
-      return (/\S$/.test(m)) ? '<strong>' + m + '</strong>' : wm;
-    });
-    // now <em>
     text = text.replace(/_(\S[\s\S]*?)_/g, function (wm, m) {
       // !/^_[^_]/.test(m) - test if it doesn't start with __ (since it seems redundant, we removed it)
       return (/\S$/.test(m)) ? '<em>' + m + '</em>' : wm;
     });
-    text = text.replace(/\*(\S[\s\S]*?)\*/g, function (wm, m) {
-      // !/^\*[^*]/.test(m) - test if it doesn't start with ** (since it seems redundant, we removed it)
-      return (/\S$/.test(m)) ? '<em>' + m + '</em>' : wm;
-    });
   }
 
+  // Now parse asterisks
+  text = text.replace(/\*\*(\S[\s\S]*?)\*\*/g, function (wm, m) {
+    return (/\S$/.test(m)) ? '<strong>' + m + '</strong>' : wm;
+  });
+
+  text = text.replace(/\*(\S[\s\S]*?)\*/g, function (wm, m) {
+    // !/^\*[^*]/.test(m) - test if it doesn't start with ** (since it seems redundant, we removed it)
+    return (/\S$/.test(m)) ? '<em>' + m + '</em>' : wm;
+  });
+
   text = globals.converter._dispatch('italicsAndBold.after', text, options, globals);
   return text;
 });

File diff suppressed because it is too large
+ 0 - 0
dist/showdown.js.map


File diff suppressed because it is too large
+ 0 - 0
dist/showdown.min.js


File diff suppressed because it is too large
+ 0 - 0
dist/showdown.min.js.map


+ 12 - 15
src/subParsers/italicsAndBold.js

@@ -6,34 +6,31 @@ showdown.subParser('italicsAndBold', function (text, options, globals) {
   // it's faster to have 2 separate regexes for each case than have just one
   // because of backtracing, in some cases, it could lead to an exponential effect
   // called "catastrophic backtrace". Ominous!
+
+  // Parse underscores
   if (options.literalMidWordUnderscores) {
-    //underscores
-    // Since we are consuming a \s character, we need to add it
     text = text.replace(/\b__(\S[\s\S]*?)__\b/gm, '<strong>$1</strong>');
     text = text.replace(/\b_(\S[\s\S]*?)_\b/gm, '<em>$1</em>');
-    //asterisks
-    text = text.replace(/\*\*(?=\S)([\s\S]*?\S[*]*)\*\*/g, '<strong>$1</strong>');
-    text = text.replace(/\*(?=\S)([\s\S]*?\S)\*/g, '<em>$1</em>');
-
   } else {
-    // <strong> must go first:
     text = text.replace(/__(\S[\s\S]*?)__/g, function (wm, m) {
       return (/\S$/.test(m)) ? '<strong>' + m + '</strong>' : wm;
     });
-    text = text.replace(/\*\*(\S[\s\S]*?)\*\*/g, function (wm, m) {
-      return (/\S$/.test(m)) ? '<strong>' + m + '</strong>' : wm;
-    });
-    // now <em>
     text = text.replace(/_(\S[\s\S]*?)_/g, function (wm, m) {
       // !/^_[^_]/.test(m) - test if it doesn't start with __ (since it seems redundant, we removed it)
       return (/\S$/.test(m)) ? '<em>' + m + '</em>' : wm;
     });
-    text = text.replace(/\*(\S[\s\S]*?)\*/g, function (wm, m) {
-      // !/^\*[^*]/.test(m) - test if it doesn't start with ** (since it seems redundant, we removed it)
-      return (/\S$/.test(m)) ? '<em>' + m + '</em>' : wm;
-    });
   }
 
+  // Now parse asterisks
+  text = text.replace(/\*\*(\S[\s\S]*?)\*\*/g, function (wm, m) {
+    return (/\S$/.test(m)) ? '<strong>' + m + '</strong>' : wm;
+  });
+
+  text = text.replace(/\*(\S[\s\S]*?)\*/g, function (wm, m) {
+    // !/^\*[^*]/.test(m) - test if it doesn't start with ** (since it seems redundant, we removed it)
+    return (/\S$/.test(m)) ? '<em>' + m + '</em>' : wm;
+  });
+
   text = globals.converter._dispatch('italicsAndBold.after', text, options, globals);
   return text;
 });

+ 4 - 3
test/features/#198.literalMidWordUnderscores-changes-behavior-of-asterisk.html

@@ -1,3 +1,4 @@
-<p>​pointer *ptr *thing</p>
-<p>something _else _bla</p>
-<p>something __else __bla</p>
+<p>foo *bar *baz</p>
+<p>foo **bar **baz</p>
+<p>foo _bar _baz</p>
+<p>foo __bar __baz</p>

+ 5 - 3
test/features/#198.literalMidWordUnderscores-changes-behavior-of-asterisk.md

@@ -1,5 +1,7 @@
-​pointer *ptr *thing
+foo *bar *baz
 
-something _else _bla
+foo **bar **baz
 
-something __else __bla
+foo _bar _baz
+
+foo __bar __baz

Some files were not shown because too many files changed in this diff