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";
|
2023-04-02 14:48:18 +00:00
|
|
|
import RehypeHighlight from "rehype-highlight";
|
|
|
|
import { useRef, useState, RefObject, useEffect } from "react";
|
2023-03-26 12:29:02 +00:00
|
|
|
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
|
|
|
|
2023-04-02 14:48:18 +00:00
|
|
|
const useLazyLoad = (ref: RefObject<Element>): boolean => {
|
|
|
|
const [isIntersecting, setIntersecting] = useState<boolean>(false);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const observer = new IntersectionObserver(([entry]) => {
|
|
|
|
if (entry.isIntersecting) {
|
|
|
|
setIntersecting(true);
|
|
|
|
observer.disconnect();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (ref.current) {
|
|
|
|
observer.observe(ref.current);
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
observer.disconnect();
|
|
|
|
};
|
|
|
|
}, [ref]);
|
|
|
|
|
|
|
|
return isIntersecting;
|
|
|
|
};
|
|
|
|
|
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-04-02 14:48:18 +00:00
|
|
|
rehypePlugins={[
|
|
|
|
RehypeKatex,
|
|
|
|
[
|
|
|
|
RehypeHighlight,
|
|
|
|
{
|
2023-04-02 19:14:53 +00:00
|
|
|
detect: false,
|
2023-04-02 14:48:18 +00:00
|
|
|
ignoreMissing: true,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
]}
|
2023-03-26 12:29:02 +00:00
|
|
|
components={{
|
|
|
|
pre: PreCode,
|
|
|
|
}}
|
2023-04-04 08:33:36 +00:00
|
|
|
linkTarget={'_blank'}
|
2023-03-23 11:28:07 +00:00
|
|
|
>
|
|
|
|
{props.content}
|
|
|
|
</ReactMarkdown>
|
|
|
|
);
|
|
|
|
}
|