Skip to content

Commit 4826841

Browse files
authoredJan 27, 2024
feat: flatten childToken arrays (#3172)
1 parent bf44ae8 commit 4826841

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed
 

‎src/Instance.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ export class Marked {
6363
const genericToken = token as Tokens.Generic;
6464
if (this.defaults.extensions?.childTokens?.[genericToken.type]) {
6565
this.defaults.extensions.childTokens[genericToken.type].forEach((childTokens) => {
66-
values = values.concat(this.walkTokens(genericToken[childTokens], callback));
66+
const tokens = genericToken[childTokens].flat(Infinity) as Token[] | TokensList;
67+
values = values.concat(this.walkTokens(tokens, callback));
6768
});
6869
} else if (genericToken.tokens) {
6970
values = values.concat(this.walkTokens(genericToken.tokens, callback));

‎test/unit/marked.test.js

+37
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,43 @@ describe('marked unit', () => {
395395
+ '\n<dt><strong>Topic 2 walked</strong> - unwalked</dt><dd><em>Description 2 walked</em></dd></p>\n');
396396
});
397397

398+
it('should walk child token arrays', () => {
399+
const walkableDescription = {
400+
extensions: [{
401+
name: 'walkableDescription',
402+
level: 'inline',
403+
start(src) { return src.indexOf(':'); },
404+
tokenizer(src, tokens) {
405+
const rule = /^:([^:\n]+):([^:\n]*)(?:\n|$)/;
406+
const match = rule.exec(src);
407+
if (match) {
408+
const token = {
409+
type: 'walkableDescription',
410+
raw: match[0],
411+
dt: [this.lexer.inline(match[1].trim())],
412+
dd: [[this.lexer.inline(match[2].trim())]]
413+
};
414+
return token;
415+
}
416+
},
417+
renderer(token) {
418+
return `\n<dt>${this.parser.parseInline(token.dt[0])}</dt><dd>${this.parser.parseInline(token.dd[0][0])}</dd>`;
419+
},
420+
childTokens: ['dd', 'dt']
421+
}],
422+
walkTokens(token) {
423+
if (token.type === 'text') {
424+
token.text += ' walked';
425+
}
426+
}
427+
};
428+
marked.use(walkableDescription);
429+
const html = marked.parse(': Topic 1 : Description 1\n'
430+
+ ': **Topic 2** : *Description 2*');
431+
assert.strictEqual(html, '<p>\n<dt>Topic 1 walked</dt><dd>Description 1 walked</dd>'
432+
+ '\n<dt><strong>Topic 2 walked</strong></dt><dd><em>Description 2 walked</em></dd></p>\n');
433+
});
434+
398435
describe('multiple extensions', () => {
399436
function createExtension(name) {
400437
return {

1 commit comments

Comments
 (1)

vercel[bot] commented on Jan 27, 2024

@vercel[bot]
Please sign in to comment.