fix: clean up Mermaid temporary DOM nodes after render failures (#23727)

* fix: clean up Mermaid temporary DOM nodes after render failures

* fix: prevent Mermaid temp DOM artifacts from accumulating
This commit is contained in:
Shirasawa
2026-04-17 11:44:10 +09:00
committed by GitHub
parent 3332878321
commit 7b5880ab9e
2 changed files with 28 additions and 6 deletions
+6
View File
@@ -260,6 +260,12 @@ select {
display: none;
}
/* Hide leaked Mermaid temp containers if render cleanup misses */
body > div[id^='dmermaid-'],
body > iframe[id^='imermaid-'] {
display: none !important;
}
.scrollbar-hidden:active::-webkit-scrollbar-thumb,
.scrollbar-hidden:focus::-webkit-scrollbar-thumb,
.scrollbar-hidden:hover::-webkit-scrollbar-thumb {
+22 -6
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) => {