Просмотр исходного кода

fix(italicsAndBold): fix double emphasis edge case

Estevao Soares dos Santos 8 лет назад
Родитель
Сommit
1832b7f721

+ 13 - 7
dist/showdown.js

@@ -1,4 +1,4 @@
-;/*! showdown 30-01-2017 */
+;/*! showdown 31-01-2017 */
 (function(){
 /**
  * Created by Tivie on 13-07-2015.
@@ -2089,30 +2089,36 @@ showdown.subParser('italicsAndBold', function (text, options, globals) {
 
   text = globals.converter._dispatch('italicsAndBold.before', text, options, globals);
 
-  // it's faster to have 2 separate regexes for each case than have just one
+  // it's faster to have 3 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) {
-    text = text.replace(/\b__(\S[\s\S]*?)__\b/gm, '<strong>$1</strong>');
-    text = text.replace(/\b_(\S[\s\S]*?)_\b/gm, '<em>$1</em>');
+    text = text.replace(/\b___(\S[\s\S]*)___\b/g, '<strong><em>$1</em></strong>');
+    text = text.replace(/\b__(\S[\s\S]*)__\b/g, '<strong>$1</strong>');
+    text = text.replace(/\b_(\S[\s\S]*?)_\b/g, '<em>$1</em>');
   } else {
+    text = text.replace(/___(\S[\s\S]*?)___/g, function (wm, m) {
+      return (/\S$/.test(m)) ? '<strong><em>' + m + '</em></strong>' : wm;
+    });
     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) {
+    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><em>' + m + '</em></strong>' : wm;
+  });
   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) {
+  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;
   });

Разница между файлами не показана из-за своего большого размера
+ 0 - 0
dist/showdown.js.map


Разница между файлами не показана из-за своего большого размера
+ 1 - 1
dist/showdown.min.js


Разница между файлами не показана из-за своего большого размера
+ 0 - 0
dist/showdown.min.js.map


+ 12 - 6
src/subParsers/italicsAndBold.js

@@ -3,30 +3,36 @@ showdown.subParser('italicsAndBold', function (text, options, globals) {
 
   text = globals.converter._dispatch('italicsAndBold.before', text, options, globals);
 
-  // it's faster to have 2 separate regexes for each case than have just one
+  // it's faster to have 3 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) {
-    text = text.replace(/\b__(\S[\s\S]*?)__\b/gm, '<strong>$1</strong>');
-    text = text.replace(/\b_(\S[\s\S]*?)_\b/gm, '<em>$1</em>');
+    text = text.replace(/\b___(\S[\s\S]*)___\b/g, '<strong><em>$1</em></strong>');
+    text = text.replace(/\b__(\S[\s\S]*)__\b/g, '<strong>$1</strong>');
+    text = text.replace(/\b_(\S[\s\S]*?)_\b/g, '<em>$1</em>');
   } else {
+    text = text.replace(/___(\S[\s\S]*?)___/g, function (wm, m) {
+      return (/\S$/.test(m)) ? '<strong><em>' + m + '</em></strong>' : wm;
+    });
     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) {
+    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><em>' + m + '</em></strong>' : wm;
+  });
   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) {
+  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;
   });

+ 4 - 0
test/cases/double-emphasis.html

@@ -0,0 +1,4 @@
+<p>a <strong><em>strong and em</em></strong> thingy</p>
+<p>bar<strong><em>bazinga</em></strong>bar</p>
+<p>a <strong><em>strong and em</em></strong> thingy</p>
+<p>bar<strong><em>bazinga</em></strong>bar</p>

+ 7 - 0
test/cases/double-emphasis.md

@@ -0,0 +1,7 @@
+a ___strong and em___ thingy
+
+bar___bazinga___bar
+
+a ***strong and em*** thingy
+
+bar***bazinga***bar

+ 4 - 0
test/issues/#332.inconsistent-behavior-of-emphasis-and-strong.html

@@ -1,11 +1,15 @@
 <p>foo *bar *baz</p>
 <p>foo **bar **baz</p>
+<p>foo ***bar ***baz</p>
 <p>foo _bar _baz</p>
 <p>foo __bar __baz</p>
+<p>foo ___bar ___baz</p>
 <p>foo *bar *baz *bazinga</p>
 <p>foo **bar **baz **bazinga</p>
+<p>foo ***bar ***baz ***bazinga</p>
 <p>foo _bar _baz __bazinga</p>
 <p>foo __bar __baz __bazinga</p>
+<p>foo ___bar ___baz ___bazinga</p>
 <p><em>f</em></p>
 <p><strong>f</strong></p>
 <p><em>f</em></p>

+ 8 - 0
test/issues/#332.inconsistent-behavior-of-emphasis-and-strong.md

@@ -2,18 +2,26 @@ foo *bar *baz
 
 foo **bar **baz
 
+foo ***bar ***baz
+
 foo _bar _baz
 
 foo __bar __baz
 
+foo ___bar ___baz
+
 foo *bar *baz *bazinga
 
 foo **bar **baz **bazinga
 
+foo ***bar ***baz ***bazinga
+
 foo _bar _baz __bazinga
 
 foo __bar __baz __bazinga
 
+foo ___bar ___baz ___bazinga
+
 *f*
 
 **f**

Некоторые файлы не были показаны из-за большого количества измененных файлов