markdown.tsx 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import ReactMarkdown from "react-markdown";
  2. import "katex/dist/katex.min.css";
  3. import RemarkMath from "remark-math";
  4. import RemarkBreaks from "remark-breaks";
  5. import RehypeKatex from "rehype-katex";
  6. import RemarkGfm from "remark-gfm";
  7. import RehypePrsim from "rehype-prism-plus";
  8. import { useRef } from "react";
  9. import { copyToClipboard } from "../utils";
  10. export function PreCode(props: { children: any }) {
  11. const ref = useRef<HTMLPreElement>(null);
  12. return (
  13. <pre ref={ref}>
  14. <span
  15. className="copy-code-button"
  16. onClick={() => {
  17. if (ref.current) {
  18. const code = ref.current.innerText;
  19. copyToClipboard(code);
  20. }
  21. }}
  22. ></span>
  23. {props.children}
  24. </pre>
  25. );
  26. }
  27. export function Markdown(props: { content: string }) {
  28. return (
  29. <ReactMarkdown
  30. remarkPlugins={[RemarkMath, RemarkGfm, RemarkBreaks]}
  31. rehypePlugins={[RehypeKatex, [RehypePrsim, { ignoreMissing: true }]]}
  32. components={{
  33. pre: PreCode,
  34. }}
  35. >
  36. {props.content}
  37. </ReactMarkdown>
  38. );
  39. }