"use client"; import { useMemo, useRef } from "react"; import type { AnchorHTMLAttributes, HTMLAttributes, MouseEvent as ReactMouseEvent, ReactNode, } from "react"; import { MessageResponse, type MessageResponseProps, } from "@/components/ai-elements/message"; import { streamdownPlugins, useStreamdownMermaidPngDownload } from "@/core/streamdown"; import { createMarkdownAnchorResolver, isInPageAnchor, scrollToInPageAnchor, } from "@/core/utils/markdown"; import { cn } from "@/lib/utils"; import { VisualizationBlock } from "../visualizations/visualization-block"; import { splitVisualizationContent } from "../visualizations/visualization-spec"; import { CitationLink } from "../citations/citation-link"; function isExternalUrl(href: string | undefined): boolean { return !!href && /^https?:\/\//.test(href); } function extractTextFromChildren(children: ReactNode): string { if (typeof children === "string" || typeof children === "number") return String(children); if (Array.isArray(children)) return children.map(extractTextFromChildren).join(""); return ""; } export type MarkdownContentProps = { content: string; isLoading: boolean; rehypePlugins: MessageResponseProps["rehypePlugins"]; className?: string; remarkPlugins?: MessageResponseProps["remarkPlugins"]; components?: MessageResponseProps["components"]; linkClassName?: (href: string, label: string) => string | undefined; onLinkClick?: ( event: ReactMouseEvent, href: string, label: string, ) => void; }; /** Renders markdown content. */ export function MarkdownContent({ content, isLoading, rehypePlugins, className, remarkPlugins = streamdownPlugins.remarkPlugins, components: componentsFromProps, linkClassName, onLinkClick, }: MarkdownContentProps) { const rootRef = useRef(null); const anchorResolver = useMemo(() => createMarkdownAnchorResolver(content), [content]); const visualizationSegments = useMemo(() => splitVisualizationContent(content), [content]); useStreamdownMermaidPngDownload(rootRef); const components = useMemo(() => { const heading = (level: 1 | 2 | 3 | 4 | 5 | 6) => { const Tag = `h${level}` as const; return ({ children, id, ...props }: HTMLAttributes) => ( {children} ); }; return { h1: heading(1), h2: heading(2), h3: heading(3), h4: heading(4), h5: heading(5), h6: heading(6), a: (props: AnchorHTMLAttributes) => { if (props.href?.startsWith("#reference-")) { return ; } if (typeof props.children === "string") { const match = /^citation:(.+)$/.exec(props.children); if (match) { const [, text] = match; return {text}; } } const { className, target, rel, ...rest } = props; const external = isExternalUrl(props.href); const resolvedHref = anchorResolver.resolveHref( props.href, extractTextFromChildren(props.children), ); if (resolvedHref === null) { return ( {props.children} ); } const finalHref = resolvedHref ?? props.href; const label = extractTextFromChildren(props.children); return ( { // HashRouter would turn a `#section` click into a route change → 404. // Scroll to the heading ourselves instead. if (isInPageAnchor(finalHref)) { event.preventDefault(); scrollToInPageAnchor(event.currentTarget, finalHref); } onLinkClick?.(event, finalHref || "", label); rest.onClick?.(event); }} /> ); }, ...componentsFromProps, }; }, [anchorResolver, componentsFromProps, linkClassName, onLinkClick]); if (!content) return null; // Preserve the exact legacy rendering path for ordinary Markdown. Heavy // visualization renderers are lazy-loaded only when a dedicated fence exists. if ( visualizationSegments.length === 1 && visualizationSegments[0]?.type === "markdown" ) { return (
{content}
); } return (
{visualizationSegments.map((segment, index) => segment.type === "visualization" ? ( ) : segment.content ? ( {segment.content} ) : null, )}
); }