Skip to content

Commit 1832b7f

Browse files
committed
fix(italicsAndBold): fix double emphasis edge case
1 parent ab54933 commit 1832b7f

9 files changed

+53
-18
lines changed

dist/showdown.js

Lines changed: 13 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/showdown.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/showdown.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/showdown.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/subParsers/italicsAndBold.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,36 @@ showdown.subParser('italicsAndBold', function (text, options, globals) {
33

44
text = globals.converter._dispatch('italicsAndBold.before', text, options, globals);
55

6-
// it's faster to have 2 separate regexes for each case than have just one
6+
// it's faster to have 3 separate regexes for each case than have just one
77
// because of backtracing, in some cases, it could lead to an exponential effect
88
// called "catastrophic backtrace". Ominous!
99

1010
// Parse underscores
1111
if (options.literalMidWordUnderscores) {
12-
text = text.replace(/\b__(\S[\s\S]*?)__\b/gm, '<strong>$1</strong>');
13-
text = text.replace(/\b_(\S[\s\S]*?)_\b/gm, '<em>$1</em>');
12+
text = text.replace(/\b___(\S[\s\S]*)___\b/g, '<strong><em>$1</em></strong>');
13+
text = text.replace(/\b__(\S[\s\S]*)__\b/g, '<strong>$1</strong>');
14+
text = text.replace(/\b_(\S[\s\S]*?)_\b/g, '<em>$1</em>');
1415
} else {
16+
text = text.replace(/___(\S[\s\S]*?)___/g, function (wm, m) {
17+
return (/\S$/.test(m)) ? '<strong><em>' + m + '</em></strong>' : wm;
18+
});
1519
text = text.replace(/__(\S[\s\S]*?)__/g, function (wm, m) {
1620
return (/\S$/.test(m)) ? '<strong>' + m + '</strong>' : wm;
1721
});
18-
text = text.replace(/_(\S[\s\S]*?)_/g, function (wm, m) {
22+
text = text.replace(/_([^\s_][\s\S]*?)_/g, function (wm, m) {
1923
// !/^_[^_]/.test(m) - test if it doesn't start with __ (since it seems redundant, we removed it)
2024
return (/\S$/.test(m)) ? '<em>' + m + '</em>' : wm;
2125
});
2226
}
2327

2428
// Now parse asterisks
29+
text = text.replace(/\*\*\*(\S[\s\S]*?)\*\*\*/g, function (wm, m) {
30+
return (/\S$/.test(m)) ? '<strong><em>' + m + '</em></strong>' : wm;
31+
});
2532
text = text.replace(/\*\*(\S[\s\S]*?)\*\*/g, function (wm, m) {
2633
return (/\S$/.test(m)) ? '<strong>' + m + '</strong>' : wm;
2734
});
28-
29-
text = text.replace(/\*(\S[\s\S]*?)\*/g, function (wm, m) {
35+
text = text.replace(/\*([^\s*][\s\S]*?)\*/g, function (wm, m) {
3036
// !/^\*[^*]/.test(m) - test if it doesn't start with ** (since it seems redundant, we removed it)
3137
return (/\S$/.test(m)) ? '<em>' + m + '</em>' : wm;
3238
});

test/cases/double-emphasis.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<p>a <strong><em>strong and em</em></strong> thingy</p>
2+
<p>bar<strong><em>bazinga</em></strong>bar</p>
3+
<p>a <strong><em>strong and em</em></strong> thingy</p>
4+
<p>bar<strong><em>bazinga</em></strong>bar</p>

test/cases/double-emphasis.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
a ___strong and em___ thingy
2+
3+
bar___bazinga___bar
4+
5+
a ***strong and em*** thingy
6+
7+
bar***bazinga***bar

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
<p>foo *bar *baz</p>
22
<p>foo **bar **baz</p>
3+
<p>foo ***bar ***baz</p>
34
<p>foo _bar _baz</p>
45
<p>foo __bar __baz</p>
6+
<p>foo ___bar ___baz</p>
57
<p>foo *bar *baz *bazinga</p>
68
<p>foo **bar **baz **bazinga</p>
9+
<p>foo ***bar ***baz ***bazinga</p>
710
<p>foo _bar _baz __bazinga</p>
811
<p>foo __bar __baz __bazinga</p>
12+
<p>foo ___bar ___baz ___bazinga</p>
913
<p><em>f</em></p>
1014
<p><strong>f</strong></p>
1115
<p><em>f</em></p>

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,26 @@ foo *bar *baz
22

33
foo **bar **baz
44

5+
foo ***bar ***baz
6+
57
foo _bar _baz
68

79
foo __bar __baz
810

11+
foo ___bar ___baz
12+
913
foo *bar *baz *bazinga
1014

1115
foo **bar **baz **bazinga
1216

17+
foo ***bar ***baz ***bazinga
18+
1319
foo _bar _baz __bazinga
1420

1521
foo __bar __baz __bazinga
1622

23+
foo ___bar ___baz ___bazinga
24+
1725
*f*
1826

1927
**f**

0 commit comments

Comments
 (0)