فهرست منبع

fix(italicsAndBold): fiz inconsistency in italicsAndBold parsing

The way showdown parsed cases suchs as this:
```
**foo **bar
__foo __bar
*foo *bar
_foo _bar
```
was inconsistent. This established that `__` or `**` preceeding a word
would not be parsed as em or strong.

Closes #332
Estevao Soares dos Santos 8 سال پیش
والد
کامیت
a4f05d4693

+ 16 - 6
dist/showdown.js

@@ -2098,16 +2098,26 @@ showdown.subParser('italicsAndBold', function (text, options, globals) {
     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)([^\r]*?\S[*]*)\*\*/g, '<strong>$1</strong>');
-    text = text.replace(/(\*)(?=\S)([^\r]*?\S)\1/g, '<em>$2</em>');
+    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, '<strong>$1</strong>');
-    text = text.replace(/\*\*(\S[\s\S]*?)\*\*/g, '<strong>$1</strong>');
+    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, '<em>$1</em>');
-    text = text.replace(/\*(\S[\s\S]*?)\*/g, '<em>$1</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;
+    });
   }
 
   text = globals.converter._dispatch('italicsAndBold.after', text, options, globals);

تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 0 - 0
dist/showdown.js.map


تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 0 - 0
dist/showdown.min.js


تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است
+ 0 - 0
dist/showdown.min.js.map


+ 16 - 6
src/subParsers/italicsAndBold.js

@@ -12,16 +12,26 @@ showdown.subParser('italicsAndBold', function (text, options, globals) {
     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)([^\r]*?\S[*]*)\*\*/g, '<strong>$1</strong>');
-    text = text.replace(/(\*)(?=\S)([^\r]*?\S)\1/g, '<em>$2</em>');
+    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, '<strong>$1</strong>');
-    text = text.replace(/\*\*(\S[\s\S]*?)\*\*/g, '<strong>$1</strong>');
+    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, '<em>$1</em>');
-    text = text.replace(/\*(\S[\s\S]*?)\*/g, '<em>$1</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;
+    });
   }
 
   text = globals.converter._dispatch('italicsAndBold.after', text, options, globals);

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

@@ -0,0 +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 *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>
+<p><strong>f</strong></p>
+<p>foo **bar **baz <strong>bazinga bla</strong></p>
+<p>foo <strong>bar **baz **bazinga bla</strong></p>
+<p>foo <strong>**bar **baz **bazinga bla</strong></p>

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

@@ -0,0 +1,29 @@
+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
+
+*f*
+
+**f**
+
+_f_
+
+__f__
+
+foo **bar **baz **bazinga bla**
+
+foo __bar **baz **bazinga bla__
+
+foo __**bar **baz **bazinga bla__

برخی فایل ها در این مقایسه diff نمایش داده نمی شوند زیرا تعداد فایل ها بسیار زیاد است