fix: mermaid rendering issue

This commit is contained in:
Timothy J. Baek 2024-08-19 17:28:38 +02:00
parent 1329eea5e5
commit b31de299e4
2 changed files with 12 additions and 20 deletions

View File

@ -213,14 +213,20 @@ __builtins__.input = input`);
let debounceTimeout; 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 (code) {
if (lang === 'mermaid' && (token?.raw ?? '').endsWith('```')) { if (lang === 'mermaid' && (token?.raw ?? '').endsWith('```')) {
(async () => { (async () => {
try { if (!mermaidHtml) {
const { svg } = await mermaid.render(`mermaid-${id}`, code); await drawMermaidDiagram();
mermaidHtml = svg;
} catch (error) {
console.error('Error:', error);
} }
})(); })();
} else { } else {
@ -235,19 +241,6 @@ __builtins__.input = input`);
debounceTimeout = setTimeout(highlightCode, 10); 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);
}
}
});
</script> </script>
<div class="my-2" dir="ltr"> <div class="my-2" dir="ltr">
@ -255,7 +248,7 @@ __builtins__.input = input`);
{#if mermaidHtml} {#if mermaidHtml}
{@html mermaidHtml} {@html mermaidHtml}
{:else} {:else}
<pre class=" mermaid-{id}">{code}</pre> <pre class="mermaid">{code}</pre>
{/if} {/if}
{:else} {:else}
<div <div

View File

@ -180,7 +180,6 @@
await tick(); await tick();
} }
await mermaid.initialize({ startOnLoad: false });
loaded = true; loaded = true;
}); });
</script> </script>