import React, { Suspense, useEffect, useState } from "react" import Markdown from "react-markdown" import remarkGfm from "remark-gfm" import { cn } from "@/lib/utils" import { CopyButton } from "@/components/ui/copy-button" interface MarkdownRendererProps { children: string } export function MarkdownRenderer({ children }: MarkdownRendererProps) { return (
{children}} if (!tokens) { return
{children}} return (
{tokens.map((line, lineIndex) => (
{line.map((token, tokenIndex) => {
const style =
typeof token.htmlStyle === "string"
? undefined
: token.htmlStyle
return (
{token.content}
)
})}
{lineIndex !== tokens.length - 1 && "\n"}
))}
)
}
)
HighlightedPre.displayName = "HighlightedCode"
interface CodeBlockProps extends React.HTMLAttributes&]:rounded-md [:not(pre)>&]:bg-background/50 [:not(pre)>&]:px-1 [:not(pre)>&]:py-0.5"
)}
{...rest}
>
{children}
)
},
pre: ({ children }: any) => children,
ol: withClass("ol", "list-decimal space-y-2 pl-6"),
ul: withClass("ul", "list-disc space-y-2 pl-6"),
li: withClass("li", "my-1.5"),
table: withClass(
"table",
"w-full border-collapse overflow-y-auto rounded-md border border-foreground/20"
),
th: withClass(
"th",
"border border-foreground/20 px-4 py-2 text-left font-bold [&[align=center]]:text-center [&[align=right]]:text-right"
),
td: withClass(
"td",
"border border-foreground/20 px-4 py-2 text-left [&[align=center]]:text-center [&[align=right]]:text-right"
),
tr: withClass("tr", "m-0 border-t p-0 even:bg-muted"),
p: withClass("p", "whitespace-pre-wrap"),
hr: withClass("hr", "border-foreground/20"),
}
function withClass(Tag: keyof JSX.IntrinsicElements, classes: string) {
const Component = ({ node, ...props }: any) => (