mirror of
https://github.com/open-webui/open-webui.git
synced 2026-09-28 01:31:28 +00:00
fix: clean up Mermaid temporary DOM nodes after render failures
This commit is contained in:
parent
f102060a6d
commit
4e31c53439
1 changed files with 22 additions and 6 deletions
|
|
@ -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) => {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue