fix: mermaid rendering
This commit is contained in:
@@ -4,18 +4,16 @@ import { nanoid } from "nanoid"
|
||||
import { memo, useEffect, useState } from "react"
|
||||
|
||||
import { useIsDark } from "~/hooks/useDarkMode"
|
||||
import { useIsUnmounted } from "~/hooks/useLifecycle"
|
||||
|
||||
import AdvancedImage from "./AdvancedImage"
|
||||
|
||||
const Mermaid = memo(
|
||||
function Mermaid(props: { children: [string] }) {
|
||||
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 isUnmounted = useIsUnmounted()
|
||||
|
||||
const isDark = useIsDark()
|
||||
|
||||
@@ -29,7 +27,7 @@ const Mermaid = memo(
|
||||
}, [isDark])
|
||||
|
||||
useEffect(() => {
|
||||
if (props.children?.[0]) {
|
||||
if (typeof props.children === "string") {
|
||||
setError("")
|
||||
setLoading(true)
|
||||
|
||||
@@ -38,7 +36,7 @@ const Mermaid = memo(
|
||||
const id = nanoid()
|
||||
let result
|
||||
try {
|
||||
result = await mermaid.render(`mermaid-${id}`, props.children[0])
|
||||
result = await mermaid.render(`mermaid-${id}`, props.children)
|
||||
} catch (error) {
|
||||
document.getElementById(`dmermaid-${id}`)?.remove()
|
||||
if (error instanceof Error) {
|
||||
@@ -49,8 +47,6 @@ const Mermaid = memo(
|
||||
setHeight(undefined)
|
||||
}
|
||||
|
||||
if (isUnmounted()) return
|
||||
|
||||
if (result) {
|
||||
setSvg(result.svg)
|
||||
|
||||
@@ -66,7 +62,7 @@ const Mermaid = memo(
|
||||
setLoading(false)
|
||||
})
|
||||
}
|
||||
}, [props.children, isDark])
|
||||
}, [props.children])
|
||||
|
||||
return loading ? (
|
||||
<div className="min-h-[50px] rounded-lg flex items-center justify-center bg-[#ECECFD] dark:bg-[#1F2020] text-sm">
|
||||
|
||||
@@ -220,8 +220,6 @@ export const renderPageContent = (content: string, strictMode?: boolean) => {
|
||||
ignoreInvalidStyle: true,
|
||||
jsx,
|
||||
jsxs,
|
||||
passKeys: true,
|
||||
passNode: true,
|
||||
}),
|
||||
toMetadata: () => {
|
||||
let metadata = {
|
||||
|
||||
Reference in New Issue
Block a user