forked from XiaoMo/ChatGPT-Next-Web
fix: #1294 fallback while mermaid render fails
This commit is contained in:
parent
696e84ea88
commit
c394b21423
@ -12,15 +12,21 @@ import mermaid from "mermaid";
|
|||||||
import LoadingIcon from "../icons/three-dots.svg";
|
import LoadingIcon from "../icons/three-dots.svg";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
|
|
||||||
export function Mermaid(props: { code: string }) {
|
export function Mermaid(props: { code: string; onError: () => void }) {
|
||||||
const ref = useRef<HTMLDivElement>(null);
|
const ref = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (props.code && ref.current) {
|
if (props.code && ref.current) {
|
||||||
mermaid.run({
|
mermaid
|
||||||
nodes: [ref.current],
|
.run({
|
||||||
});
|
nodes: [ref.current],
|
||||||
|
})
|
||||||
|
.catch((e) => {
|
||||||
|
props.onError();
|
||||||
|
console.error("[Mermaid] ", e.message);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||||
}, [props.code]);
|
}, [props.code]);
|
||||||
|
|
||||||
function viewSvgInNewWindow() {
|
function viewSvgInNewWindow() {
|
||||||
@ -38,7 +44,7 @@ export function Mermaid(props: { code: string }) {
|
|||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className="no-dark"
|
className="no-dark"
|
||||||
style={{ cursor: "pointer" }}
|
style={{ cursor: "pointer", overflow: "auto" }}
|
||||||
ref={ref}
|
ref={ref}
|
||||||
onClick={() => viewSvgInNewWindow()}
|
onClick={() => viewSvgInNewWindow()}
|
||||||
>
|
>
|
||||||
@ -60,7 +66,7 @@ export function PreCode(props: { children: any }) {
|
|||||||
}, [props.children]);
|
}, [props.children]);
|
||||||
|
|
||||||
if (mermaidCode) {
|
if (mermaidCode) {
|
||||||
return <Mermaid code={mermaidCode} />;
|
return <Mermaid code={mermaidCode} onError={() => setMermaidCode("")} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
Loading…
Reference in New Issue
Block a user