"use client"; import { code } from "@streamdown/code"; import { Streamdown, type Components, type UrlTransform } from "streamdown"; import { cn } from "@/lib/utils"; import { CodeBlock } from "./code-block"; const ALLOWED_LINK_PREFIXES = ["https://", "http://", "mailto:", "#", "/"]; const ALLOWED_IMAGE_PREFIXES = ["https://", "data:image/"]; /** * Whitelist URL scheme to mitigate XSS via javascript:/data: links while * keeping inline fragments and relative paths usable inside the dashboard. */ const safeUrlTransform: UrlTransform = (url, key) => { const lower = url.toLowerCase(); const allowList = key === "src" ? ALLOWED_IMAGE_PREFIXES : ALLOWED_LINK_PREFIXES; return allowList.some((prefix) => lower.startsWith(prefix)) ? url : ""; }; interface MarkdownProps { children: string; className?: string; linkComponent?: Components["a"]; } /** * Thin wrapper around Streamdown that applies our shadcn-friendly typography * and a conservative URL whitelist. Streamdown ships KaTeX, Shiki and GFM out * of the box and repairs partial Markdown during streaming. */ export function Markdown({ children, className, linkComponent, }: MarkdownProps) { return (
) keeps the subtle chip look. Block // code is fully owned by the CodeBlock component below. "[&_:not(pre)>code]:rounded [&_:not(pre)>code]:bg-[hsl(var(--muted))] [&_:not(pre)>code]:px-1 [&_:not(pre)>code]:py-0.5 [&_:not(pre)>code]:text-[0.85em]", "[&_table]:my-2 [&_table]:w-full [&_table]:border-collapse", "[&_th]:border [&_th]:border-[hsl(var(--border))] [&_th]:px-2 [&_th]:py-1 [&_th]:text-left [&_th]:font-semibold", "[&_td]:border [&_td]:border-[hsl(var(--border))] [&_td]:px-2 [&_td]:py-1", "[&_blockquote]:my-2 [&_blockquote]:border-l-2 [&_blockquote]:border-[hsl(var(--border))] [&_blockquote]:pl-3 [&_blockquote]:text-muted-foreground", "[&_hr]:my-3 [&_hr]:border-[hsl(var(--border))]", className, )} > ` or inline event // handler that the Markdown → HTML path would pass through // verbatim. skipHtml urlTransform={safeUrlTransform} plugins={{ code }} components={{ pre: CodeBlock, ...(linkComponent ? { a: linkComponent } : {}), }} controls={{ code: false, table: false, mermaid: false, }} > {children}
); }