Skip to content

Commit b7e7560

Browse files
committed
fix: make some regexes a bit faster and make tab char equivalent to 4 spaces
1 parent 799abea commit b7e7560

13 files changed

Lines changed: 35 additions & 253 deletions

dist/showdown.js

Lines changed: 15 additions & 124 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/showdown.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/showdown.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/showdown.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/subParsers/anchors.js

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -53,79 +53,15 @@ showdown.subParser('anchors', function (text, options, globals) {
5353
};
5454

5555
// First, handle reference-style links: [link text] [id]
56-
/*
57-
text = text.replace(/
58-
( // wrap whole match in $1
59-
\[
60-
(
61-
(?:
62-
\[[^\]]*\] // allow brackets nested one level
63-
|
64-
[^\[] // or anything else
65-
)*
66-
)
67-
\]
68-
69-
[ ]? // one optional space
70-
(?:\n[ ]*)? // one optional newline followed by spaces
71-
72-
\[
73-
(.*?) // id = $3
74-
\]
75-
)()()()() // pad remaining backreferences
76-
/g,_DoAnchors_callback);
77-
*/
7856
text = text.replace(/(\[((?:\[[^\]]*]|[^\[\]])*)][ ]?(?:\n[ ]*)?\[(.*?)])()()()()/g, writeAnchorTag);
7957

80-
//
8158
// Next, inline-style links: [link text](url "optional title")
82-
//
83-
84-
/*
85-
text = text.replace(/
86-
( // wrap whole match in $1
87-
\[
88-
(
89-
(?:
90-
\[[^\]]*\] // allow brackets nested one level
91-
|
92-
[^\[\]] // or anything else
93-
)
94-
)
95-
\]
96-
\( // literal paren
97-
[ \t]*
98-
() // no id, so leave $3 empty
99-
<?(.*?)>? // href = $4
100-
[ \t]*
101-
( // $5
102-
(['"]) // quote char = $6
103-
(.*?) // Title = $7
104-
\6 // matching quote
105-
[ \t]* // ignore any spaces/tabs between closing quote and )
106-
)? // title is optional
107-
\)
108-
)
109-
/g,writeAnchorTag);
110-
*/
11159
text = text.replace(/(\[((?:\[[^\]]*]|[^\[\]])*)]\([ \t]*()<?(.*?(?:\(.*?\).*?)?)>?[ \t]*((['"])(.*?)\6[ \t]*)?\))/g,
11260
writeAnchorTag);
11361

114-
//
11562
// Last, handle reference-style shortcuts: [link text]
11663
// These must come last in case you've also got [link test][1]
11764
// or [link test](/foo)
118-
//
119-
120-
/*
121-
text = text.replace(/
122-
( // wrap whole match in $1
123-
\[
124-
([^\[\]]+) // link text = $2; can't contain '[' or ']'
125-
\]
126-
)()()()()() // pad rest of backreferences
127-
/g, writeAnchorTag);
128-
*/
12965
text = text.replace(/(\[([^\[\]]+)])()()()()()/g, writeAnchorTag);
13066

13167
text = globals.converter._dispatch('anchors.after', text, options, globals);

src/subParsers/autoLinks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ showdown.subParser('autoLinks', function (text, options, globals) {
55

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

1111
text = text.replace(delimUrlRegex, replaceLink);

src/subParsers/blockQuotes.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,8 @@ showdown.subParser('blockQuotes', function (text, options, globals) {
22
'use strict';
33

44
text = globals.converter._dispatch('blockQuotes.before', text, options, globals);
5-
/*
6-
text = text.replace(/
7-
( // Wrap whole match in $1
8-
(
9-
^[ \t]*>[ \t]? // '>' at the start of a line
10-
.+\n // rest of the first line
11-
(.+\n)* // subsequent consecutive lines
12-
\n* // blanks
13-
)+
14-
)
15-
/gm, function(){...});
16-
*/
17-
18-
text = text.replace(/((^[ \t]{0,3}>[ \t]?.+\n(.+\n)*\n*)+)/gm, function (wholeMatch, m1) {
5+
6+
text = text.replace(/((^ {0,3}>[ \t]?.+\n(.+\n)*\n*)+)/gm, function (wholeMatch, m1) {
197
var bq = m1;
208

219
// attacklab: hack around Konqueror 3.5.4 bug:

src/subParsers/codeBlocks.js

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,8 @@ showdown.subParser('codeBlocks', function (text, options, globals) {
55
'use strict';
66

77
text = globals.converter._dispatch('codeBlocks.before', text, options, globals);
8-
/*
9-
text = text.replace(text,
10-
/(?:\n\n|^)
11-
( // $1 = the code block -- one or more lines, starting with a space/tab
12-
(?:
13-
(?:[ ]{4}|\t) // Lines must start with a tab or a tab-width of spaces - attacklab: g_tab_width
14-
.*\n+
15-
)+
16-
)
17-
(\n*[ ]{0,3}[^ \t\n]|(?=~0)) // attacklab: g_tab_width
18-
/g,function(){...});
19-
*/
20-
21-
// attacklab: sentinel workarounds for lack of \A and \Z, safari\khtml bug
8+
9+
// sentinel workarounds for lack of \A and \Z, safari\khtml bug
2210
text += '~0';
2311

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

45-
// attacklab: strip sentinel
33+
// strip sentinel
4634
text = text.replace(/~0/, '');
4735

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

0 commit comments

Comments
 (0)