|
1 | | -import { useState } from "react"; |
2 | | -import ReactMarkdown from "react-markdown"; |
| 1 | +import { memo, useMemo, useState } from "react"; |
| 2 | +import ReactMarkdown, { type Components } from "react-markdown"; |
3 | 3 | import remarkGfm from "remark-gfm"; |
4 | 4 | import rehypeHighlight from "rehype-highlight"; |
5 | 5 | import { Check, Copy } from "lucide-react"; |
6 | 6 | import { cn } from "@/lib/utils"; |
| 7 | +import { MermaidDiagram } from "@/components/mermaid-diagram"; |
7 | 8 | import "highlight.js/styles/github-dark.css"; |
8 | 9 |
|
| 10 | +// Hoisted to module scope: react-markdown rebuilds its unified processor |
| 11 | +// whenever these array/object references change, so keeping them stable |
| 12 | +// across renders avoids redoing that work on every streamed token. |
| 13 | +const REMARK_PLUGINS = [remarkGfm]; |
| 14 | +const REHYPE_PLUGINS = [rehypeHighlight]; |
| 15 | + |
9 | 16 | function CodeBlock({ className, children }: { className?: string; children: React.ReactNode }) { |
10 | 17 | const [copied, setCopied] = useState(false); |
11 | 18 | const text = String(children).replace(/\n$/, ""); |
@@ -33,37 +40,49 @@ function CodeBlock({ className, children }: { className?: string; children: Reac |
33 | 40 | ); |
34 | 41 | } |
35 | 42 |
|
36 | | -export function Markdown({ content }: { content: string }) { |
| 43 | +// isStreaming gates Mermaid rendering: a ```mermaid block is invalid syntax |
| 44 | +// for most of the time it's still being typed out token by token, so we show |
| 45 | +// it as a plain code block until the message is done, then swap it for the |
| 46 | +// live diagram — same pattern the agent write_file preview already uses for |
| 47 | +// its diff view. |
| 48 | +function createComponents(isStreaming: boolean): Components { |
| 49 | + return { |
| 50 | + pre: ({ children }) => <>{children}</>, |
| 51 | + code: ({ className, children, ...props }) => { |
| 52 | + const isInline = !className; |
| 53 | + if (isInline) { |
| 54 | + return ( |
| 55 | + <code className="rounded bg-muted px-1 py-0.5 text-[0.85em]" {...props}> |
| 56 | + {children} |
| 57 | + </code> |
| 58 | + ); |
| 59 | + } |
| 60 | + const language = className?.replace("hljs language-", "").replace("language-", "").trim(); |
| 61 | + if (language === "mermaid" && !isStreaming) { |
| 62 | + return <MermaidDiagram code={String(children).replace(/\n$/, "")} />; |
| 63 | + } |
| 64 | + return <CodeBlock className={className}>{children}</CodeBlock>; |
| 65 | + }, |
| 66 | + a: ({ className, ...props }) => ( |
| 67 | + <a |
| 68 | + className={cn(className, "underline underline-offset-2")} |
| 69 | + target="_blank" |
| 70 | + rel="noopener noreferrer" |
| 71 | + {...props} |
| 72 | + /> |
| 73 | + ), |
| 74 | + }; |
| 75 | +} |
| 76 | + |
| 77 | +const STATIC_COMPONENTS = createComponents(false); |
| 78 | + |
| 79 | +export const Markdown = memo(function Markdown({ content, isStreaming = false }: { content: string; isStreaming?: boolean }) { |
| 80 | + const components = useMemo(() => (isStreaming ? createComponents(true) : STATIC_COMPONENTS), [isStreaming]); |
37 | 81 | return ( |
38 | 82 | <div className="prose-chat"> |
39 | | - <ReactMarkdown |
40 | | - remarkPlugins={[remarkGfm]} |
41 | | - rehypePlugins={[rehypeHighlight]} |
42 | | - components={{ |
43 | | - pre: ({ children }) => <>{children}</>, |
44 | | - code: ({ className, children, ...props }) => { |
45 | | - const isInline = !className; |
46 | | - if (isInline) { |
47 | | - return ( |
48 | | - <code className="rounded bg-muted px-1 py-0.5 text-[0.85em]" {...props}> |
49 | | - {children} |
50 | | - </code> |
51 | | - ); |
52 | | - } |
53 | | - return <CodeBlock className={className}>{children}</CodeBlock>; |
54 | | - }, |
55 | | - a: ({ className, ...props }) => ( |
56 | | - <a |
57 | | - className={cn(className, "underline underline-offset-2")} |
58 | | - target="_blank" |
59 | | - rel="noopener noreferrer" |
60 | | - {...props} |
61 | | - /> |
62 | | - ), |
63 | | - }} |
64 | | - > |
| 83 | + <ReactMarkdown remarkPlugins={REMARK_PLUGINS} rehypePlugins={REHYPE_PLUGINS} components={components}> |
65 | 84 | {content} |
66 | 85 | </ReactMarkdown> |
67 | 86 | </div> |
68 | 87 | ); |
69 | | -} |
| 88 | +}); |
0 commit comments