From 283f74f24bc6df1c39c4b2a74ef923a68752e6e5 Mon Sep 17 00:00:00 2001 From: Ragib MRB Date: Mon, 23 Nov 2026 12:01:24 +0530 Subject: [PATCH] fix: skip missing optional childTokens properties in walkTokens (#4262) --- src/Instance.ts | 6 ++++-- test/unit/marked.test.js | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Instance.ts b/src/Instance.ts index 95f90ceb..76c0f945 100644 --- a/src/Instance.ts +++ b/src/Instance.ts @@ -87,8 +87,10 @@ export class Marked { 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)); diff --git a/test/unit/marked.test.js b/test/unit/marked.test.js index c64602a9..f92c9a8b 100644 --- a/test/unit/marked.test.js +++ b/test/unit/marked.test.js @@ -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' },