mirror of
https://github.com/markedjs/marked.git
synced 2026-11-30 16:48:05 +01:00
fix: skip missing optional childTokens properties in walkTokens (#4262)
This commit is contained in:
+4
-2
@@ -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));
|
||||
|
||||
@@ -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' },
|
||||
|
||||
Reference in New Issue
Block a user