diff --git a/src/Lexer.ts b/src/Lexer.ts index 5952b4a3..67767a4c 100644 --- a/src/Lexer.ts +++ b/src/Lexer.ts @@ -364,16 +364,14 @@ export class _Lexer { // One paren scan per inline run, shared with Tokenizer.link: without a // ')' ahead the link regex cannot match, and skipping it avoids its // quadratic backtracking over unterminated-link inputs like - // `'[a](b'.repeat(n)`. A nested run receives a substring of the outer - // run, so if the outer had no ')' neither does this substring — skip - // the scan in that case. + // `'[a](b'.repeat(n)`. Extension tokenizers can pass generated text to + // nested runs, so each run must check its own source. const prevLinkParenPossible = this.state.linkParenPossible; - this.state.linkParenPossible = prevLinkParenPossible && src.includes(')'); + this.state.linkParenPossible = src.includes(')'); try { return this.#inlineTokensInner(src, tokens); } finally { - // A nested run (e.g. a link label) anchored on its own substring; - // restore the outer run's flag for the rest of its loop. + // Restore the outer run's flag for the rest of its loop. this.state.linkParenPossible = prevLinkParenPossible; } } diff --git a/test/unit/marked.test.js b/test/unit/marked.test.js index 07290b73..f335f034 100644 --- a/test/unit/marked.test.js +++ b/test/unit/marked.test.js @@ -71,6 +71,57 @@ describe('marked unit', () => { }); }); + describe('nested inline tokenizers', () => { + const mention = { + name: 'mention', + level: 'inline', + start(src) { return src.indexOf('@'); }, + tokenizer(src) { + const match = /^@(alice|bob|plain)\b/.exec(src); + if (match) { + const text = match[1] === 'plain' ? 'plain' : `[${match[1]}](/users/${match[1]})`; + return { type: 'mention', raw: match[0], tokens: this.lexer.inlineTokens(text) }; + } + }, + renderer(token) { return this.parser.parseInline(token.tokens); }, + }; + + const cases = [ + ['a generated link without a closing parenthesis in the input', '@alice', 'alice'], + ['a generated link when the input contains a closing parenthesis', '@alice)', 'alice)'], + ['a generated link inside emphasis', '*Hello @alice*', 'Hello alice'], + ['multiple generated links', '@alice and @bob', 'alice and bob'], + ['the outer link after a child without a closing parenthesis', '@plain [outside](/outer)', 'plain outside'], + ]; + + for (const [name, input, expected] of cases) { + it(`should parse ${name}`, () => { + marked.use({ extensions: [mention] }); + assert.strictEqual(marked.parseInline(input), expected); + }); + } + + it('should parse a generated link in a block', () => { + marked.use({ extensions: [mention] }); + assert.strictEqual(marked.parse('@alice'), '

alice

\n'); + }); + + it('should parse generated links from an overridden tokenizer', () => { + marked.use({ + tokenizer: { + emStrong(src) { + if (src.startsWith('@')) { + const text = '[inner](/inside)'; + return { type: 'strong', raw: '@', text, tokens: this.lexer.inlineTokens(text) }; + } + return false; + }, + }, + }); + assert.strictEqual(marked.parseInline('@'), 'inner'); + }); + }); + describe('use extension', () => { it('should use custom block tokenizer + renderer extensions', () => { const underline = {