-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
97 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,92 @@ | ||
"use client" | ||
|
||
import type { Element } from "hast" | ||
import { toText } from "hast-util-to-text" | ||
import { nanoid } from "nanoid" | ||
import { memo, useEffect, useState } from "react" | ||
|
||
import { useIsDark } from "~/hooks/useDarkMode" | ||
|
||
import AdvancedImage from "./AdvancedImage" | ||
|
||
const Mermaid = memo( | ||
function Mermaid(props: { children: string }) { | ||
const [loading, setLoading] = useState(true) | ||
const [error, setError] = useState("") | ||
const [svg, setSvg] = useState("") | ||
const [width, setWidth] = useState<number>() | ||
const [height, setHeight] = useState<number>() | ||
const Mermaid = memo(function Mermaid(props: { | ||
children: string | ||
node: Element | ||
}) { | ||
const [loading, setLoading] = useState(true) | ||
const [error, setError] = useState("") | ||
const [svg, setSvg] = useState("") | ||
const [width, setWidth] = useState<number>() | ||
const [height, setHeight] = useState<number>() | ||
const text = toText(props.node, { | ||
whitespace: "pre", | ||
}) | ||
|
||
const isDark = useIsDark() | ||
const isDark = useIsDark() | ||
|
||
useEffect(() => { | ||
import("mermaid").then(async (mo) => { | ||
const mermaid = mo.default | ||
mermaid.initialize({ | ||
theme: isDark ? "dark" : "default", | ||
}) | ||
useEffect(() => { | ||
import("mermaid").then(async (mo) => { | ||
const mermaid = mo.default | ||
mermaid.initialize({ | ||
theme: isDark ? "dark" : "default", | ||
}) | ||
}, [isDark]) | ||
}) | ||
}, [isDark]) | ||
|
||
useEffect(() => { | ||
if (typeof props.children === "string") { | ||
setError("") | ||
setLoading(true) | ||
useEffect(() => { | ||
if (text) { | ||
setError("") | ||
setLoading(true) | ||
|
||
import("mermaid").then(async (mo) => { | ||
const mermaid = mo.default | ||
const id = nanoid() | ||
let result | ||
try { | ||
result = await mermaid.render(`mermaid-${id}`, props.children) | ||
} catch (error) { | ||
document.getElementById(`dmermaid-${id}`)?.remove() | ||
if (error instanceof Error) { | ||
setError(error.message) | ||
} | ||
setSvg("") | ||
setWidth(undefined) | ||
setHeight(undefined) | ||
import("mermaid").then(async (mo) => { | ||
const mermaid = mo.default | ||
const id = nanoid() | ||
let result | ||
try { | ||
result = await mermaid.render(`mermaid-${id}`, text) | ||
} catch (error) { | ||
document.getElementById(`dmermaid-${id}`)?.remove() | ||
if (error instanceof Error) { | ||
setError(error.message) | ||
} | ||
setSvg("") | ||
setWidth(undefined) | ||
setHeight(undefined) | ||
} | ||
|
||
if (result) { | ||
setSvg(result.svg) | ||
if (result) { | ||
setSvg(result.svg) | ||
|
||
const match = result.svg.match( | ||
/viewBox="[^"]*\s([\d.]+)\s([\d.]+)"/, | ||
) | ||
if (match?.[1] && match?.[2]) { | ||
setWidth(parseInt(match?.[1])) | ||
setHeight(parseInt(match?.[2])) | ||
} | ||
setError("") | ||
const match = result.svg.match(/viewBox="[^"]*\s([\d.]+)\s([\d.]+)"/) | ||
if (match?.[1] && match?.[2]) { | ||
setWidth(parseInt(match?.[1])) | ||
setHeight(parseInt(match?.[2])) | ||
} | ||
setLoading(false) | ||
}) | ||
} | ||
}, [props.children]) | ||
setError("") | ||
} | ||
setLoading(false) | ||
}) | ||
} | ||
}, [text]) | ||
|
||
return loading ? ( | ||
<div className="min-h-[50px] rounded-lg flex items-center justify-center bg-[#ECECFD] dark:bg-[#1F2020] text-sm"> | ||
Mermaid Loading... | ||
</div> | ||
) : svg ? ( | ||
<div> | ||
<AdvancedImage | ||
alt="mermaid" | ||
src={ | ||
"data:image/svg+xml;base64," + Buffer.from(svg).toString("base64") | ||
} | ||
width={width} | ||
height={height} | ||
/> | ||
</div> | ||
) : ( | ||
<div className="min-h-[50px] rounded-lg flex items-center justify-center bg-red-100 text-sm"> | ||
{error || "Error"} | ||
</div> | ||
) | ||
}, | ||
(prevProps, nextProps) => { | ||
return prevProps.children?.[0] === nextProps.children?.[0] | ||
}, | ||
) | ||
return loading ? ( | ||
<div className="min-h-[50px] rounded-lg flex items-center justify-center bg-[#ECECFD] dark:bg-[#1F2020] text-sm"> | ||
Mermaid Loading... | ||
</div> | ||
) : svg ? ( | ||
<div> | ||
<AdvancedImage | ||
alt="mermaid" | ||
src={"data:image/svg+xml;base64," + Buffer.from(svg).toString("base64")} | ||
width={width} | ||
height={height} | ||
/> | ||
</div> | ||
) : ( | ||
<div className="min-h-[50px] rounded-lg flex items-center justify-center bg-red-100 text-sm"> | ||
{error || "Error"} | ||
</div> | ||
) | ||
}) | ||
|
||
export default Mermaid |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import type { Root } from "hast" | ||
import type { Plugin } from "unified" | ||
import { visit } from "unist-util-visit" | ||
|
||
export const rehypeMermaid: Plugin<[], Root> = () => (tree: Root) => { | ||
visit(tree, { tagName: "code" }, (node, i, parent) => { | ||
if ( | ||
Array.isArray(node.properties.className) && | ||
node.properties.className.includes("language-mermaid") && | ||
parent?.type === "element" | ||
) { | ||
parent.tagName = "mermaid" | ||
node.tagName = "div" | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.