aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/packages/docs/app/components/mermaid.tsx
blob: 04d89d9b69ab396bd7fc96e781a44b4dad047d0d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"use client";

import mermaid from "mermaid";
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(() => {
    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 (
    <div className="not-prose my-6">
      <div ref={ref} className="mermaid" />
    </div>
  );
}