Skip to content

Commit

Permalink
fix(italicsAndBold): fiz inconsistency in italicsAndBold parsing
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tivie committed Jan 29, 2017
1 parent e4c43ea commit a4f05d4
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 16 deletions.
22 changes: 16 additions & 6 deletions dist/showdown.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/showdown.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions dist/showdown.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/showdown.min.js.map

Large diffs are not rendered by default.

22 changes: 16 additions & 6 deletions src/subParsers/italicsAndBold.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
15 changes: 15 additions & 0 deletions test/issues/#332.inconsistent-behavior-of-emphasis-and-strong.html
Original file line number Diff line number Diff line change
@@ -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 changes: 29 additions & 0 deletions test/issues/#332.inconsistent-behavior-of-emphasis-and-strong.md
Original file line number Diff line number Diff line change
@@ -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__

0 comments on commit a4f05d4

Please sign in to comment.