Skip to content

Commit 746c5a3

Browse files
author
tkokhing
committed
further test Mermaid rendering
1 parent 3bcc802 commit 746c5a3

2 files changed

Lines changed: 34 additions & 143 deletions

File tree

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,51 @@
1-
// render-mermaid.tsx
21
"use client";
32

3+
import { useEffect, useRef, useState } from "react";
44
import mermaid from "mermaid";
5-
import { useEffect, useRef } from "react";
65

7-
let mermaidInitialized = false;
6+
type Props = {
7+
chart: string;
8+
};
89

9-
export default function Mermaid({ chart }: { chart: string }) {
10+
export default function RenderMermaid({ chart }: Props) {
1011
const ref = useRef<HTMLDivElement>(null);
12+
const [svg, setSvg] = useState<string>("");
1113

1214
useEffect(() => {
13-
if (!mermaidInitialized) {
14-
mermaid.initialize({
15-
startOnLoad: false,
16-
theme: "default",
17-
securityLevel: "loose",
18-
});
19-
mermaidInitialized = true;
20-
}
21-
22-
const renderChart = async () => {
23-
if (!ref.current) return;
24-
25-
const id = "mermaid-" + Math.random().toString(36).slice(2);
15+
if (!ref.current) return;
2616

27-
const { svg } = await mermaid.render(id, chart);
28-
29-
// Inject white background rectangle into SVG
30-
const svgWithBg = svg.replace(
31-
"<svg",
32-
`<svg style="background:white"`
33-
);
17+
if (!chart || typeof chart !== "string") {
18+
console.error("Invalid Mermaid chart:", chart);
19+
return;
20+
}
3421

35-
ref.current.innerHTML = svgWithBg;
22+
mermaid.initialize({
23+
startOnLoad: false,
24+
theme: "default",
25+
securityLevel: "loose",
26+
});
27+
28+
const renderDiagram = async () => {
29+
try {
30+
const { svg } = await mermaid.render(
31+
`mermaid-${Date.now()}`,
32+
chart.trim()
33+
);
34+
setSvg(svg);
35+
} catch (err) {
36+
console.error("Mermaid render error:", err, chart);
37+
}
3638
};
3739

38-
renderChart();
40+
renderDiagram();
3941
}, [chart]);
4042

4143
return (
4244
<div className="not-prose flex-auto justify-center my-8 mx-5">
43-
<div ref={ref} className="max-w-full overflow-x-auto" />
45+
<div ref={ref}
46+
className="max-w-full overflow-x-auto"
47+
dangerouslySetInnerHTML={{ __html: svg }}
48+
/>
4449
</div>
4550
);
4651
}

src/app/logic/page.tsx

Lines changed: 2 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,17 @@
11
import Container from "@/app/_components/preference/container";
2-
import { loadPost } from "@/lib/share/api";
3-
import { PostBody } from "@/app/_components/post_gen/post-body";
4-
5-
import Mermaid from "@/app/_components/post_gen/render-mermaid";
6-
72
// import MyTest from "@/app/_components/mytest";
83
// import MyTest2 from "@/app/_components/mytest2";
94

105
export default function Index() {
116

12-
const ImportComponents = {
13-
Mermaid,
14-
};
15-
16-
const diagram = `
17-
flowchart LR
18-
A[User] --> B[Gateway]
19-
B --> C[Application]
20-
C --> D[(Database)]
21-
`;
22-
23-
const dfdiagram = `
24-
%%{init: {
25-
"theme": "default",
26-
"themeVariables": {
27-
"background": "#ffffff",
28-
"primaryTextColor": "#000000",
29-
"primaryBorderColor": "#444444",
30-
"lineColor": "#444444",
31-
"fontFamily": "Arial, Helvetica, sans-serif"
32-
}
33-
}}%%
34-
35-
flowchart TB
36-
37-
subgraph Internet
38-
U[User]
39-
end
40-
41-
subgraph Cloud_Environment
42-
CA[Cloud Application]
43-
CD[(Cloud Database)]
44-
end
45-
46-
subgraph Hybrid_Connectivity
47-
HC[VPN / Direct Connect]
48-
end
49-
50-
subgraph Secure_Intermediary
51-
HG[Hybrid Gateway]
52-
end
53-
54-
subgraph On_Premises
55-
OP[On-Premises Application]
56-
OD[(On-Premises Database)]
57-
end
58-
59-
U -->|1 Login Request| CA
60-
CA -->|2 Read / Write| CD
61-
CA -->|3 API Request| HC
62-
HC -->|4 Secure Gateway| HG
63-
HG -->|5 Internal Call| OP
64-
OP -->|6 Query| OD
65-
66-
%% focus node
67-
classDef focus fill:#ffffff,stroke:#ff0000,stroke-width:3px,color:#000000;
68-
69-
%% dim nodes
70-
classDef dim fill:#f5f5f5,stroke:#cccccc,color:#999999;
71-
72-
class HG focus;
73-
class U,CA dim;
74-
75-
%% trust zone styling
76-
style Internet fill:#ffffff,stroke:#999999,stroke-dasharray: 5 5
77-
style Cloud_Environment fill:#eef5ff,stroke:#5b8bd9
78-
style Hybrid_Connectivity fill:#fff7e6,stroke:#d69e2e
79-
style Secure_Intermediary fill:#ffeaea,stroke:#ff0000
80-
style On_Premises fill:#f0fff4,stroke:#38a169
81-
`;
82-
83-
const readmermaid = loadPost("mermaid.md","_blog_post/_blogs")
84-
// const readmermaidmdx = loadPost("test_mermaid.mdx","_blog_post/_blogs")
857
return (
868
<main>
879
<Container>
8810
<h1> This page is for logic testing only...</h1>
89-
<h1> - - - - </h1>
90-
<h2> Mermaid Server-side WORK!...</h2>
91-
<p> - PostBody content=readmermaid </p>
92-
93-
<PostBody content={readmermaid} components={ImportComponents}/>
94-
<br />
95-
<p> - PostBody content=readmermaidmdx </p>
96-
{/* <PostBody content={readmermaidmdx} components={ImportComponents}/> */}
97-
98-
<h2> Mermaid `const diagram` script directly at <code>page.tsx</code> WORK and font is NOT gray out!...</h2>
99-
<br />
100-
<h3>diagram</h3>
101-
<Mermaid chart={diagram} />
11+
{/* <MyTest />
12+
<MyTest2 /> */}
10213

103-
<h3>dfdiagram</h3>
104-
<Mermaid chart={dfdiagram} />
10514
</Container>
10615
</main>
10716
);
10817
}
109-
110-
111-
//
112-
//
113-
//
114-
115-
// import Container from "@/app/_components/preference/container";
116-
// // import MyTest from "@/app/_components/mytest";
117-
// // import MyTest2 from "@/app/_components/mytest2";
118-
119-
// export default function Index() {
120-
121-
// return (
122-
// <main>
123-
// <Container>
124-
// <h1> This page is for logic testing only...</h1>
125-
// {/* <MyTest />
126-
// <MyTest2 /> */}
127-
128-
// </Container>
129-
// </main>
130-
// );
131-
// }

0 commit comments

Comments
 (0)