fix doc-site fence cache safety

This commit is contained in:
_Kerman
2026-08-19 14:27:56 +08:00
parent e850c07691
commit f281933dde
4 changed files with 11 additions and 8 deletions
+7 -4
View File
@@ -331,9 +331,9 @@ export default withMermaid({
const renderText = md.renderer.rules.text
const renderCode = md.renderer.rules.code_inline
const renderFence = md.renderer.rules.fence
if (renderText === undefined || renderCode === undefined || renderFence === undefined) {
throw new Error('VitePress Markdown renderer is missing a required rendering rule.')
}
if (renderText === undefined) throw new Error('VitePress Markdown renderer is missing the text rendering rule.')
if (renderCode === undefined) throw new Error('VitePress Markdown renderer is missing the inline-code rendering rule.')
if (renderFence === undefined) throw new Error('VitePress Markdown renderer is missing the fence rendering rule.')
md.renderer.rules.text = (...args) => escapeVueInterpolation(renderText(...args))
md.renderer.rules.code_inline = (...args) => escapeVueInterpolation(renderCode(...args))
const renderedFences = new Map<string, string>()
@@ -341,8 +341,11 @@ export default withMermaid({
const [tokens, index] = args
const token = tokens[index]
if (token === undefined) throw new Error('VitePress code-fence renderer received no token.')
// Mermaid output embeds the token position, so only Shiki fences are reusable.
// Mermaid output embeds the token position, and VitePress snippets resolve source files during rendering.
if (['mermaid', 'mmd'].includes(token.info.trim().split(/\s+/, 1)[0] ?? '')) return renderFence(...args)
if (Reflect.get(token, 'src') !== undefined) return renderFence(...args)
// Keep the cache build-local; a dev renderer can survive many HMR updates.
if (process.env.NODE_ENV !== 'production') return renderFence(...args)
const key = JSON.stringify([token.content, token.info, token.markup, token.attrs])
const cached = renderedFences.get(key)
if (cached !== undefined) return cached