import { Children, isValidElement } from "react"; import ReactMarkdown from "react-markdown"; import rehypeKatex from "rehype-katex"; import remarkGfm from "remark-gfm"; import remarkMath from "remark-math"; import { CodeBlock } from "@/components/CodeBlock"; import { cn } from "@/lib/utils"; import "katex/dist/katex.min.css"; interface MarkdownTextRendererProps { children: string; className?: string; } /** * Heavy markdown stack (GFM, math, KaTeX, syntax highlighting) kept in a * separate chunk so the app shell can paint sooner on refresh. */ export default function MarkdownTextRenderer({ children, className, }: MarkdownTextRendererProps) { return (
; } const raw = String(kids).replace(/\n$/, ""); /** Plain fenced ``` blocks (no language) & wide one-liners: block monospace, not inline pill. */ const widePlainBlock = raw.includes("\n") || raw.length > 120; if (widePlainBlock) { return ( {kids} ); } return ( {kids} ); }, pre({ children: markdownChildren }) { const kids = Children.toArray(markdownChildren); const lone = kids.length === 1 ? kids[0] : null; /** Highlighted fences render ``CodeBlock`` (block shell); skip invalid ``
``. */ if (lone != null && isValidElement(lone) && lone.type === CodeBlock) { return <>{markdownChildren}; } return (
                {markdownChildren}
              
); }, a({ href, children: markdownChildren, ...props }) { return ( {markdownChildren} ); }, }} > {children}
); }