-
Notifications
You must be signed in to change notification settings - Fork 12
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
5 changed files
with
919 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
"use client" | ||
|
||
import { MutableRefObject, ReactElement, useEffect, useId, useRef, useState } from "react" | ||
import { MermaidConfig } from "mermaid" | ||
|
||
function useIsVisible(ref: MutableRefObject<HTMLElement>) { | ||
const [isIntersecting, setIsIntersecting] = useState(false) | ||
|
||
useEffect(() => { | ||
const observer = new IntersectionObserver(([entry]) => { | ||
if (entry.isIntersecting) { | ||
// disconnect after once visible to avoid re-rendering of chart when `isIntersecting` will | ||
// be changed to true/false | ||
observer.disconnect() | ||
setIsIntersecting(true) | ||
} | ||
}) | ||
|
||
observer.observe(ref.current) | ||
return () => { | ||
observer.disconnect() | ||
} | ||
}, [ref]) | ||
|
||
return isIntersecting | ||
} | ||
|
||
export function Mermaid({ chart }: { chart: string }): ReactElement { | ||
const id = useId() | ||
const [svg, setSvg] = useState("") | ||
const containerRef = useRef<HTMLDivElement>(null!) | ||
const isVisible = useIsVisible(containerRef) | ||
|
||
useEffect(() => { | ||
// Fix when inside element with `display: hidden` https://github.com/shuding/nextra/issues/3291 | ||
if (!isVisible) { | ||
return | ||
} | ||
const htmlElement = document.documentElement | ||
const observer = new MutationObserver(renderChart) | ||
observer.observe(htmlElement, { attributes: true }) | ||
renderChart() | ||
|
||
return () => { | ||
observer.disconnect() | ||
} | ||
|
||
// Switching themes taken from https://github.com/mermaid-js/mermaid/blob/1b40f552b20df4ab99a986dd58c9d254b3bfd7bc/packages/mermaid/src/docs/.vitepress/theme/Mermaid.vue#L53 | ||
async function renderChart() { | ||
const isDarkTheme = | ||
htmlElement.classList.contains("dark") || | ||
htmlElement.attributes.getNamedItem("data-theme")?.value === "dark" | ||
|
||
const mermaidConfig: MermaidConfig = { | ||
securityLevel: "loose", | ||
fontFamily: "inherit", | ||
themeCSS: "margin: 1.5rem auto 0;", | ||
theme: isDarkTheme ? "dark" : "default", | ||
themeVariables: { | ||
background: isDarkTheme ? "#97eae5" : "#003366", | ||
primaryColor: isDarkTheme ? "#97eae5" : "#003366", | ||
git0: "#97eae5", | ||
git1: "#DC606B", | ||
git2: "#DC9B14" | ||
} | ||
} | ||
|
||
const { default: mermaid } = await import("mermaid") | ||
|
||
try { | ||
mermaid.initialize(mermaidConfig) | ||
const { svg } = await mermaid.render( | ||
// strip invalid characters for `id` attribute | ||
id.replaceAll(":", ""), | ||
chart.replaceAll("\\n", "\n"), | ||
containerRef.current | ||
) | ||
setSvg(svg) | ||
} catch (error) { | ||
console.error("Error while rendering mermaid", error) | ||
} | ||
} | ||
}, [chart, isVisible]) | ||
|
||
return <div ref={containerRef} dangerouslySetInnerHTML={{ __html: svg }} /> | ||
} |
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,53 @@ | ||
import { Code, Root, RootContent } from "mdast" | ||
import { Plugin } from "unified" | ||
import { visit } from "unist-util-visit" | ||
|
||
const COMPONENT_NAME = "Mermaid" | ||
|
||
const MERMAID_IMPORT_AST = { | ||
type: "mdxjsEsm", | ||
data: { | ||
estree: { | ||
body: [ | ||
{ | ||
type: "ImportDeclaration", | ||
specifiers: [ | ||
{ | ||
type: "ImportSpecifier", | ||
imported: { type: "Identifier", name: COMPONENT_NAME }, | ||
local: { type: "Identifier", name: COMPONENT_NAME } | ||
} | ||
], | ||
source: { type: "Literal", value: "@/components/mermaid/Mermaid" } | ||
} | ||
] | ||
} | ||
} | ||
} as RootContent | ||
|
||
export const remarkMermaid: Plugin<[], Root> = () => (ast, _file, done) => { | ||
// eslint-disable-next-line | ||
const codeblocks: any[] = [] | ||
visit(ast, { type: "code", lang: "mermaid" }, (node: Code, index, parent) => { | ||
codeblocks.push([node, index, parent]) | ||
}) | ||
|
||
if (codeblocks.length !== 0) { | ||
for (const [node, index, parent] of codeblocks) { | ||
parent.children.splice(index, 1, { | ||
type: "mdxJsxFlowElement", | ||
name: COMPONENT_NAME, | ||
attributes: [ | ||
{ | ||
type: "mdxJsxAttribute", | ||
name: "chart", | ||
value: node.value.replaceAll("\n", "\\n") | ||
} | ||
] | ||
}) | ||
} | ||
ast.children.unshift(MERMAID_IMPORT_AST) | ||
} | ||
|
||
done() | ||
} |
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
Oops, something went wrong.