diff --git a/packages/console/src/mdx-components/Mermaid/index.tsx b/packages/console/src/mdx-components/Mermaid/index.tsx index e7f7180cb..1cc8c0ed5 100644 --- a/packages/console/src/mdx-components/Mermaid/index.tsx +++ b/packages/console/src/mdx-components/Mermaid/index.tsx @@ -1,15 +1,19 @@ import { Theme } from '@logto/schemas'; -import mermaid from 'mermaid'; +import { type Mermaid as MermaidType } from 'mermaid'; import { useEffect } from 'react'; import useTheme from '@/hooks/use-theme'; -mermaid.initialize({ - startOnLoad: true, - theme: 'default', - securityLevel: 'loose', - fontFamily: 'Fira Code', -}); +const loadMermaid = async () => { + // Define this variable to "outsmart" the detection of the dynamic import by Parcel: + // https://github.com/parcel-bundler/parcel/issues/7064#issuecomment-942441649 + const uri = 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs'; + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + const imported: { default: MermaidType } = await import(uri); + return imported.default; +}; + +const mermaidPromise = loadMermaid(); type Props = { readonly children: string; @@ -24,14 +28,16 @@ export default function Mermaid({ children }: Props) { const theme = useTheme(); useEffect(() => { - mermaid.initialize({ - theme: themeToMermaidTheme[theme], - }); + (async () => { + const mermaid = await mermaidPromise; + mermaid.initialize({ + startOnLoad: false, + theme: themeToMermaidTheme[theme], + securityLevel: 'loose', + }); + await mermaid.run(); + })(); }, [theme]); - useEffect(() => { - mermaid.contentLoaded(); - }, []); - return