From c394b214231508c25843fa37534ced3b9232a2b8 Mon Sep 17 00:00:00 2001 From: Yidadaa Date: Mon, 8 May 2023 22:18:19 +0800 Subject: [PATCH] fix: #1294 fallback while mermaid render fails --- app/components/markdown.tsx | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/app/components/markdown.tsx b/app/components/markdown.tsx index 9e71bb04..cbe730cd 100644 --- a/app/components/markdown.tsx +++ b/app/components/markdown.tsx @@ -12,15 +12,21 @@ import mermaid from "mermaid"; import LoadingIcon from "../icons/three-dots.svg"; import React from "react"; -export function Mermaid(props: { code: string }) { +export function Mermaid(props: { code: string; onError: () => void }) { const ref = useRef(null); useEffect(() => { if (props.code && ref.current) { - mermaid.run({ - nodes: [ref.current], - }); + mermaid + .run({ + nodes: [ref.current], + }) + .catch((e) => { + props.onError(); + console.error("[Mermaid] ", e.message); + }); } + // eslint-disable-next-line react-hooks/exhaustive-deps }, [props.code]); function viewSvgInNewWindow() { @@ -38,7 +44,7 @@ export function Mermaid(props: { code: string }) { return (
viewSvgInNewWindow()} > @@ -60,7 +66,7 @@ export function PreCode(props: { children: any }) { }, [props.children]); if (mermaidCode) { - return ; + return setMermaidCode("")} />; } return (