ChatGPT-Next-Web/app/components/markdown.tsx

163 lines
4.0 KiB
TypeScript
Raw Normal View History

import ReactMarkdown from "react-markdown";
import "katex/dist/katex.min.css";
import RemarkMath from "remark-math";
2023-03-30 04:48:38 +00:00
import RemarkBreaks from "remark-breaks";
import RehypeKatex from "rehype-katex";
import RemarkGfm from "remark-gfm";
import RehypeHighlight from "rehype-highlight";
import { useRef, useState, RefObject, useEffect } from "react";
2023-03-26 12:29:02 +00:00
import { copyToClipboard } from "../utils";
2023-05-05 15:32:35 +00:00
import mermaid from "mermaid";
2023-03-26 12:29:02 +00:00
import LoadingIcon from "../icons/three-dots.svg";
import React from "react";
import { useDebouncedCallback, useThrottledCallback } from "use-debounce";
2023-07-05 17:33:30 +00:00
import { showImageModal } from "./ui-lib";
export function Mermaid(props: { code: string }) {
2023-05-05 15:32:35 +00:00
const ref = useRef<HTMLDivElement>(null);
const [hasError, setHasError] = useState(false);
2023-05-05 15:32:35 +00:00
useEffect(() => {
if (props.code && ref.current) {
mermaid
.run({
nodes: [ref.current],
suppressErrors: true,
})
.catch((e) => {
setHasError(true);
console.error("[Mermaid] ", e.message);
});
2023-05-05 15:32:35 +00:00
}
// eslint-disable-next-line react-hooks/exhaustive-deps
2023-05-05 15:32:35 +00:00
}, [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" });
2023-07-05 17:33:30 +00:00
showImageModal(URL.createObjectURL(blob));
2023-05-05 15:32:35 +00:00
}
if (hasError) {
return null;
}
2023-05-05 15:32:35 +00:00
return (
<div
className="no-dark mermaid"
style={{
cursor: "pointer",
overflow: "auto",
}}
2023-05-05 15:32:35 +00:00
ref={ref}
onClick={() => viewSvgInNewWindow()}
>
{props.code}
</div>
);
}
2023-03-26 12:29:02 +00:00
export function PreCode(props: { children: any }) {
const ref = useRef<HTMLPreElement>(null);
const refText = ref.current?.innerText;
2023-05-05 15:32:35 +00:00
const [mermaidCode, setMermaidCode] = useState("");
const renderMermaid = useDebouncedCallback(() => {
2023-05-05 15:32:35 +00:00
if (!ref.current) return;
const mermaidDom = ref.current.querySelector("code.language-mermaid");
if (mermaidDom) {
setMermaidCode((mermaidDom as HTMLElement).innerText);
}
}, 600);
2023-05-05 15:32:35 +00:00
useEffect(() => {
setTimeout(renderMermaid, 1);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [refText]);
2023-03-26 12:29:02 +00:00
return (
<>
{mermaidCode.length > 0 && (
<Mermaid code={mermaidCode} key={mermaidCode} />
)}
<pre ref={ref}>
<span
className="copy-code-button"
onClick={() => {
if (ref.current) {
const code = ref.current.innerText;
copyToClipboard(code);
}
}}
></span>
{props.children}
</pre>
</>
2023-03-26 12:29:02 +00:00
);
}
function _MarkDownContent(props: { content: string }) {
return (
<ReactMarkdown
remarkPlugins={[RemarkMath, RemarkGfm, RemarkBreaks]}
rehypePlugins={[
RehypeKatex,
[
RehypeHighlight,
{
detect: false,
ignoreMissing: true,
},
],
]}
components={{
pre: PreCode,
a: (aProps) => {
const href = aProps.href || "";
const isInternal = /^\/#/i.test(href);
const target = isInternal ? "_self" : aProps.target ?? "_blank";
return <a {...aProps} target={target} />;
},
}}
>
{props.content}
</ReactMarkdown>
);
}
export const MarkdownContent = React.memo(_MarkDownContent);
export function Markdown(
props: {
content: string;
loading?: boolean;
fontSize?: number;
parentRef?: RefObject<HTMLDivElement>;
2023-05-01 18:52:25 +00:00
defaultShow?: boolean;
} & React.DOMAttributes<HTMLDivElement>,
) {
const mdRef = useRef<HTMLDivElement>(null);
return (
<div
className="markdown-body"
style={{
fontSize: `${props.fontSize ?? 14}px`,
}}
ref={mdRef}
onContextMenu={props.onContextMenu}
onDoubleClickCapture={props.onDoubleClickCapture}
2023-08-14 03:10:02 +00:00
dir="auto"
>
{props.loading ? (
<LoadingIcon />
) : (
<MarkdownContent content={props.content} />
)}
</div>
);
}