import dynamic from "next/dynamic"; import { useMemo, memo } from "react"; import ReactMarkdown, { type Options, type Components } from "react-markdown"; import rehypeRaw from "rehype-raw"; import remarkGfm from "remark-gfm"; import remarkMath from "remark-math"; import remarkBreaks from "remark-breaks"; import rehypeHighlight from "rehype-highlight"; import rehypeKatex from "rehype-katex"; import { clsx } from "clsx"; import { omit } from "radash"; import "katex/dist/katex.min.css"; import "./style.css"; const Code = dynamic(() => import("./Code")); const Mermaid = dynamic(() => import("./Mermaid")); export type MarkdownProps = { id?: string; className?: string; children: string; components?: Partial; }; function MarkdownBlock({ children: content, ...rest }: Options) { const remarkPlugins = useMemo( () => rest.remarkPlugins ?? [], [rest.remarkPlugins] ); const rehypePlugins = useMemo( () => rest.rehypePlugins ?? [], [rest.rehypePlugins] ); const components = useMemo(() => rest.components ?? {}, [rest.components]); return ( { const { children, className, ...rest } = props; return (
              {children}
            
); }, code: (props) => { const { children, className, ...rest } = props; const isInline = !props.node?.position?.start.line || props.node?.position?.start.line === props.node?.position?.end.line; if (isInline) { return ( {children} ); } if (className?.includes("hljs")) { const lang = /language-(\w+)/.exec(className || ""); if (lang && lang[1] === "mermaid") { return {children}; } return ( {children} ); } else { return ( {children} ); } }, a: (props) => { const { children, className, href = "", target, ...rest } = props; if (/\.(aac|mp3|opus|wav)$/.test(href)) { return (
); } if (/\.(3gp|3g2|webm|ogv|mpeg|mp4|avi)$/.test(href)) { return ( ); } const isInternal = /^\/#/i.test(href); const isReferenceLink = /^[0-9]*$/.test(children?.toString() || ""); return ( {children} ); }, img: (props) => { const { className, src, alt, ...rest } = props; return ( {alt} ); }, ...components, }} > {content}
); } export default memo(MarkdownBlock);