markdown.tsx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 RehypeHighlight from "rehype-highlight";
  8. import { useRef, useState, RefObject, useEffect } 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. const useLazyLoad = (ref: RefObject<Element>): boolean => {
  28. const [isIntersecting, setIntersecting] = useState<boolean>(false);
  29. useEffect(() => {
  30. const observer = new IntersectionObserver(([entry]) => {
  31. if (entry.isIntersecting) {
  32. setIntersecting(true);
  33. observer.disconnect();
  34. }
  35. });
  36. if (ref.current) {
  37. observer.observe(ref.current);
  38. }
  39. return () => {
  40. observer.disconnect();
  41. };
  42. }, [ref]);
  43. return isIntersecting;
  44. };
  45. export function Markdown(props: { content: string }) {
  46. return (
  47. <ReactMarkdown
  48. remarkPlugins={[RemarkMath, RemarkGfm, RemarkBreaks]}
  49. rehypePlugins={[
  50. RehypeKatex,
  51. [
  52. RehypeHighlight,
  53. {
  54. detect: false,
  55. ignoreMissing: true,
  56. },
  57. ],
  58. ]}
  59. components={{
  60. pre: PreCode,
  61. }}
  62. >
  63. {props.content}
  64. </ReactMarkdown>
  65. );
  66. }