2023-03-20 06:06:29 +00:00
|
|
|
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";
|
2023-03-20 06:06:29 +00:00
|
|
|
import RehypeKatex from "rehype-katex";
|
2023-03-23 11:28:07 +00:00
|
|
|
import RemarkGfm from "remark-gfm";
|
|
|
|
import RehypePrsim from "rehype-prism-plus";
|
2023-03-26 12:29:02 +00:00
|
|
|
import { useRef } from "react";
|
|
|
|
import { copyToClipboard } from "../utils";
|
|
|
|
|
|
|
|
export function PreCode(props: { children: any }) {
|
|
|
|
const ref = useRef<HTMLPreElement>(null);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<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-20 06:06:29 +00:00
|
|
|
|
|
|
|
export function Markdown(props: { content: string }) {
|
2023-03-23 11:28:07 +00:00
|
|
|
return (
|
|
|
|
<ReactMarkdown
|
2023-03-30 04:48:38 +00:00
|
|
|
remarkPlugins={[RemarkMath, RemarkGfm, RemarkBreaks]}
|
2023-03-26 12:29:02 +00:00
|
|
|
rehypePlugins={[RehypeKatex, [RehypePrsim, { ignoreMissing: true }]]}
|
|
|
|
components={{
|
|
|
|
pre: PreCode,
|
|
|
|
}}
|
2023-03-23 11:28:07 +00:00
|
|
|
>
|
|
|
|
{props.content}
|
|
|
|
</ReactMarkdown>
|
|
|
|
);
|
|
|
|
}
|