import React from 'react'; import ReactMarkdown from 'react-markdown'; import remarkGfm from 'remark-gfm'; import rehypeRaw from 'rehype-raw'; import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'; import { tomorrow } from 'react-syntax-highlighter/dist/cjs/styles/prism'; import Mermaid from './Mermaid'; interface MarkdownProps { content: string; } const Markdown: React.FC = ({ content }) => { // Define markdown components const MarkdownComponents: React.ComponentProps['components'] = { p({ children, ...props }: { children?: React.ReactNode }) { return

{children}

; }, h1({ children, ...props }: { children?: React.ReactNode }) { return

{children}

; }, h2({ children, ...props }: { children?: React.ReactNode }) { // Special styling for ReAct headings if (children && typeof children === 'string') { const text = children.toString(); if (text.includes('Thought') || text.includes('Action') || text.includes('Observation') || text.includes('Answer')) { return (

{children}

); } } return

{children}

; }, h3({ children, ...props }: { children?: React.ReactNode }) { return

{children}

; }, h4({ children, ...props }: { children?: React.ReactNode }) { return

{children}

; }, ul({ children, ...props }: { children?: React.ReactNode }) { return ; }, ol({ children, ...props }: { children?: React.ReactNode }) { return
    {children}
; }, li({ children, ...props }: { children?: React.ReactNode }) { return
  • {children}
  • ; }, a({ children, href, ...props }: { children?: React.ReactNode; href?: string }) { return ( {children} ); }, blockquote({ children, ...props }: { children?: React.ReactNode }) { return (
    {children}
    ); }, table({ children, ...props }: { children?: React.ReactNode }) { return (
    {children}
    ); }, thead({ children, ...props }: { children?: React.ReactNode }) { return {children}; }, tbody({ children, ...props }: { children?: React.ReactNode }) { return {children}; }, tr({ children, ...props }: { children?: React.ReactNode }) { return {children}; }, th({ children, ...props }: { children?: React.ReactNode }) { return ( {children} ); }, td({ children, ...props }: { children?: React.ReactNode }) { return {children}; }, code(props: { inline?: boolean; className?: string; children?: React.ReactNode; // eslint-disable-next-line @typescript-eslint/no-explicit-any [key: string]: any; // Using any here as it's required for ReactMarkdown components }) { const { inline, className, children, ...otherProps } = props; const match = /language-(\w+)/.exec(className || ''); const codeContent = children ? String(children).replace(/\n$/, '') : ''; // Handle Mermaid diagrams if (!inline && match && match[1] === 'mermaid') { return (
    ); } // Handle code blocks if (!inline && match) { return (
    {match[1]}
    {codeContent}
    ); } // Handle inline code return ( {children} ); }, }; return (
    {content}
    ); }; export default Markdown;