feat: support mermaid

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei 2023-04-12 23:23:48 +08:00
parent 9f8bb7bfbb
commit 67653b15f8
No known key found for this signature in database
GPG Key ID: 0F62D33977F021F7
5 changed files with 14 additions and 4 deletions

View File

@ -24,7 +24,6 @@ export const PageContent: React.FC<{
}) => {
const $articleRef = useRef<HTMLDivElement>(null)
useCodeCopy()
useMermaid($articleRef)
const inParsedContent = useMemo(() => {
if (parsedContent) {
@ -36,6 +35,7 @@ export const PageContent: React.FC<{
return null
}
}, [content, parsedContent])
useMermaid($articleRef, inParsedContent?.element)
return (
<div

View File

@ -17,3 +17,7 @@ pre {
@apply inline-block w-4 text-right mr-5 -ml-2 text-zinc-400;
content: attr(line);
}
pre.mermaid {
@apply bg-transparent flex justify-center;
}

View File

@ -9,6 +9,7 @@ declare global {
export const useMermaid = (
contentRef: React.MutableRefObject<HTMLElement | null>,
deps: any,
) => {
useEffect(() => {
if (contentRef.current) {
@ -28,5 +29,5 @@ export const useMermaid = (
}
})
}
}, [contentRef])
}, [contentRef, deps])
}

View File

@ -6,7 +6,12 @@ import { visit } from "unist-util-visit"
export const rehypeWrapCode: Plugin<Array<void>, Root> = () => {
return (tree) => {
visit(tree, { type: "element", tagName: "pre" }, (node, index, parent) => {
if (parent && typeof index === "number") {
if (
parent &&
typeof index === "number" &&
// @ts-ignore
node?.properties?.className?.[0] !== "mermaid"
) {
const wrapper = u("element", {
tagName: "div",
properties: {

View File

@ -7,7 +7,7 @@ export const remarkMermaid: Plugin<Array<void>, Root> = () => (tree, file) => {
if (node.type === "code" && node.lang === "mermaid") {
// @ts-ignore
node.type = "html"
node.value = `<div class="mermaid">${node.value}</div>`
node.value = `<pre class="mermaid" data-mermaid='true'>${node.value}</pre>`
}
})
}