import React from 'react'; import ReactMarkdown from 'react-markdown'; import remarkGfm from 'remark-gfm'; import rehypeRaw from 'rehype-raw'; // 新增:用于解析 Markdown 中的 HTML 标签 import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'; import { vscDarkPlus } from 'react-syntax-highlighter/dist/esm/styles/prism'; export interface MarkdownPreviewProps { content?: string; fileName?: string; } export const MarkdownPreview: React.FC = ({ content = '', fileName }) => { if (!content) { return (
No content available
); } return (
url} // ✅ 放行 data:image 等所有协议 components={{ // ✅ 自定义图片渲染器,解决 Base64 图片显示问题并限制最大宽度 img: ({ node, ...props }) => ( ), // 代码块高亮 code({ node, inline, className, children, ...props }: any) { const match = /language-(\w+)/.exec(className || ''); const codeString = String(children).replace(/\n$/, ''); if (inline) { return ( {children} ); } if (match) { return ( {codeString} ); } return (
                  
                    {codeString}
                  
                
); }, // 链接 a: ({ href, children }) => ( {children} ), // 表格 (保持原有样式,rehypeRaw 会让 HTML table 进入此组件) table: ({ children }) => (
{children}
), thead: ({ children }) => ( {children} ), th: ({ children }) => ( {children} ), td: ({ children }) => ( {children} ), }} > {content}
); };