feat: flatten childToken arrays (#3172)

This commit is contained in:
Tony Brix
2024-01-26 17:26:33 -07:00
committed by GitHub
parent bf44ae85ed
commit 4826841bd6
2 changed files with 39 additions and 1 deletions
+2 -1
View File
@@ -63,7 +63,8 @@ export class Marked {
const genericToken = token as Tokens.Generic;
if (this.defaults.extensions?.childTokens?.[genericToken.type]) {
this.defaults.extensions.childTokens[genericToken.type].forEach((childTokens) => {
values = values.concat(this.walkTokens(genericToken[childTokens], callback));
const tokens = genericToken[childTokens].flat(Infinity) as Token[] | TokensList;
values = values.concat(this.walkTokens(tokens, callback));
});
} else if (genericToken.tokens) {
values = values.concat(this.walkTokens(genericToken.tokens, callback));
+37
View File
@@ -395,6 +395,43 @@ describe('marked unit', () => {
+ '\n<dt><strong>Topic 2 walked</strong> - unwalked</dt><dd><em>Description 2 walked</em></dd></p>\n');
});
it('should walk child token arrays', () => {
const walkableDescription = {
extensions: [{
name: 'walkableDescription',
level: 'inline',
start(src) { return src.indexOf(':'); },
tokenizer(src, tokens) {
const rule = /^:([^:\n]+):([^:\n]*)(?:\n|$)/;
const match = rule.exec(src);
if (match) {
const token = {
type: 'walkableDescription',
raw: match[0],
dt: [this.lexer.inline(match[1].trim())],
dd: [[this.lexer.inline(match[2].trim())]]
};
return token;
}
},
renderer(token) {
return `\n<dt>${this.parser.parseInline(token.dt[0])}</dt><dd>${this.parser.parseInline(token.dd[0][0])}</dd>`;
},
childTokens: ['dd', 'dt']
}],
walkTokens(token) {
if (token.type === 'text') {
token.text += ' walked';
}
}
};
marked.use(walkableDescription);
const html = marked.parse(': Topic 1 : Description 1\n'
+ ': **Topic 2** : *Description 2*');
assert.strictEqual(html, '<p>\n<dt>Topic 1 walked</dt><dd>Description 1 walked</dd>'
+ '\n<dt><strong>Topic 2 walked</strong></dt><dd><em>Description 2 walked</em></dd></p>\n');
});
describe('multiple extensions', () => {
function createExtension(name) {
return {