Skip to content

Commit

Permalink
feat: flatten childToken arrays (#3172)
Browse files Browse the repository at this point in the history
  • Loading branch information
UziTech committed Jan 27, 2024
1 parent bf44ae8 commit 4826841
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Instance.ts
Expand Up @@ -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));
Expand Down
37 changes: 37 additions & 0 deletions test/unit/marked.test.js
Expand Up @@ -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 {
Expand Down

1 comment on commit 4826841

@vercel
Copy link

@vercel vercel bot commented on 4826841 Jan 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.