From 4cc47fbdd31fd15d9ce5acc982111af486a6febc Mon Sep 17 00:00:00 2001 From: "Anthony Fu (via agent)" Date: Mon, 24 Aug 2026 05:59:21 +0000 Subject: [PATCH] fix: renderInlineAsync no longer routes async render through patched sync renderInline (#35) --- packages/markdown-exit/src/renderer.ts | 8 ++++- packages/markdown-exit/tests/renderer.test.ts | 29 +++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/packages/markdown-exit/src/renderer.ts b/packages/markdown-exit/src/renderer.ts index cf00138..5f1b1ab 100644 --- a/packages/markdown-exit/src/renderer.ts +++ b/packages/markdown-exit/src/renderer.ts @@ -388,7 +388,13 @@ export class Renderer { */ async renderInlineAsync(tokens: Token[], options: RenderOptions, env?: any): Promise { const renderInline = this.renderInline - if (renderInline !== Renderer.prototype.renderInline) { + // Honor a patched sync `renderInline` only when `renderInlineAsync` isn't + // also patched. When a plugin wraps both, its async wrapper already ran, so + // the sync path is redundant and throws on async inline rules (#35). + if ( + renderInline !== Renderer.prototype.renderInline + && this.renderInlineAsync === Renderer.prototype.renderInlineAsync + ) { return renderInline.call(this, tokens, options, env) } diff --git a/packages/markdown-exit/tests/renderer.test.ts b/packages/markdown-exit/tests/renderer.test.ts index 24dd95d..5e79f19 100644 --- a/packages/markdown-exit/tests/renderer.test.ts +++ b/packages/markdown-exit/tests/renderer.test.ts @@ -119,6 +119,35 @@ describe('renderAsync', () => { expect(inline).toBe('hi') }) + it('renders async inline rules when a plugin wraps both renderInline and renderInlineAsync', async () => { + // Reproduces https://github.com/serkodev/markdown-exit/issues/35 + // A plugin (e.g. @comark/markdown-it) wraps BOTH inline render methods, + // only preprocessing tokens then delegating. renderInlineAsync must not + // route the async render back through the patched sync renderInline, which + // would throw on async inline rules (Shiki, KaTeX, etc.). + const md = createMarkdownExit({ html: true }) + + const wrapped: string[] = [] + const wrap = any>(fn: T): T => + (function (this: unknown, tokens: unknown, options: unknown, env: unknown) { + wrapped.push(fn.name || 'wrapped') + return fn.call(this, tokens, options, env) + }) as unknown as T + + md.renderer.renderInline = wrap(md.renderer.renderInline) + md.renderer.renderInlineAsync = wrap(md.renderer.renderInlineAsync) + + md.renderer.rules.code_inline = async (tokens, idx) => { + await Promise.resolve() + return `${tokens[idx].content}` + } + + const html = await md.renderAsync('a `b` c\n') + expect(html).toBe('

a b c

\n') + // The async-aware wrapper still runs (its preprocessing is honored). + expect(wrapped.length).toBeGreaterThan(0) + }) + it('fallback supplies an empty env when none is given', async () => { const md = createMarkdownExit() const original = md.renderer.render.bind(md.renderer)