From 14832b994f2ad504c70f5e101ddfa96018e657d6 Mon Sep 17 00:00:00 2001 From: NtskwK Date: Thu, 26 Mar 2026 10:08:23 +0800 Subject: chore(docs): refactor Mermaid component to render charts asynchronously and improve security --- packages/docs/app/components/mermaid.tsx | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) (limited to 'packages/docs') diff --git a/packages/docs/app/components/mermaid.tsx b/packages/docs/app/components/mermaid.tsx index d408aad..04d89d9 100644 --- a/packages/docs/app/components/mermaid.tsx +++ b/packages/docs/app/components/mermaid.tsx @@ -6,25 +6,36 @@ import { useEffect, useRef } from "react"; mermaid.initialize({ startOnLoad: false, theme: "default", + securityLevel: "strict", }); export function Mermaid({ chart }: { chart: string }) { const ref = useRef(null); useEffect(() => { - if (ref.current) { - ref.current.innerHTML = chart; - mermaid.run({ - nodes: [ref.current], - }); - } + const renderChart = async () => { + if (!ref.current) return; + + try { + const id = `mermaid-${Math.random().toString(36).slice(2, 9)}`; + const { svg } = await mermaid.render(id, chart); + // Use innerHTML with sanitized SVG from mermaid.render + // biome-disable-next-line security/noInnerHtml + ref.current.innerHTML = svg; + } catch { + // Invalid chart definition, render nothing + if (ref.current) { + ref.current.innerHTML = ""; + } + } + }; + + renderChart(); }, [chart]); return (
-
- {chart} -
+
); } -- cgit v1.2.3-70-g09d2