fix(literalMidWordUnderscores): Inconsistent behavior of emphasis and strong with literalMidWordUndescores

Closes #333
This commit is contained in:
Estevao Soares dos Santos 2017-01-29 23:31:52 +00:00
parent a4f05d4693
commit 0292ae0dcb
7 changed files with 21 additions and 21 deletions

BIN
dist/showdown.js vendored

Binary file not shown.

BIN
dist/showdown.js.map vendored

Binary file not shown.

BIN
dist/showdown.min.js vendored

Binary file not shown.

Binary file not shown.

View File

@ -6,33 +6,30 @@ showdown.subParser('italicsAndBold', function (text, options, globals) {
// it's faster to have 2 separate regexes for each case than have just one // 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 // because of backtracing, in some cases, it could lead to an exponential effect
// called "catastrophic backtrace". Ominous! // called "catastrophic backtrace". Ominous!
// Parse underscores
if (options.literalMidWordUnderscores) { 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, '<strong>$1</strong>');
text = text.replace(/\b_(\S[\s\S]*?)_\b/gm, '<em>$1</em>'); 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 { } else {
// <strong> must go first:
text = text.replace(/__(\S[\s\S]*?)__/g, function (wm, m) { text = text.replace(/__(\S[\s\S]*?)__/g, function (wm, m) {
return (/\S$/.test(m)) ? '<strong>' + m + '</strong>' : wm; 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) { 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) // !/^_[^_]/.test(m) - test if it doesn't start with __ (since it seems redundant, we removed it)
return (/\S$/.test(m)) ? '<em>' + m + '</em>' : wm; 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) { 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) // !/^\*[^*]/.test(m) - test if it doesn't start with ** (since it seems redundant, we removed it)
return (/\S$/.test(m)) ? '<em>' + m + '</em>' : wm; return (/\S$/.test(m)) ? '<em>' + m + '</em>' : wm;
}); });
}
text = globals.converter._dispatch('italicsAndBold.after', text, options, globals); text = globals.converter._dispatch('italicsAndBold.after', text, options, globals);
return text; return text;

View File

@ -1,3 +1,4 @@
<p>pointer *ptr *thing</p> <p>foo *bar *baz</p>
<p>something _else _bla</p> <p>foo **bar **baz</p>
<p>something __else __bla</p> <p>foo _bar _baz</p>
<p>foo __bar __baz</p>

View File

@ -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