-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathChatMarkdown.tsx
More file actions
166 lines (161 loc) · 5.23 KB
/
Copy pathChatMarkdown.tsx
File metadata and controls
166 lines (161 loc) · 5.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import {
Heading,
Separator,
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
Text,
} from "@posthog/quill";
import {
parseOpenFence,
splitMarkdownBlocks,
} from "@posthog/ui/features/editor/components/splitMarkdownBlocks";
import { HighlightedCode } from "@posthog/ui/primitives/HighlightedCode";
import { memo, useMemo } from "react";
import Markdown, { type Components } from "react-markdown";
import rehypeSanitize from "rehype-sanitize";
import remarkGfm from "remark-gfm";
/**
* The chat thread's own markdown renderer — intentionally separate from the app-wide
* `MarkdownRenderer` (which carries PostHog deeplink handling, Radix Text wrappers, and other
* product baggage). This one is a thin, generic react-markdown setup for chat bubble content:
* GFM + sanitized HTML, minimal prose styling. Restyle the element map below per product.
*/
const components: Components = {
p: ({ children }) => (
<Text className="text-sm leading-[1.5]">{children}</Text>
),
a: ({ children, href }) => (
<a
href={href}
target="_blank"
rel="noreferrer"
className="text-primary underline underline-offset-2"
>
{children}
</a>
),
ul: ({ children }) => (
<ul className="list-disc space-y-0.5 ps-4">{children}</ul>
),
ol: ({ children }) => (
<ol className="list-decimal space-y-0.5 ps-5">{children}</ol>
),
li: ({ children }) => <li className="text-sm">{children}</li>,
code: ({ className, children }) => {
const match = /language-(\w+)/.exec(className ?? "");
if (match) {
// Fenced block with a language → Shiki-highlighted (theme-aware). The `pre` renderer
// below provides the box; HighlightedCode renders the colored <code> inside it.
return (
<HighlightedCode
code={String(children).replace(/\n$/, "")}
language={match[1]}
className="rounded-sm bg-muted/50 text-xs"
/>
);
}
return (
<code className="rounded rounded-sm border border-border bg-muted/50 px-1 font-mono text-xs">
{children}
</code>
);
},
pre: ({ children }) => (
<pre className="overflow-x-auto rounded-lg border border-border bg-muted/50 p-3 text-sm leading-[1.5]">
{children}
</pre>
),
h1: ({ children }) => (
<Heading size="xl" className="font-bold">
{children}
</Heading>
),
h2: ({ children }) => (
<Heading size="lg" className="font-bold">
{children}
</Heading>
),
h3: ({ children }) => (
<Heading size="base" className="font-bold">
{children}
</Heading>
),
blockquote: ({ children }) => (
<blockquote className="border-(--gray-6) border-s-2 ps-3 text-(--gray-11)">
{children}
</blockquote>
),
hr: () => <Separator />,
table: ({ children }) => (
<Table size="sm" className="rounded-md border border-border">
{children}
</Table>
),
thead: ({ children }) => <TableHeader>{children}</TableHeader>,
th: ({ children }) => <TableHead>{children}</TableHead>,
tbody: ({ children }) => <TableBody>{children}</TableBody>,
tr: ({ children }) => <TableRow>{children}</TableRow>,
td: ({ children }) => <TableCell>{children}</TableCell>,
};
const remarkPlugins = [remarkGfm];
const rehypePlugins = [rehypeSanitize];
export const ChatMarkdown = memo(function ChatMarkdown({
content,
}: {
content: string;
}) {
return (
<div className="flex flex-col gap-3 [&>*:first-child]:mt-0 [&>*:last-child]:mb-0">
<Markdown
remarkPlugins={remarkPlugins}
rehypePlugins={rehypePlugins}
components={components}
>
{content}
</Markdown>
</div>
);
});
/**
* Streaming variant of {@link ChatMarkdown}: splits the message into top-level blocks so completed
* blocks keep a stable string and their memoized parse is reused — each streamed frame re-parses
* only the growing tail block, O(last block) instead of O(message).
*
* While the tail sits inside an unterminated code fence it renders as plain monospace in the same
* `pre` box the finished block will use — no per-frame Shiki highlight, no layout shift when the
* fence closes. Completed messages should render through {@link ChatMarkdown} directly for a
* single, fully-correct parse.
*/
export const ChatStreamingMarkdown = memo(function ChatStreamingMarkdown({
content,
}: {
content: string;
}) {
const blocks = useMemo(() => splitMarkdownBlocks(content), [content]);
const lastIndex = blocks.length - 1;
return (
<div className="flex flex-col gap-3 [&>*:first-child]:mt-0 [&>*:last-child]:mb-0">
{blocks.map((block, index) => {
const key = `b${index}`;
const openFence = index === lastIndex ? parseOpenFence(block) : null;
if (openFence) {
return (
<div key={key} className="flex flex-col gap-3">
{openFence.before.trim() ? (
<ChatMarkdown content={openFence.before} />
) : null}
<pre className="overflow-x-auto rounded-lg border border-border bg-muted/50 p-3 text-sm leading-[1.5]">
<code className="font-mono text-xs">{openFence.code}</code>
</pre>
</div>
);
}
return <ChatMarkdown key={key} content={block} />;
})}
</div>
);
});