diff options
| author | 2026-04-01 10:49:11 +0800 | |
|---|---|---|
| committer | 2026-04-01 10:49:11 +0800 | |
| commit | f75fe4cc916f6935d063cde4216e9f3b309c6047 (patch) | |
| tree | 9dc5e42c4c0b19f34fcf18e8032d82f823e7cd54 /packages/docs/app/components/mermaid.tsx | |
| parent | b6a238f5102049e1f525d6dc13dac8a4c82ca1a3 (diff) | |
| parent | ed0744635367a5bae9e486d796d7ba6ab696289c (diff) | |
| download | DropOut-f75fe4cc916f6935d063cde4216e9f3b309c6047.tar.gz DropOut-f75fe4cc916f6935d063cde4216e9f3b309c6047.zip | |
Merge branch 'main' into chore/change-license-to-agpl
Diffstat (limited to 'packages/docs/app/components/mermaid.tsx')
| -rw-r--r-- | packages/docs/app/components/mermaid.tsx | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/packages/docs/app/components/mermaid.tsx b/packages/docs/app/components/mermaid.tsx index 2df47cc..fe153ec 100644 --- a/packages/docs/app/components/mermaid.tsx +++ b/packages/docs/app/components/mermaid.tsx @@ -6,24 +6,37 @@ import { useEffect, useRef } from "react"; mermaid.initialize({ startOnLoad: false, theme: "default", + securityLevel: "strict", }); export function Mermaid({ chart }: { chart: string }) { const ref = useRef<HTMLDivElement>(null); useEffect(() => { - if (ref.current) { - mermaid.run({ - nodes: [ref.current], + let current = true; + const id = `mermaid-${Math.random().toString(36).slice(2, 9)}`; + mermaid + .render(id, chart) + .then(({ svg }) => { + if (!current) return; + const parser = new DOMParser(); + const doc = parser.parseFromString(svg, "image/svg+xml"); + const svgEl = doc.querySelector("svg"); + if (ref.current && svgEl) { + ref.current.replaceChildren(svgEl); + } + }) + .catch(() => { + if (current) ref.current?.replaceChildren(); }); - } + return () => { + current = false; + }; }, [chart]); return ( <div className="not-prose my-6"> - <div ref={ref} className="mermaid"> - {chart} - </div> + <div ref={ref} className="mermaid" /> </div> ); } |