diff --git a/src/lib/components/chat/Messages/CodeBlock.svelte b/src/lib/components/chat/Messages/CodeBlock.svelte index f368c8459..d8cf40c7b 100644 --- a/src/lib/components/chat/Messages/CodeBlock.svelte +++ b/src/lib/components/chat/Messages/CodeBlock.svelte @@ -213,14 +213,20 @@ __builtins__.input = input`); let debounceTimeout; + const drawMermaidDiagram = async () => { + try { + const { svg } = await mermaid.render(`mermaid-${id}`, code); + mermaidHtml = svg; + } catch (error) { + console.error('Error:', error); + } + }; + $: if (code) { if (lang === 'mermaid' && (token?.raw ?? '').endsWith('```')) { (async () => { - try { - const { svg } = await mermaid.render(`mermaid-${id}`, code); - mermaidHtml = svg; - } catch (error) { - console.error('Error:', error); + if (!mermaidHtml) { + await drawMermaidDiagram(); } })(); } else { @@ -235,19 +241,6 @@ __builtins__.input = input`); debounceTimeout = setTimeout(highlightCode, 10); } } - - onMount(async () => { - await mermaid.initialize({ startOnLoad: true }); - - if (lang === 'mermaid' && (token?.raw ?? '').endsWith('```')) { - try { - const { svg } = await mermaid.render(`mermaid-${id}`, code); - mermaidHtml = svg; - } catch (error) { - console.error('Error:', error); - } - } - });
{code}+
{code}{/if} {:else}