Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/Lexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,16 +357,14 @@ export class _Lexer<ParserOutput = string, RendererOutput = string> {
// 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;
}
}
Expand Down
51 changes: 51 additions & 0 deletions test/unit/marked.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', '<a href="/users/alice">alice</a>'],
['a generated link when the input contains a closing parenthesis', '@alice)', '<a href="/users/alice">alice</a>)'],
['a generated link inside emphasis', '*Hello @alice*', '<em>Hello <a href="/users/alice">alice</a></em>'],
['multiple generated links', '@alice and @bob', '<a href="/users/alice">alice</a> and <a href="/users/bob">bob</a>'],
['the outer link after a child without a closing parenthesis', '@plain [outside](/outer)', 'plain <a href="/outer">outside</a>'],
];

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'), '<p><a href="/users/alice">alice</a></p>\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('@'), '<strong><a href="/inside">inner</a></strong>');
});
});

describe('use extension', () => {
it('should use custom block tokenizer + renderer extensions', () => {
const underline = {
Expand Down
Loading