"use client"; import React, { useState } from "react"; import ReactMarkdown from "react-markdown"; import remarkGfm from "remark-gfm"; import rehypeSlug from "rehype-slug"; import rehypeHighlight from "rehype-highlight"; function extractText(node: React.ReactNode): string { if (node === null || node === undefined || typeof node === "boolean") { return ""; } if (typeof node === "string" || typeof node === "number") { return String(node); } if (Array.isArray(node)) { return node.map(extractText).join(""); } if (React.isValidElement(node)) { return extractText((node.props as { children?: React.ReactNode })?.children); } return ""; } function PreBlock({ children, ...props }: React.HTMLAttributes) { const [copied, setCopied] = useState(false); const textContent = extractText(children).replace(/\n$/, ""); let language = "text"; if (React.isValidElement(children)) { const className = (children.props as { className?: string })?.className || ""; const match = /language-(\w+)/.exec(className); if (match) { language = match[1]; } } function handleCopy() { navigator.clipboard.writeText(textContent); setCopied(true); setTimeout(() => setCopied(false), 2000); } return (
{/* Code block header bar */}
{language}
        {children}
      
); } export default function Markdown({ source }: { source: string }) { return (
) => { if (className?.includes("hljs") || className?.includes("language-")) { return ( {children} ); } return ( {children} ); }, table: ({ node, ...props }) => (
), blockquote: ({ children }) => { return (
{children}
); }, }} > {source} ); }