mirror of
https://github.com/markedjs/marked.git
synced 2026-11-30 16:48:05 +01:00
fix: await promise arrays from walkTokens extensions (#4153)
* fix: await promise arrays from walkTokens extensions * fix: declare promise arrays for walkTokens extensions
This commit is contained in:
+1
-1
@@ -280,7 +280,7 @@ export class Marked<ParserOutput = string, RendererOutput = string> {
|
||||
const packWalktokens = pack.walkTokens;
|
||||
opts.walkTokens = function(token) {
|
||||
const values: MaybePromise[] = [];
|
||||
values.push(packWalktokens.call(this, token));
|
||||
pushValues(values, packWalktokens.call(this, token));
|
||||
if (walkTokens) {
|
||||
pushValues(values, walkTokens.call(this, token));
|
||||
}
|
||||
|
||||
@@ -109,9 +109,9 @@ export interface MarkedExtension<ParserOutput = string, RendererOutput = string>
|
||||
* The walkTokens function gets called with every token.
|
||||
* Child tokens are called before moving on to sibling tokens.
|
||||
* Each token is passed by reference so updates are persisted when passed to the parser.
|
||||
* The return value of the function is ignored.
|
||||
* When async is true, returned promises, including arrays of promises, are awaited before parsing.
|
||||
*/
|
||||
walkTokens?: ((token: Token) => void | Promise<void>) | null;
|
||||
walkTokens?: ((token: Token) => void | Promise<void> | (void | Promise<void>)[]) | null;
|
||||
}
|
||||
|
||||
export interface MarkedOptions<ParserOutput = string, RendererOutput = string> extends Omit<MarkedExtension<ParserOutput, RendererOutput>, 'hooks' | 'renderer' | 'tokenizer' | 'extensions' | 'walkTokens'> {
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
import { marked, Marked } from 'marked';
|
||||
import type { MarkedExtension } from 'marked';
|
||||
|
||||
// Compile against the public declarations, including contextual token typing.
|
||||
const extension: MarkedExtension = {
|
||||
async: true,
|
||||
walkTokens(token) {
|
||||
return [
|
||||
Promise.resolve().then(() => {
|
||||
if (token.type === 'text') {
|
||||
token.text = token.text.toUpperCase();
|
||||
}
|
||||
}),
|
||||
undefined,
|
||||
];
|
||||
},
|
||||
};
|
||||
|
||||
marked.use(extension);
|
||||
const parser = new Marked(extension);
|
||||
parser.use({
|
||||
async: true,
|
||||
walkTokens(token) {
|
||||
return [Promise.resolve().then(() => {
|
||||
token.raw = token.raw.trim();
|
||||
})];
|
||||
},
|
||||
});
|
||||
|
||||
marked.use({ walkTokens: () => [] });
|
||||
marked.use({ walkTokens: () => undefined });
|
||||
marked.use({ walkTokens: () => Promise.resolve() });
|
||||
|
||||
const block: Promise<string> = marked.parse('text', { async: true });
|
||||
const inline: Promise<string> = parser.parseInline('text', { async: true });
|
||||
await Promise.all([block, inline]);
|
||||
@@ -1184,6 +1184,36 @@ br
|
||||
assert.strictEqual(html, new Marked().parse('| A | B |\n|---|---|\n| C | D |\n\n- E\n - *F*\n\n> G\n'));
|
||||
});
|
||||
|
||||
it('should flatten arrays returned by composed walkTokens extensions', () => {
|
||||
const first = Promise.resolve();
|
||||
const second = Promise.resolve();
|
||||
marked.use(
|
||||
{ walkTokens: () => [first] },
|
||||
{ walkTokens: () => [second] },
|
||||
);
|
||||
|
||||
assert.deepEqual(
|
||||
marked.walkTokens([{ type: 'text', raw: 'text', text: 'text' }], marked.defaults.walkTokens),
|
||||
[second, first],
|
||||
);
|
||||
});
|
||||
|
||||
it('should await promise arrays returned by walkTokens extensions', async() => {
|
||||
marked.use({
|
||||
async: true,
|
||||
walkTokens(token) {
|
||||
if (token.type === 'text') {
|
||||
return [timeout().then(() => {
|
||||
token.text = token.text.toUpperCase();
|
||||
})];
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
assert.strictEqual(await marked.parse('before'), '<p>BEFORE</p>\n');
|
||||
assert.strictEqual(await marked.parseInline('before'), 'BEFORE');
|
||||
});
|
||||
|
||||
it('should copy returned arrays by index like concat', async() => {
|
||||
const tokens = marked.lexer('before');
|
||||
const pending = timeout().then(() => {
|
||||
|
||||
Reference in New Issue
Block a user