mirror of
https://github.com/markedjs/marked.git
synced 2026-11-30 16:48:05 +01:00
* fix: Avoid RegExp lookbehind assertions (#3816) RegExp lookbehind assertions are supported in Safari >= 16.5, avoiding them keeps support for older Safari versions. * Improve perf of blockSkip on modern engines RegExp lookbehind assertions are supported in Safari >= 16.5. But the lookbehind-less version has a performance penalty. So having a fallback keeps support for older Safari versions while maintaining performance for modern engines. * Add comment about lookbehind browser support As suggested by @styfle Co-authored-by: Steven <steven@ceriously.com> --------- Co-authored-by: Steven <steven@ceriously.com>
This commit is contained in:
co-authored by
Steven
parent
0551ab5968
commit
c056df082c
+3
-1
@@ -320,8 +320,10 @@ export class _Lexer<ParserOutput = string, RendererOutput = string> {
|
||||
}
|
||||
|
||||
// Mask out other blocks
|
||||
let offset;
|
||||
while ((match = this.tokenizer.rules.inline.blockSkip.exec(maskedSrc)) != null) {
|
||||
maskedSrc = maskedSrc.slice(0, match.index) + '[' + 'a'.repeat(match[0].length - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.blockSkip.lastIndex);
|
||||
offset = match[2] ? match[2].length : 0;
|
||||
maskedSrc = maskedSrc.slice(0, match.index + offset) + '[' + 'a'.repeat(match[0].length - offset - 2) + ']' + maskedSrc.slice(this.tokenizer.rules.inline.blockSkip.lastIndex);
|
||||
}
|
||||
|
||||
// Mask out blocks from extensions
|
||||
|
||||
+15
-3
@@ -16,6 +16,17 @@ function edit(regex: string | RegExp, opt = '') {
|
||||
return obj;
|
||||
}
|
||||
|
||||
const supportsLookbehind = (() => {
|
||||
try {
|
||||
// eslint-disable-next-line prefer-regex-literals
|
||||
return !!new RegExp('(?<=1)(?<!1)');
|
||||
} catch {
|
||||
// See browser support here:
|
||||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Regular_expressions/Lookbehind_assertion
|
||||
return false;
|
||||
}
|
||||
})();
|
||||
|
||||
export const other = {
|
||||
codeRemoveIndent: /^(?: {1,4}| {0,3}\t)/gm,
|
||||
outputLinkReplace: /\\([\[\]])/g,
|
||||
@@ -267,9 +278,10 @@ const _punctuationOrSpaceGfmStrongEm = /(?!~)[\s\p{P}\p{S}]/u;
|
||||
const _notPunctuationOrSpaceGfmStrongEm = /(?:[^\s\p{P}\p{S}]|~)/u;
|
||||
|
||||
// sequences em should skip over [title](link), `code`, <html>
|
||||
const blockSkip = edit(/link|code|html/, 'g')
|
||||
.replace('link', /\[(?:[^\[\]`]|(?<!`)(?<a>`+)[^`]+\k<a>(?!`))*?\]\((?:\\[\s\S]|[^\\\(\)]|\((?:\\[\s\S]|[^\\\(\)])*\))*\)/)
|
||||
.replace('code', /(?<!`)(?<b>`+)[^`]+\k<b>(?!`)/)
|
||||
const blockSkip = edit(/link|precode-code|html/, 'g')
|
||||
.replace('link', /\[(?:[^\[\]`]|(?<a>`+)[^`]+\k<a>(?!`))*?\]\((?:\\[\s\S]|[^\\\(\)]|\((?:\\[\s\S]|[^\\\(\)])*\))*\)/)
|
||||
.replace('precode-', supportsLookbehind ? '(?<!`)()' : '(^^|[^`])')
|
||||
.replace('code', /(?<b>`+)[^`]+\k<b>(?!`)/)
|
||||
.replace('html', /<(?! )[^<>]*?>/)
|
||||
.getRegex();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user