feat: mermaid unique id and error handling
This commit is contained in:
@@ -2,7 +2,7 @@ import { cn } from "~/lib/utils"
|
||||
import { useCodeCopy } from "~/hooks/useCodeCopy"
|
||||
import { renderPageContent } from "~/markdown"
|
||||
import { PostToc } from "~/components/site/PostToc"
|
||||
import { MutableRefObject, useMemo, useRef } from "react"
|
||||
import { MutableRefObject, useMemo } from "react"
|
||||
|
||||
export const PageContent: React.FC<{
|
||||
content?: string
|
||||
@@ -21,7 +21,6 @@ export const PageContent: React.FC<{
|
||||
onMouseEnter,
|
||||
parsedContent,
|
||||
}) => {
|
||||
const $articleRef = useRef<HTMLDivElement>(null)
|
||||
useCodeCopy()
|
||||
|
||||
const inParsedContent = useMemo(() => {
|
||||
@@ -40,7 +39,6 @@ export const PageContent: React.FC<{
|
||||
className={cn("relative", className)}
|
||||
onMouseEnter={onMouseEnter}
|
||||
onScroll={(e) => onScroll?.((e.target as any)?.scrollTop)}
|
||||
ref={$articleRef}
|
||||
>
|
||||
<div className="xlog-post-content prose" ref={inputRef}>
|
||||
{inParsedContent?.element}
|
||||
|
||||
@@ -2,11 +2,13 @@ import { FC, useEffect, useState } from "react"
|
||||
import { useTranslation } from "react-i18next"
|
||||
import { useMediaStore } from "~/hooks/useDarkMode"
|
||||
import { useIsUnmounted } from "~/hooks/useLifecycle"
|
||||
import { nanoid } from "nanoid"
|
||||
|
||||
export const Mermaid: FC<{
|
||||
children: [string]
|
||||
}> = (props) => {
|
||||
const [loading, setLoading] = useState(true)
|
||||
const [error, setError] = useState("")
|
||||
const [svg, setSvg] = useState("")
|
||||
const isUnmounted = useIsUnmounted()
|
||||
|
||||
@@ -22,14 +24,32 @@ export const Mermaid: FC<{
|
||||
}, [isDark])
|
||||
|
||||
useEffect(() => {
|
||||
import("mermaid").then(async (mo) => {
|
||||
const mermaid = mo.default
|
||||
const result = await mermaid.render("mermaid", props.children[0])
|
||||
if (props.children?.[0]) {
|
||||
setError("")
|
||||
setLoading(true)
|
||||
|
||||
if (isUnmounted()) return
|
||||
setSvg(result.svg)
|
||||
setLoading(false)
|
||||
})
|
||||
import("mermaid").then(async (mo) => {
|
||||
const mermaid = mo.default
|
||||
const id = nanoid()
|
||||
let result
|
||||
try {
|
||||
result = await mermaid.render(`mermaid-${id}`, props.children[0])
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
setError(error.message)
|
||||
}
|
||||
setSvg("")
|
||||
}
|
||||
|
||||
if (isUnmounted()) return
|
||||
|
||||
if (result) {
|
||||
setSvg(result.svg)
|
||||
setError("")
|
||||
}
|
||||
setLoading(false)
|
||||
})
|
||||
}
|
||||
}, [props.children])
|
||||
|
||||
const { t } = useTranslation("common")
|
||||
@@ -38,7 +58,11 @@ export const Mermaid: FC<{
|
||||
<div className="h-[50px] rounded-lg flex items-center justify-center bg-[#ECECFD] dark:bg-[#1F2020] text-sm">
|
||||
Mermaid {t("Loading")}...
|
||||
</div>
|
||||
) : (
|
||||
) : svg ? (
|
||||
<div dangerouslySetInnerHTML={{ __html: svg }} />
|
||||
) : (
|
||||
<div className="h-[50px] rounded-lg flex items-center justify-center bg-red-100 text-sm">
|
||||
{error || "Error"}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user