mirror of
https://github.com/markedjs/marked.git
synced 2026-11-30 16:48:05 +01:00
fix: honor per-call async options when composing hooks (#4139)
This commit is contained in:
+2
-2
@@ -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,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: {
|
||||
|
||||
Reference in New Issue
Block a user