Skip to content

Commit

Permalink
fix: make some regexes a bit faster and make tab char equivalent to 4…
Browse files Browse the repository at this point in the history
… spaces
  • Loading branch information
tivie committed Aug 30, 2016
1 parent 799abea commit b7e7560
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 253 deletions.
139 changes: 15 additions & 124 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.

2 changes: 1 addition & 1 deletion 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.

64 changes: 0 additions & 64 deletions src/subParsers/anchors.js
Expand Up @@ -53,79 +53,15 @@ showdown.subParser('anchors', function (text, options, globals) {
};

// First, handle reference-style links: [link text] [id]
/*
text = text.replace(/
( // wrap whole match in $1
\[
(
(?:
\[[^\]]*\] // allow brackets nested one level
|
[^\[] // or anything else
)*
)
\]
[ ]? // one optional space
(?:\n[ ]*)? // one optional newline followed by spaces
\[
(.*?) // id = $3
\]
)()()()() // pad remaining backreferences
/g,_DoAnchors_callback);
*/
text = text.replace(/(\[((?:\[[^\]]*]|[^\[\]])*)][ ]?(?:\n[ ]*)?\[(.*?)])()()()()/g, writeAnchorTag);

//
// Next, inline-style links: [link text](url "optional title")
//

/*
text = text.replace(/
( // wrap whole match in $1
\[
(
(?:
\[[^\]]*\] // allow brackets nested one level
|
[^\[\]] // or anything else
)
)
\]
\( // literal paren
[ \t]*
() // no id, so leave $3 empty
<?(.*?)>? // href = $4
[ \t]*
( // $5
(['"]) // quote char = $6
(.*?) // Title = $7
\6 // matching quote
[ \t]* // ignore any spaces/tabs between closing quote and )
)? // title is optional
\)
)
/g,writeAnchorTag);
*/
text = text.replace(/(\[((?:\[[^\]]*]|[^\[\]])*)]\([ \t]*()<?(.*?(?:\(.*?\).*?)?)>?[ \t]*((['"])(.*?)\6[ \t]*)?\))/g,
writeAnchorTag);

//
// Last, handle reference-style shortcuts: [link text]
// These must come last in case you've also got [link test][1]
// or [link test](/foo)
//

/*
text = text.replace(/
( // wrap whole match in $1
\[
([^\[\]]+) // link text = $2; can't contain '[' or ']'
\]
)()()()()() // pad rest of backreferences
/g, writeAnchorTag);
*/
text = text.replace(/(\[([^\[\]]+)])()()()()()/g, writeAnchorTag);

text = globals.converter._dispatch('anchors.after', text, options, globals);
Expand Down
2 changes: 1 addition & 1 deletion src/subParsers/autoLinks.js
Expand Up @@ -5,7 +5,7 @@ showdown.subParser('autoLinks', function (text, options, globals) {

var simpleURLRegex = /\b(((https?|ftp|dict):\/\/|www\.)[^'">\s]+\.[^'">\s]+)(?=\s|$)(?!["<>])/gi,
delimUrlRegex = /<(((https?|ftp|dict):\/\/|www\.)[^'">\s]+)>/gi,
simpleMailRegex = /(?:^|[ \n\t])([A-Za-z0-9!#$%&'*+-/=?^_`\{|}~\.]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)(?:$|[ \n\t])/gi,
simpleMailRegex = /(?:^|\s)([A-Za-z0-9!#$%&'*+-/=?^_`\{|}~\.]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)(?:$|\s)/gi,
delimMailRegex = /<(?:mailto:)?([-.\w]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)>/gi;

text = text.replace(delimUrlRegex, replaceLink);
Expand Down
16 changes: 2 additions & 14 deletions src/subParsers/blockQuotes.js
Expand Up @@ -2,20 +2,8 @@ showdown.subParser('blockQuotes', function (text, options, globals) {
'use strict';

text = globals.converter._dispatch('blockQuotes.before', text, options, globals);
/*
text = text.replace(/
( // Wrap whole match in $1
(
^[ \t]*>[ \t]? // '>' at the start of a line
.+\n // rest of the first line
(.+\n)* // subsequent consecutive lines
\n* // blanks
)+
)
/gm, function(){...});
*/

text = text.replace(/((^[ \t]{0,3}>[ \t]?.+\n(.+\n)*\n*)+)/gm, function (wholeMatch, m1) {

text = text.replace(/((^ {0,3}>[ \t]?.+\n(.+\n)*\n*)+)/gm, function (wholeMatch, m1) {
var bq = m1;

// attacklab: hack around Konqueror 3.5.4 bug:
Expand Down
18 changes: 3 additions & 15 deletions src/subParsers/codeBlocks.js
Expand Up @@ -5,20 +5,8 @@ showdown.subParser('codeBlocks', function (text, options, globals) {
'use strict';

text = globals.converter._dispatch('codeBlocks.before', text, options, globals);
/*
text = text.replace(text,
/(?:\n\n|^)
( // $1 = the code block -- one or more lines, starting with a space/tab
(?:
(?:[ ]{4}|\t) // Lines must start with a tab or a tab-width of spaces - attacklab: g_tab_width
.*\n+
)+
)
(\n*[ ]{0,3}[^ \t\n]|(?=~0)) // attacklab: g_tab_width
/g,function(){...});
*/

// attacklab: sentinel workarounds for lack of \A and \Z, safari\khtml bug

// sentinel workarounds for lack of \A and \Z, safari\khtml bug
text += '~0';

var pattern = /(?:\n\n|^)((?:(?:[ ]{4}|\t).*\n+)+)(\n*[ ]{0,3}[^ \t\n]|(?=~0))/g;
Expand All @@ -42,7 +30,7 @@ showdown.subParser('codeBlocks', function (text, options, globals) {
return showdown.subParser('hashBlock')(codeblock, options, globals) + nextChar;
});

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

text = globals.converter._dispatch('codeBlocks.after', text, options, globals);
Expand Down

0 comments on commit b7e7560

Please sign in to comment.