Skip to content

Commit

Permalink
fix: parser slowness with certain inputs
Browse files Browse the repository at this point in the history
A bad desinged regex was causing the parser to become extremelly slow
when given some inputs.

Closes #315
  • Loading branch information
tivie committed Dec 19, 2016
1 parent 143f324 commit da8fb53
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 19 deletions.
13 changes: 5 additions & 8 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.

6 changes: 3 additions & 3 deletions src/subParsers/blockGamut.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ showdown.subParser('blockGamut', function (text, options, globals) {

// Do Horizontal Rules:
var key = showdown.subParser('hashBlock')('<hr />', options, globals);
text = text.replace(/^ {0,2}( ?\* ?){3,}[ \t]*$/gm, key);
text = text.replace(/^ {0,2}( ?- ?){3,}[ \t]*$/gm, key);
text = text.replace(/^ {0,2}( ?_ ?){3,}[ \t]*$/gm, key);
text = text.replace(/^( ?-){3,}[ \t]*$/gm, key);
text = text.replace(/^( ?\*){3,}[ \t]*$$/gm, key);
text = text.replace(/^( ?_){3,}[ \t]*$$/gm, key);

text = showdown.subParser('lists')(text, options, globals);
text = showdown.subParser('codeBlocks')(text, options, globals);
Expand Down
5 changes: 1 addition & 4 deletions src/subParsers/lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/
showdown.subParser('lists', function (text, options, globals) {
'use strict';

text = globals.converter._dispatch('lists.before', text, options, globals);
/**
* Process the contents of a single ordered or unordered list, splitting it
Expand Down Expand Up @@ -78,7 +77,7 @@ showdown.subParser('lists', function (text, options, globals) {
// <ul><li>- - a</li></ul>
// So, to prevent it, we will put a marker (~A)in the beginning of the line
// Kind of hackish/monkey patching, but seems more effective than overcomplicating the list parser
item = item.replace(/^([-*+]|\d\.)[ \t]+[\s\n]*/g, function (wm2) {
item = item.replace(/^([-*+]|\d\.)[ \t]+[\S\n ]*/g, function (wm2) {
return '~A' + wm2;
});

Expand All @@ -101,7 +100,6 @@ showdown.subParser('lists', function (text, options, globals) {

// now we need to remove the marker (~A)
item = item.replace('~A', '');

// we can finally wrap the line in list item tags
item = '<li' + bulletStyle + '>' + item + '</li>\n';
return item;
Expand Down Expand Up @@ -180,7 +178,6 @@ showdown.subParser('lists', function (text, options, globals) {

// strip sentinel
text = text.replace(/~0/, '');

text = globals.converter._dispatch('lists.after', text, options, globals);
return text;
});
3 changes: 3 additions & 0 deletions test/issues/#312.spaced-dashes-followed-by-char4.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<ul>
<li>- - - -- - - - - - - -- - - - - - - - - - - - - - - - - - - - abcd</li>
</ul>
1 change: 1 addition & 0 deletions test/issues/#312.spaced-dashes-followed-by-char4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- - - - -- - - - - - - -- - - - - - - - - - - - - - - - - - - - abcd

0 comments on commit da8fb53

Please sign in to comment.