fix: clean up Mermaid temporary DOM nodes after render failures

This commit is contained in:
Shirasawa 2026-04-15 01:42:40 +08:00
parent f102060a6d
commit 4e31c53439

View file

@ -1713,13 +1713,29 @@ export const initMermaid = async () => {
return mermaid;
};
export const renderMermaidDiagram = async (mermaid, code: string) => {
const parseResult = await mermaid.parse(code, { suppressErrors: false });
if (parseResult) {
const { svg } = await mermaid.render(`mermaid-${uuidv4()}`, code);
return svg;
const cleanupMermaidTempElements = (id: string) => {
if (typeof document === 'undefined') {
return;
}
document.getElementById(id)?.remove();
document.getElementById(`d${id}`)?.remove();
document.getElementById(`i${id}`)?.remove();
};
export const renderMermaidDiagram = async (mermaid: typeof import('mermaid').default, code: string, renderId?: string) => {
const id = renderId ?? `mermaid-${uuidv4()}`;
try {
const parseResult = await mermaid.parse(code, { suppressErrors: false });
if (parseResult) {
const { svg } = await mermaid.render(id, code);
return svg;
}
return '';
} finally {
// Mermaid can leave temporary d*/i* wrappers on error paths.
cleanupMermaidTempElements(id);
}
return '';
};
export const renderVegaVisualization = async (spec: string, i18n?: any) => {