Skip to content

Commit

Permalink
feat: close ChatGPTNextWeb#951 support mermaid
Browse files Browse the repository at this point in the history
  • Loading branch information
Yidadaa committed May 5, 2023
1 parent fe8e3f2 commit d88da1f
Show file tree
Hide file tree
Showing 3 changed files with 491 additions and 5 deletions.
49 changes: 49 additions & 0 deletions app/components/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,61 @@ import RemarkGfm from "remark-gfm";
import RehypeHighlight from "rehype-highlight";
import { useRef, useState, RefObject, useEffect } from "react";
import { copyToClipboard } from "../utils";
import mermaid from "mermaid";

import LoadingIcon from "../icons/three-dots.svg";
import React from "react";

export function Mermaid(props: { code: string }) {
const ref = useRef<HTMLDivElement>(null);

useEffect(() => {
if (props.code && ref.current) {
mermaid.run({
nodes: [ref.current],
});
}
}, [props.code]);

function viewSvgInNewWindow() {
const svg = ref.current?.querySelector("svg");
if (!svg) return;
const text = new XMLSerializer().serializeToString(svg);
const blob = new Blob([text], { type: "image/svg+xml" });
const url = URL.createObjectURL(blob);
const win = window.open(url);
if (win) {
win.onload = () => URL.revokeObjectURL(url);
}
}

return (
<div
className="no-dark"
style={{ cursor: "pointer" }}
ref={ref}
onClick={() => viewSvgInNewWindow()}
>
{props.code}
</div>
);
}

export function PreCode(props: { children: any }) {
const ref = useRef<HTMLPreElement>(null);
const [mermaidCode, setMermaidCode] = useState("");

useEffect(() => {
if (!ref.current) return;
const mermaidDom = ref.current.querySelector("code.language-mermaid");
if (mermaidDom) {
setMermaidCode((mermaidDom as HTMLElement).innerText);
}
}, [props.children]);

if (mermaidCode) {
return <Mermaid code={mermaidCode} />;
}

return (
<pre ref={ref}>
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"emoji-picker-react": "^4.4.7",
"eventsource-parser": "^0.1.0",
"fuse.js": "^6.6.2",
"mermaid": "^10.1.0",
"next": "^13.3.1-canary.8",
"node-fetch": "^3.3.1",
"openai": "^3.2.1",
Expand Down
Loading

0 comments on commit d88da1f

Please sign in to comment.