Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/markdown-exit/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,13 @@ export class Renderer {
*/
async renderInlineAsync(tokens: Token[], options: RenderOptions, env?: any): Promise<string> {
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)
}

Expand Down
29 changes: 29 additions & 0 deletions packages/markdown-exit/tests/renderer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,35 @@ describe('renderAsync', () => {
expect(inline).toBe('<em>hi</em>')
})

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 = <T extends (...args: any[]) => 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 `<code>${tokens[idx].content}</code>`
}

const html = await md.renderAsync('a `b` c\n')
expect(html).toBe('<p>a <code>b</code> c</p>\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)
Expand Down
Loading