"use client"; import { Children, isValidElement, useMemo } from "react"; import ReactMarkdown from "react-markdown"; import remarkGfm from "remark-gfm"; import type { Components } from "react-markdown"; import { MermaidDiagram } from "./mermaid-diagram"; interface MarkdownRendererProps { content: string; } function getTextContent(node: React.ReactNode): string { if (typeof node === "string") return node; if (typeof node === "number") return String(node); if (!node) return ""; if (Array.isArray(node)) return node.map(getTextContent).join(""); if (isValidElement(node) && (node.props as Record)?.children) return getTextContent((node.props as Record).children as React.ReactNode); return ""; } const components: Components = { // ── Headings ── h1: ({ children }) => (

{children}

), h2: ({ children }) => (

{children}

), h3: ({ children }) => (

{children}

), h4: ({ children }) => (

{children}

), // ── Paragraphs & text ── p: ({ children }) => (

{children}

), strong: ({ children }) => ( {children} ), em: ({ children }) => ( {children} ), // ── Lists ── ul: ({ children }) => ( ), ol: ({ children }) => (
    {children}
), li: ({ children }) =>
  • {children}
  • , // ── Links ── a: ({ href, children }) => ( {children} ), // ── Blockquotes ── blockquote: ({ children }) => (
    {children}
    ), // ── Code blocks — with Mermaid detection ── pre: ({ children }) => { const child = Children.only(children); const childProps = isValidElement(child) ? (child.props as Record) : null; if (childProps?.className === "language-mermaid") { const chart = getTextContent(childProps.children as React.ReactNode); return ; } return (
            {children}
          
    ); }, code: ({ className, children }) => { const isBlock = className?.startsWith("language-"); if (isBlock) { return ( {children} ); } return ( {children} ); }, // ── Tables — Excel-style ── table: ({ children }) => (
    {children}
    ), thead: ({ children }) => ( {children} ), th: ({ children }) => ( {children} ), tbody: ({ children }) => {children}, tr: ({ children }) => ( {children} ), td: ({ children }) => ( {children} ), // ── Horizontal rule ── hr: () => (
    ), // ── Images (placeholder) ── img: ({ alt }) => ( [{alt || "image"}] ), }; export function MarkdownRenderer({ content }: MarkdownRendererProps) { const processedContent = useMemo(() => content, [content]); return (
    {processedContent}
    ); }