From 171f177a42f09d4812c68b045c3bb50122f6b2aa Mon Sep 17 00:00:00 2001 From: generaltony007 <191973953+hakarrr@users.noreply.github.com> Date: Tue, 17 Nov 2026 21:07:23 +0530 Subject: [PATCH] perf: skip paragraph lookahead for initial quoted code (#4233) * fix: skip paragraph lookahead for initial quoted code Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com> * test: preserve quoted paragraph continuation boundary Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com> * test: cover alternating indented code and quoted code Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com> --------- Co-authored-by: GeneralTony007 <144252170+GeneralTony007@users.noreply.github.com> --- src/rules.ts | 10 ++++++++-- .../redos/alternating_indented_code_blockquote.cjs | 4 ++++ test/unit/Lexer.test.js | 6 ++++++ 3 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 test/specs/redos/alternating_indented_code_blockquote.cjs diff --git a/src/rules.ts b/src/rules.ts index 601c598a..2297cc1a 100644 --- a/src/rules.ts +++ b/src/rules.ts @@ -217,8 +217,14 @@ const blockquoteParagraph = createParagraph(/ {0,3}(?:[*+-]|\d{1,9}[.)])(?:[ \t] // a line with only whitespace after the marker is blank, so it must not start a // paragraph that lazily continues onto the next line -const blockquote = edit(/^( {0,3}> ?(?:(?![ \t]*(?:\n|$))(paragraph)|[^\n]*)(?:\n|$))+/) - .replace('paragraph', blockquoteParagraph) +// Only a paragraph can lazily continue the first quoted line. Indented code +// must not scan the remaining document looking for paragraph continuation. +// Later quoted lines may continue an earlier paragraph and keep the old rule. +const blockquoteLine = / {0,3}> ?(?:(?!blank)(paragraph)|[^\n]*)(?:\n|$)/; +const blockquote = edit(/^(first)(?:next)*/) + .replace('first', edit(blockquoteLine).replace('blank', /[ \t]*(?:\n|$)| {4}/).getRegex()) + .replace('next', edit(blockquoteLine).replace('blank', /[ \t]*(?:\n|$)/).getRegex()) + .replace(/paragraph/g, blockquoteParagraph) .getRegex(); /** diff --git a/test/specs/redos/alternating_indented_code_blockquote.cjs b/test/specs/redos/alternating_indented_code_blockquote.cjs new file mode 100644 index 00000000..6c0e3bcf --- /dev/null +++ b/test/specs/redos/alternating_indented_code_blockquote.cjs @@ -0,0 +1,4 @@ +module.exports = { + markdown: ' x\n> y\n'.repeat(5000), + html: '
x\n\n\n\n'.repeat(5000), +}; diff --git a/test/unit/Lexer.test.js b/test/unit/Lexer.test.js index a020bda7..13c3b9b8 100644 --- a/test/unit/Lexer.test.js +++ b/test/unit/Lexer.test.js @@ -27,6 +27,12 @@ function expectInlineTokens({ md, options, tokens, links = {} }) { describe('Lexer', () => { describe('rules', () => { + it('should not scan lazy lines after an initial quoted code line', () => { + const rule = new Lexer().tokenizer.rules.block.blockquote; + assert.strictEqual(rule.exec('> y\n x\n> y\n')[0], '> y\n'); + assert.strictEqual(rule.exec('> a\n> y\n x\n')[0], '> a\n> y\n x\n'); + }); + it('should create indentation regexes for zero indent', () => { const { nextBulletRegex,\ny\n