The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 25, 16:00 UTC
simp
Julius Marminge committed
Feb 28, 2026 at 15:10 UTC
33e6f35ce9b4c846a64c818430797590fe965fe0
1 file changed
+71
-143
apps/web/src/components/ChatMarkdown.tsx
+71
-143
@@ -1,8 +1,14 @@
1
-import { getSharedHighlighter } from "@pierre/diffs";
1
+import {
2
+ getSharedHighlighter,
3
+ type DiffsHighlighter,
4
+ type SupportedLanguages,
5
+} from "@pierre/diffs";
6
import { CheckIcon, CopyIcon } from "lucide-react";
7
import {
8
Children,
9
+ Suspense,
10
isValidElement,
11
+ use,
12
useCallback,
13
memo,
14
useEffect,
@@ -14,12 +20,12 @@ import {
20
import type { Components } from "react-markdown";
21
import ReactMarkdown from "react-markdown";
22
import remarkGfm from "remark-gfm";
17
-import { LRUCache } from "../lib/lruCache";
18
-import { readNativeApi } from "../nativeApi";
23
import { resolveDiffThemeName, type DiffThemeName } from "../lib/diffThemes";
24
import { fnv1a32 } from "../lib/diffRendering";
25
+import { LRUCache } from "../lib/lruCache";
26
import { useTheme } from "../hooks/useTheme";
27
import { resolveMarkdownFileLinkTarget } from "../markdown-links";
28
+import { readNativeApi } from "../nativeApi";
29
import { preferredTerminalEditor } from "../terminal-links";
30
31
interface ChatMarkdownProps {
@@ -35,11 +41,7 @@ const highlightedCodeCache = new LRUCache<string>(
41
MAX_HIGHLIGHT_CACHE_ENTRIES,
42
MAX_HIGHLIGHT_CACHE_MEMORY_BYTES,
43
);
38
-interface InFlightHighlight {
39
- promise: Promise<string>;
40
- enableCache: () => void;
41
-}
42
-const inFlightHighlights = new Map<string, InFlightHighlight>();
44
+const highlighterPromiseCache = new Map<string, Promise<DiffsHighlighter>>();
45
46
function extractFenceLanguage(className: string | undefined): string {
47
const match = className?.match(CODE_FENCE_LANGUAGE_REGEX);
@@ -81,85 +83,33 @@ function extractCodeBlock(
83
};
84
}
85
84
-async function highlightCodeBlock(code: string, language: string, themeName: DiffThemeName) {
85
- try {
86
- const highlighter = await getSharedHighlighter({
87
- themes: [themeName],
88
- langs: [language],
89
- });
90
- return highlighter.codeToHtml(code, { lang: language, theme: themeName });
91
- } catch {
92
- const highlighter = await getSharedHighlighter({
93
- themes: [themeName],
94
- langs: ["text"],
95
- });
96
- return highlighter.codeToHtml(code, { lang: "text", theme: themeName });
97
- }
98
-}
99
-
86
function createHighlightCacheKey(code: string, language: string, themeName: DiffThemeName): string {
87
return `${fnv1a32(code).toString(36)}:${code.length}:${language}:${themeName}`;
88
}
89
90
+function createHighlighterPromiseKey(language: string, themeName: DiffThemeName): string {
91
+ return `${language}:${themeName}`;
92
+}
93
+
94
function estimateHighlightedSize(html: string, code: string): number {
95
return Math.max(html.length * 2, code.length * 3);
96
}
97
108
-function getCachedOrCreateHighlight({
109
- cacheKey,
110
- code,
111
- language,
112
- themeName,
113
- useCache,
114
-}: {
115
- cacheKey: string;
116
- code: string;
117
- language: string;
118
- themeName: DiffThemeName;
119
- useCache: boolean;
120
-}): Promise<string> {
121
- if (useCache) {
122
- const cached = highlightedCodeCache.get(cacheKey);
123
- if (cached) {
124
- return Promise.resolve(cached);
125
- }
126
- }
127
-
128
- const existingInFlight = inFlightHighlights.get(cacheKey);
129
- if (existingInFlight) {
130
- if (useCache) {
131
- existingInFlight.enableCache();
132
- }
133
- return existingInFlight.promise;
134
- }
98
+function getHighlighterPromise(
99
+ language: string,
100
+ themeName: DiffThemeName,
101
+): Promise<DiffsHighlighter> {
102
+ const cacheKey = createHighlighterPromiseKey(language, themeName);
103
+ const cached = highlighterPromiseCache.get(cacheKey);
104
+ if (cached) return cached;
105
136
- let shouldCache = useCache;
137
- const requestPromise = highlightCodeBlock(code, language, themeName)
138
- .then((html) => {
139
- if (shouldCache) {
140
- highlightedCodeCache.set(cacheKey, html, estimateHighlightedSize(html, code));
141
- }
142
- return html;
143
- })
144
- .finally(() => {
145
- inFlightHighlights.delete(cacheKey);
146
- });
147
-
148
- inFlightHighlights.set(cacheKey, {
149
- promise: requestPromise,
150
- enableCache: () => {
151
- shouldCache = true;
152
- },
106
+ const promise = getSharedHighlighter({
107
+ themes: [themeName],
108
+ langs: [language as SupportedLanguages],
109
+ preferredHighlighter: "shiki-js",
110
});
154
- return requestPromise;
155
-}
156
-
157
-interface ShikiCodeBlockProps {
158
- className: string | undefined;
159
- code: string;
160
- themeName: DiffThemeName;
161
- isStreaming: boolean;
162
- fallback: ReactNode;
111
+ highlighterPromiseCache.set(cacheKey, promise);
112
+ return promise;
113
}
114
115
function MarkdownCodeBlock({ code, children }: { code: string; children: ReactNode }) {
@@ -210,70 +160,47 @@ function MarkdownCodeBlock({ code, children }: { code: string; children: ReactNo
160
);
161
}
162
213
-function ShikiCodeBlock({ className, code, themeName, isStreaming, fallback }: ShikiCodeBlockProps) {
163
+interface SuspenseShikiCodeBlockProps {
164
+ className: string | undefined;
165
+ code: string;
166
+ themeName: DiffThemeName;
167
+ isStreaming: boolean;
168
+}
169
+
170
+function SuspenseShikiCodeBlock({
171
+ className,
172
+ code,
173
+ themeName,
174
+ isStreaming,
175
+}: SuspenseShikiCodeBlockProps) {
176
const language = extractFenceLanguage(className);
177
const cacheKey = createHighlightCacheKey(code, language, themeName);
216
- const [highlightState, setHighlightState] = useState<{
217
- cacheKey: string;
218
- html: string | null;
219
- }>(() => ({
220
- cacheKey,
221
- html: isStreaming ? null : (highlightedCodeCache.get(cacheKey) ?? null),
222
- }));
223
- const cachedHighlightedHtml = isStreaming ? null : (highlightedCodeCache.get(cacheKey) ?? null);
224
- const highlightedHtml =
225
- cachedHighlightedHtml ?? (highlightState.cacheKey === cacheKey ? highlightState.html : null);
226
-
227
- useEffect(() => {
228
- let cancelled = false;
178
+ const cachedHighlightedHtml = !isStreaming ? highlightedCodeCache.get(cacheKey) : null;
179
230
- if (cachedHighlightedHtml) {
231
- setHighlightState((current) => {
232
- if (current.cacheKey === cacheKey && current.html === cachedHighlightedHtml) {
233
- return current;
234
- }
235
- return { cacheKey, html: cachedHighlightedHtml };
236
- });
237
- return () => {
238
- cancelled = true;
239
- };
240
- }
241
-
242
- setHighlightState((current) => {
243
- if (current.cacheKey === cacheKey) {
244
- return current;
245
- }
246
- return { cacheKey, html: null };
247
- });
248
-
249
- void getCachedOrCreateHighlight({
250
- cacheKey,
251
- code,
252
- language,
253
- themeName,
254
- useCache: !isStreaming,
255
- })
256
- .then((html) => {
257
- if (cancelled) {
258
- return;
259
- }
260
- setHighlightState((current) => {
261
- if (current.cacheKey === cacheKey && current.html === html) {
262
- return current;
263
- }
264
- return { cacheKey, html };
265
- });
266
- })
267
- .catch(() => undefined);
180
+ if (cachedHighlightedHtml != null) {
181
+ return (
182
+ <div
183
+ className="chat-markdown-shiki"
184
+ dangerouslySetInnerHTML={{ __html: cachedHighlightedHtml }}
185
+ />
186
+ );
187
+ }
188
269
- return () => {
270
- cancelled = true;
271
- };
272
- }, [cacheKey, cachedHighlightedHtml, code, isStreaming, language, themeName]);
189
+ const highlighter = use(getHighlighterPromise(language, themeName));
190
+ const highlightedHtml = useMemo(
191
+ () => highlighter.codeToHtml(code, { lang: language, theme: themeName }),
192
+ [code, highlighter, language, themeName],
193
+ );
194
274
- if (!highlightedHtml) {
275
- return fallback;
276
- }
195
+ useEffect(() => {
196
+ if (!isStreaming) {
197
+ highlightedCodeCache.set(
198
+ cacheKey,
199
+ highlightedHtml,
200
+ estimateHighlightedSize(highlightedHtml, code),
201
+ );
202
+ }
203
+ }, [cacheKey, code, highlightedHtml, isStreaming]);
204
205
return (
206
<div className="chat-markdown-shiki" dangerouslySetInnerHTML={{ __html: highlightedHtml }} />
@@ -316,13 +243,14 @@ function ChatMarkdown({ text, cwd, isStreaming = false }: ChatMarkdownProps) {
243
244
return (
245
<MarkdownCodeBlock code={codeBlock.code}>
319
- <ShikiCodeBlock
320
- className={codeBlock.className}
321
- code={codeBlock.code}
322
- themeName={diffThemeName}
323
- isStreaming={isStreaming}
324
- fallback={<pre {...props}>{children}</pre>}
325
- />
246
+ <Suspense fallback={<pre {...props}>{children}</pre>}>
247
+ <SuspenseShikiCodeBlock
248
+ className={codeBlock.className}
249
+ code={codeBlock.code}
250
+ themeName={diffThemeName}
251
+ isStreaming={isStreaming}
252
+ />
253
+ </Suspense>
254
</MarkdownCodeBlock>
255
);
256
},