import React, { useMemo, useState, useEffect } from 'react'; import MDEditor from '@uiw/react-md-editor'; import remarkMath from 'remark-math'; import rehypeKatex from 'rehype-katex'; import katex from 'katex'; import 'katex/dist/katex.css'; import { MCPClient } from '../mcp/client'; /** * Custom Code component that handles: * - Language "katex" or "math": live KaTeX math display * - Language "svg": live SVG rendering * - Standard code blocks with syntax highlighting */ const CodeRenderer = ({ node, className, children, ...props }) => { const codeString = String(children || '').replace(/\n$/, ''); const match = /language-(\w+)/.exec(className || ''); const language = match ? match[1].toLowerCase() : ''; if (language === 'katex' || language === 'math') { try { const html = katex.renderToString(codeString, { displayMode: true, throwOnError: false, strict: false }); return (
); } catch (err) { return (
Failed to render KaTeX: {err.message}
); } } if (language === 'svg') { return (
SVG Preview
); } return ( {children} ); }; /** * Custom Image component that safely renders SVGs and normal images */ const ImageRenderer = ({ node, src, alt, ...props }) => { const isSvg = src && (src.endsWith('.svg') || src.includes('.svg?') || src.startsWith('data:image/svg+xml')); return ( {alt { e.target.style.opacity = '0.5'; e.target.title = `Failed to load image: ${src}`; }} {...props} /> {alt && {alt}} ); }; export default function Editor({ value, onChange, colorMode = 'dark', theme = 'solarized-dark', height = '100%', onFileDrop }) { const previewOptions = useMemo(() => ({ remarkPlugins: [remarkMath], rehypePlugins: [[rehypeKatex, { throwOnError: false, strict: false }]], components: { code: CodeRenderer, img: ImageRenderer, } }), []); const handleDragOver = (e) => { e.preventDefault(); e.stopPropagation(); }; const handleDrop = (e) => { e.preventDefault(); e.stopPropagation(); const file = e.dataTransfer?.files?.[0]; if (file && onFileDrop) { onFileDrop(file); } }; return (
); }