markdown.tsx 1023 B

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