markdown-exit icon indicating copy to clipboard operation
markdown-exit copied to clipboard

renderInlineAsync() falls back to sync renderInline, throwing "async rule detected" when a plugin wraps the renderer

Open antfubot opened this issue 3 months ago • 0 comments

Summary

renderInlineAsync() (new in 1.2.0) delegates to a monkey-patched sync renderer.renderInline whenever one is present, even when async inline rules are registered. Any plugin that wraps both renderInline and renderInlineAsync (a natural pattern) therefore routes the async render back through the sync path, which throws:

Error: Renderer.renderInline: async rule detected, use renderInlineAsync()

This looks like the renderInline counterpart of the "honor monkey-patched renderer" fallback added for #33 — but unlike render/renderAsync, the inline variant is unsafe.

Root cause

Renderer.prototype.renderInlineAsync (dist/index.js, ~L3697):

async renderInlineAsync(tokens, options, env) {
  const renderInline = this.renderInline
  if (renderInline !== Renderer.prototype.renderInline)
    return renderInline.call(this, tokens, options, env) // <-- sync fallback
  // ...proper async loop (Promise.all over rules)...
}

When a plugin wraps renderInline, this.renderInline !== Renderer.prototype.renderInline, so renderInlineAsync calls the sync renderInline. If an inline rule is async (Shiki, KaTeX, etc.), the sync renderInline hits its own isPromiseLike guard and throws.

Crucially, a plugin that is async-aware and also wraps renderInlineAsync has already run its preprocessing in its own async wrapper by the time control reaches the prototype method — so delegating to the sync path is both unnecessary and wrong.

Minimal reproduction (markdown-exit only)

import MarkdownExit from 'markdown-exit'

const md = new MarkdownExit({ html: true })

// A plugin that wraps BOTH renderInline and renderInlineAsync, only
// preprocessing tokens then delegating (the @comark/markdown-it pattern).
const wrap = fn => function (tokens, options, env) {
  // real plugins mutate `tokens` here
  return fn.call(this, tokens, options, env)
}
md.renderer.renderInline = wrap(md.renderer.renderInline)
md.renderer.renderInlineAsync = wrap(md.renderer.renderInlineAsync)

// An async inline rule, as Shiki/KaTeX register in real apps
md.renderer.rules.code_inline = async (tokens, i) => {
  await Promise.resolve()
  return `<code>${tokens[i].content}</code>`
}

await md.renderAsync('a `b` c\n')
// THROWS: Renderer.renderInline: async rule detected, use renderInlineAsync()
  • 1.1.0-beta.2: renders fine (no such fallback).
  • 1.2.0: throws.

Real-world impact

This breaks Slidev: decks with comark: true apply @comark/markdown-it (which wraps both inline render methods) on top of async Shiki/KaTeX inline rules, so slidev build fails on every fresh install that resolves markdown-exit to 1.2.0. We've pinned to 1.1.0-beta.2 as a stopgap (slidevjs/slidev#2717) and would love to unpin once this is fixed.

Suggested fix

renderInlineAsync should not blindly delegate to a patched sync renderInline. Options:

  • Run the async loop directly and drop the sync fallback for the inline path (a patched renderInlineAsync, which async-aware plugins wrap, is what gets called anyway); or
  • Only take the fallback when no async inline rule is present.

I verified locally that replacing renderInlineAsync with a plain async loop (Promise.all over the rules, no sync fallback) renders the Slidev + comark + async-rule case correctly.

Environment

  • markdown-exit: 1.2.0 (broken), 1.1.0-beta.2 (works)
  • @comark/markdown-it: 0.3.4
  • Node: reproduced on current LTS

Filed with the help of an agent.

antfubot avatar Aug 24 '26 05:08 antfubot