fix: honor per-call async options when composing hooks (#4139)

This commit is contained in:
Huanyi Xie
2026-10-25 23:38:05 -06:00
committed by GitHub
parent 38c7e55207
commit 379be9d656
2 changed files with 23 additions and 2 deletions
+2 -2
View File
@@ -240,7 +240,7 @@ export class Marked<ParserOutput = string, RendererOutput = string> {
if (_Hooks.passThroughHooks.has(prop)) {
// @ts-expect-error cannot type hook function dynamically
hooks[hooksProp] = (arg: unknown) => {
if (this.defaults.async && _Hooks.passThroughHooksRespectAsync.has(prop)) {
if (hooks.options.async && _Hooks.passThroughHooksRespectAsync.has(prop)) {
return (async() => {
const ret = await hooksFunc.call(hooks, arg);
return prevHook.call(hooks, ret);
@@ -253,7 +253,7 @@ export class Marked<ParserOutput = string, RendererOutput = string> {
} else {
// @ts-expect-error cannot type hook function dynamically
hooks[hooksProp] = (...args: unknown[]) => {
if (this.defaults.async) {
if (hooks.options.async) {
return (async() => {
let ret = await hooksFunc.apply(hooks, args);
if (ret === false) {
+21
View File
@@ -21,6 +21,27 @@ describe('Hooks', () => {
marked = new Marked();
});
it('awaits chained preprocess hooks with a per-call async option', async() => {
marked.use({ hooks: { preprocess: markdown => markdown.toUpperCase() } });
marked.use({ hooks: { async preprocess(markdown) { return markdown + ' world'; } } });
const html = await marked.parse('hello', { async: true });
assert.strictEqual(html.trim(), '<p>HELLO WORLD</p>');
assert.strictEqual(marked.defaults.async, false);
});
it('awaits chained postprocess hooks with a per-call async option', async() => {
marked.use({ hooks: { postprocess: html => html.trim() } });
marked.use({ hooks: { async postprocess(html) { return html + ' '; } } });
const html = await marked.parse('hello', { async: true });
assert.strictEqual(html, '<p>hello</p>');
});
it('uses an asynchronous provider fallback with a per-call async option', async() => {
marked.use({ hooks: { async provideParser() { return false; } } });
const html = await marked.parse('hello', { async: true });
assert.strictEqual(html.trim(), '<p>hello</p>');
});
it('should preprocess markdown', () => {
marked.use({
hooks: {