Skip to content

Commit

Permalink
fix(literalMidWordUnderscores): Inconsistent behavior of emphasis and…
Browse files Browse the repository at this point in the history
… strong with literalMidWordUndescores

Closes #333
  • Loading branch information
tivie committed Jan 29, 2017
1 parent a4f05d4 commit 0292ae0
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 40 deletions.
27 changes: 12 additions & 15 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.

27 changes: 12 additions & 15 deletions src/subParsers/italicsAndBold.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Original file line number Diff line number Diff line change
@@ -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>
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 0292ae0

Please sign in to comment.