import React, { useState } from 'react'; import ReactMarkdown from 'react-markdown'; import remarkMath from 'remark-math'; import rehypeKatex from 'rehype-katex'; import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'; import { vscDarkPlus } from 'react-syntax-highlighter/dist/esm/styles/prism'; import { Copy, Check, Terminal } from 'lucide-react'; const CodeBlock = ({ node, inline, className, children, ...props }: any) => { const [copied, setCopied] = useState(false); const match = /language-(\w+)/.exec(className || ''); const language = match ? match[1] : ''; // Inline code (e.g. `const x = 1`) if (inline) { return ( {children} ); } const codeString = String(children).replace(/\n$/, ''); const handleCopy = () => { navigator.clipboard.writeText(codeString); setCopied(true); setTimeout(() => setCopied(false), 2000); }; return (
{/* Code Header */}
{language || 'text'}
{/* Syntax Highlighter */}
{codeString}
); }; const MarkdownRenderer = ({ content, className }: { content: string, className?: string }) => { /** * Pre-process content to handle common LaTeX delimiters from Gemini * and optimize Markdown compatibility. */ const preprocessMarkdown = (text: string) => { if (!text) return ""; return text // Replace \[ ... \] with $$ ... $$ .replace(/\\\[/g, '$$$$') .replace(/\\\]/g, '$$$$') // Replace \( ... \) with $ ... $ .replace(/\\\(/g, '$$') .replace(/\\\)/g, '$$') // Fix potential spacing issues between bold marks and math delimiters .replace(/\*\*(\$)/g, '** $1') .replace(/(\$)\*\*/g, '$1 **'); }; return (
{preprocessMarkdown(content)}
); }; export default MarkdownRenderer;