"use client" import ReactMarkdown from "react-markdown" import remarkGfm from "remark-gfm" import rehypeHighlight from "rehype-highlight" import "highlight.js/styles/github-dark.css" import { DiffTool } from "./diff-tool" import { useState, isValidElement, type ReactNode } from "react" import { cn } from "@/lib/utils" import { Copy } from "lucide-react" interface MarkdownRendererProps { content: string className?: string tone?: "default" | "bubble" } // Parse diff tool calls from markdown content function parseDiffTools(content: string) { const diffToolRegex = /```diff-tool\n([\s\S]*?)\n```/g const tools: Array<{ match: string; props: any }> = [] let match while ((match = diffToolRegex.exec(content)) !== null) { try { const props = JSON.parse(match[1]) tools.push({ match: match[0], props }) } catch (e) { console.error('Failed to parse diff tool:', e) } } return tools } export function MarkdownRenderer({ content, className = "", tone = "default" }: MarkdownRendererProps) { // Parse diff tools from content const diffTools = parseDiffTools(content) let processedContent = content // Replace diff tool calls with placeholders diffTools.forEach((tool, index) => { processedContent = processedContent.replace(tool.match, `__DIFF_TOOL_${index}__`) }) const baseTone = tone === "bubble" ? "text-charcoal dark:text-white" : "text-charcoal dark:text-foreground" const mutedTone = tone === "bubble" ? "text-charcoal/80 dark:text-white/80" : "text-charcoal/80 dark:text-foreground/75" return (
{children}
) }, // Custom styling for different elements h1: ({ children }) => (
{children}
)
}
return (
{children}
)
},
pre: ({ children, className }) => (
{children}), a: ({ children, href }) => ( {children} ), strong: ({ children }) => ( {children} ), em: ({ children }) => ( {children} ), table: ({ children }) => (
{children}