Skip to content

Commit

Permalink
refactor(italicsAndBold): refactoring of italicsAndBold regexes for s…
Browse files Browse the repository at this point in the history
…peed
  • Loading branch information
tivie committed Jan 29, 2017
1 parent 062e465 commit e4c43ea
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 14 deletions.
16 changes: 11 additions & 5 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.

16 changes: 11 additions & 5 deletions src/subParsers/italicsAndBold.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@ showdown.subParser('italicsAndBold', function (text, options, globals) {

text = globals.converter._dispatch('italicsAndBold.before', 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!
if (options.literalMidWordUnderscores) {
//underscores
// Since we are consuming a \s character, we need to add it
text = text.replace(/(^|\s|>|\b)__(?=\S)([\s\S]+?)__(?=\b|<|\s|$)/gm, '$1<strong>$2</strong>');
text = text.replace(/(^|\s|>|\b)_(?=\S)([\s\S]+?)_(?=\b|<|\s|$)/gm, '$1<em>$2</em>');
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[*]*)\1/g, '<strong>$2</strong>');
text = text.replace(/\*\*(?=\S)([^\r]*?\S[*]*)\*\*/g, '<strong>$1</strong>');
text = text.replace(/(\*)(?=\S)([^\r]*?\S)\1/g, '<em>$2</em>');

} else {
// <strong> must go first:
text = text.replace(/(\*\*|__)(?=\S)([^\r]*?\S[*_]*)\1/g, '<strong>$2</strong>');
text = text.replace(/(\*|_)(?=\S)([^\r]*?\S)\1/g, '<em>$2</em>');
text = text.replace(/__(\S[\s\S]*?)__/g, '<strong>$1</strong>');
text = text.replace(/\*\*(\S[\s\S]*?)\*\*/g, '<strong>$1</strong>');
// now <em>
text = text.replace(/_(\S[\s\S]*?)_/g, '<em>$1</em>');
text = text.replace(/\*(\S[\s\S]*?)\*/g, '<em>$1</em>');
}

text = globals.converter._dispatch('italicsAndBold.after', text, options, globals);
Expand Down
2 changes: 2 additions & 0 deletions test/cases/emphasis.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@
<p>escaped word*with*asterixs</p>
<p>escaped word**with**asterixs</p>
<p>escaped word<strong>*with*</strong>bold asterixs</p>
<p>foo<strong>bar</strong>baz</p>
<p>foo<strong>bar</strong>baz</p>
4 changes: 4 additions & 0 deletions test/cases/emphasis.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,7 @@ escaped word\*with*asterixs
escaped word\*\*with\*\*asterixs

escaped word**\*with\***bold asterixs

foo**bar**baz

foo__bar__baz

0 comments on commit e4c43ea

Please sign in to comment.