import React, { useMemo } from "react"; import ReactMarkdown from "react-markdown"; import rehypeRaw from "rehype-raw"; import rehypeSanitize from "rehype-sanitize"; import remarkGfm from "remark-gfm"; import { cn } from "@/lib/utils"; import { Citation } from "./chat/Citation"; import { Source } from "./chat/types"; interface MarkdownViewerProps { content: string; className?: string; getCitationSource?: (id: number) => Source | null; } export function MarkdownViewer({ content, className, getCitationSource }: MarkdownViewerProps) { // Memoize the markdown components to prevent unnecessary re-renders const components = useMemo(() => { return { // Define custom components for markdown elements p: ({node, children, ...props}: any) => { // If there's no getCitationSource function, just render normally if (!getCitationSource) { return

{children}

; } // Process citations within paragraph content return

{processCitationsInReactChildren(children, getCitationSource)}

; }, a: ({node, children, ...props}: any) => { // Process citations within link content if needed const processedChildren = getCitationSource ? processCitationsInReactChildren(children, getCitationSource) : children; return {processedChildren}; }, li: ({node, children, ...props}: any) => { // Process citations within list item content const processedChildren = getCitationSource ? processCitationsInReactChildren(children, getCitationSource) : children; return
  • {processedChildren}
  • ; }, ul: ({node, ...props}: any) =>