Skip to content

Commit

Permalink
Improve worst-case performance of inline.text regex, marked#1460
Browse files Browse the repository at this point in the history
  • Loading branch information
yahtnif committed May 6, 2019
1 parent 6d1b115 commit 22defb9
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/inline-lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export class InlineLexer {
reflink: /^!?\[(label)\]\[(?!\s*\])((?:\\[\[\]]?|[^\[\]\\])+)\]/,
strong: /^__([^\s_])__(?!_)|^\*\*([^\s*])\*\*(?!\*)|^__([^\s][\s\S]*?[^\s])__(?!_)|^\*\*([^\s][\s\S]*?[^\s])\*\*(?!\*)/,
tag: new RegExp(tag),
text: /^(`+|[^`])[\s\S]*?(?=[\\<!\[`*]|\b_| {2,}\n|$)/
text: /^(`+|[^`])(?:[\s\S]*?(?:(?=[\\<!\[`*]|\b_|$)|[^ ](?= {2,}\n))|(?= {2,}\n))/
}

const _attribute: RegExp = /\s+[a-zA-Z:_][\w.:-]*(?:\s*=\s*"[^"]*"|\s*=\s*'[^']*'|\s*=\s*[^\s"'=<>`]+)?/
Expand Down Expand Up @@ -221,13 +221,7 @@ export class InlineLexer {
*/
const del: RegExp = /^~~(?=\S)([\s\S]*?\S)~~/

const text: RegExp = new ExtendRegexp(base.text)
.setGroup(']|', '~]|')
.setGroup(
'|$',
"|https?://|ftp://|www\\.|[a-zA-Z0-9.!#$%&'*+/=?^_`{\\|}~-]+@|$"
)
.getRegex()
const text = /^(`+|[^`])(?:[\s\S]*?(?:(?=[\\<!\[`*~]|\b_|https?:\/\/|ftp:\/\/|www\.|$)|[^ ](?= {2,}\n)|[^a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-](?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@))|(?= {2,}\n|[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@))/

return (this.gfmRules = {
...base,
Expand All @@ -250,7 +244,7 @@ export class InlineLexer {
...gfm,
...{
br: new ExtendRegexp(gfm.br).setGroup('{2,}', '*').getRegex(),
text: new ExtendRegexp(gfm.text).setGroup('{2,}', '*').getRegex()
text: new ExtendRegexp(gfm.text).setGroup(/\{2,\}/g, '*').getRegex()
}
})
}
Expand Down Expand Up @@ -420,7 +414,10 @@ export class InlineLexer {
if ((execArr = this.rules.link.exec(nextPart))) {
const lastParenIndex = this.findClosingBracket(execArr[2], '()')
if (lastParenIndex > -1) {
const linkLen = execArr[0].length - (execArr[2].length - lastParenIndex) - (execArr[3] || '').length
const linkLen =
execArr[0].length -
(execArr[2].length - lastParenIndex) -
(execArr[3] || '').length
execArr[2] = execArr[2].substring(0, lastParenIndex)
execArr[0] = execArr[0].substring(0, linkLen).trim()
execArr[3] = ''
Expand Down

0 comments on commit 22defb9

Please sign in to comment.