import ReactMarkdown from "react-markdown"; import remarkGfm from "remark-gfm"; import rehypeRaw from "rehype-raw"; import { Prism as SyntaxHighlighter } from "react-syntax-highlighter"; import { vscDarkPlus } from "react-syntax-highlighter/dist/esm/styles/prism"; import { useState } from "react"; interface MarkdownViewerProps { content: string; } const CodeHeader: React.FC<{ language: string; onCopy: () => void; copied: boolean; }> = ({ language, onCopy, copied }) => (
{language}
{copied ? "已复制!" : "复制代码"}
); const MarkdownViewer: React.FC = ({ content }) => { const [copiedIndex, setCopiedIndex] = useState(null); const handleCopy = (code: string, index: number) => { navigator.clipboard.writeText(code); setCopiedIndex(index); setTimeout(() => setCopiedIndex(null), 2000); }; return ( handleCopy(code, node?.position?.start.line ?? 0) } copied={copiedIndex === node?.position?.start.line} />
{code}
); } return ( {children} ); }, }} > {content}
); }; export default MarkdownViewer;