diff --git a/app/components/markdown.tsx b/app/components/markdown.tsx index af7b484a..fbde6453 100644 --- a/app/components/markdown.tsx +++ b/app/components/markdown.tsx @@ -11,19 +11,21 @@ import mermaid from "mermaid"; import LoadingIcon from "../icons/three-dots.svg"; import React from "react"; -import { useThrottledCallback } from "use-debounce"; +import { useDebouncedCallback, useThrottledCallback } from "use-debounce"; -export function Mermaid(props: { code: string; onError: () => void }) { +export function Mermaid(props: { code: string }) { const ref = useRef(null); + const [hasError, setHasError] = useState(false); useEffect(() => { if (props.code && ref.current) { mermaid .run({ nodes: [ref.current], + suppressErrors: true, }) .catch((e) => { - props.onError(); + setHasError(true); console.error("[Mermaid] ", e.message); }); } @@ -42,10 +44,17 @@ export function Mermaid(props: { code: string; onError: () => void }) { } } + if (hasError) { + return null; + } + return (
viewSvgInNewWindow()} > @@ -56,33 +65,40 @@ export function Mermaid(props: { code: string; onError: () => void }) { export function PreCode(props: { children: any }) { const ref = useRef(null); + const refText = ref.current?.innerText; const [mermaidCode, setMermaidCode] = useState(""); - useEffect(() => { + const renderMermaid = useDebouncedCallback(() => { if (!ref.current) return; const mermaidDom = ref.current.querySelector("code.language-mermaid"); if (mermaidDom) { setMermaidCode((mermaidDom as HTMLElement).innerText); } - }, [props.children]); + }, 600); - if (mermaidCode) { - return setMermaidCode("")} />; - } + useEffect(() => { + setTimeout(renderMermaid, 1); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [refText]); return ( -
-       {
-          if (ref.current) {
-            const code = ref.current.innerText;
-            copyToClipboard(code);
-          }
-        }}
-      >
-      {props.children}
-    
+ <> + {mermaidCode.length > 0 && ( + + )} +
+         {
+            if (ref.current) {
+              const code = ref.current.innerText;
+              copyToClipboard(code);
+            }
+          }}
+        >
+        {props.children}
+      
+ ); } diff --git a/app/styles/markdown.scss b/app/styles/markdown.scss index 0a6b3bc5..31d10c4c 100644 --- a/app/styles/markdown.scss +++ b/app/styles/markdown.scss @@ -1117,3 +1117,15 @@ .markdown-body ::-webkit-calendar-picker-indicator { filter: invert(50%); } + +.markdown-body .mermaid { + border: var(--border-in-light); + margin-bottom: 10px; + border-radius: 4px; + padding: 10px; + background-color: var(--white); +} + +#dmermaid { + display: none; +}