fix: skip missing optional childTokens properties in walkTokens (#4262)

This commit is contained in:
Ragib MRB
2026-11-22 23:31:24 -07:00
committed by GitHub
parent f5ffd221ba
commit 283f74f24b
2 changed files with 28 additions and 2 deletions
+4 -2
View File
@@ -87,8 +87,10 @@ export class Marked<ParserOutput = string, RendererOutput = string> {
const genericToken = token as Tokens.Generic;
if (this.defaults.extensions?.childTokens?.[genericToken.type]) {
this.defaults.extensions.childTokens[genericToken.type].forEach((childTokens) => {
const tokens = genericToken[childTokens].flat(Infinity) as Token[] | TokensList;
pushValues(values, this.walkTokens(tokens, callback));
if (genericToken[childTokens]) {
const tokens = genericToken[childTokens].flat(Infinity) as Token[] | TokensList;
pushValues(values, this.walkTokens(tokens, callback));
}
});
} else if (genericToken.tokens) {
pushValues(values, this.walkTokens(genericToken.tokens, callback));
+24
View File
@@ -1315,6 +1315,30 @@ br
assert.deepEqual(values, ['dl', 'text', 'strong', 'text']);
});
it('should ignore missing optional childTokens properties on extension tokens', () => {
marked.use({
extensions: [{
name: 'dl',
level: 'block',
childTokens: ['dt', 'dd'],
tokenizer(src) {
const match = /^:(.+)\n?/.exec(src);
if (match) {
return {
type: 'dl',
raw: match[0],
dt: this.lexer.inlineTokens(match[1]),
};
}
},
}],
});
const tokens = marked.lexer(':term');
const values = marked.walkTokens(tokens, (token) => [token.type]);
assert.deepEqual(values, ['dl', 'text']);
});
it('should return values from every walkTokens extension', () => {
marked.use(
{ walkTokens: () => 'one' },