"use client"; import { Button } from "@workspace/ui/components/button"; import { cn } from "@workspace/ui/lib/utils"; import { Check, Copy } from "lucide-react"; import { useCallback, useState } from "react"; import ReactMarkdown, { type Components } from "react-markdown"; import { Prism as SyntaxHighlighter } from "react-syntax-highlighter"; import { oneDark } from "react-syntax-highlighter/dist/esm/styles/prism"; import rehypeKatex from "rehype-katex"; import rehypeRaw from "rehype-raw"; import remarkGfm from "remark-gfm"; import remarkMath from "remark-math"; import remarkToc from "remark-toc"; interface CodeBlockProps { className?: string; children?: React.ReactNode; [key: string]: unknown; } function CodeBlock({ className, children, ...props }: CodeBlockProps) { const [copied, setCopied] = useState(false); const match = className?.startsWith("language-") ? /language-(\w+)/.exec(className) : null; const handleCopy = useCallback((text: string) => { navigator.clipboard .writeText(text) .then(() => { setCopied(true); setTimeout(() => setCopied(false), 3000); }) .catch(() => { alert("Failed to copy text. Please try again."); }); }, []); if (match) { return (
{children}
);
}
interface MarkdownProps {
children: string;
components?: Components;
}
export function Markdown({ children, components }: MarkdownProps) {
return (