@devries-cameron20 / t3code Mirror · read-only / commits / a24f7a1150
Fork 0

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 21, 21:00 UTC

fix(web): code blocks always show their own highlighted source (#34527)

Co-authored-by: maria-rcks <254055478+maria-rcks@users.noreply.github.com>

maria committed Nov 12, 2026 at 00:21 UTC a24f7a115000012c7f867a53eda4b21f945b5d72
3 files changed +23 -5
apps/web/src/components/ChatMarkdown.browser.test.tsx
+15
@@ -275,6 +275,21 @@ it("reuses the parse of a remounted message that has stable extra plugins", asyn
275 expect(parsed).toHaveBeenCalledTimes(1);
276 });
277
278 +it("highlights a code block's own source when an earlier block of the same length was cached", async () => {
279 + setup();
280 + const highlightedText = (container: HTMLElement) =>
281 + container.querySelector(".chat-markdown-shiki")?.textContent?.trim();
282 + // These two lines share a 32-bit FNV-1a hash and a length.
283 + const first = 'const commitSha = "f36a6d8fe0aa";';
284 + const second = 'const commitSha = "cb06f8d135b3";';
285 + {
286 + await using screen = await renderText(`\`\`\`ts\n${first}\n\`\`\``);
287 + await vi.waitFor(() => expect(highlightedText(screen.container)).toBe(first));
288 + }
289 + await using screen = await renderText(`\`\`\`ts\n${second}\n\`\`\``);
290 + await vi.waitFor(() => expect(highlightedText(screen.container)).toBe(second));
291 +});
292 +
293 describe("ChatMarkdown favicon privacy", () => {
294 it("suppresses private link images while preserving public links across updates", async () => {
295 setup();
apps/web/src/components/chat/MarkdownCodeBlock.logic.ts
+7 -4
@@ -1,7 +1,7 @@
1 import { Children, isValidElement, type ReactNode } from "react";
2 import type { ExtraProps as ReactMarkdownExtraProps } from "react-markdown";
3
4 -import { fnv1a32, type DiffThemeName } from "../../lib/diffRendering";
4 +import type { DiffThemeName } from "../../lib/diffRendering";
5
6 const CODE_FENCE_LANGUAGE_REGEX = /(?:^|\s)language-([^\s]+)/;
7
@@ -106,14 +106,17 @@ export function extractCodeBlock(
106 };
107 }
108
109 +// The key holds the code itself: a hash would let two different blocks share
110 +// highlighted HTML. The length prefix keeps a language with ":" unambiguous.
111 export function createHighlightCacheKey(
112 code: string,
113 language: string,
114 themeName: DiffThemeName,
115 ): string {
114 - return `${fnv1a32(code).toString(36)}:${code.length}:${language}:${themeName}`;
116 + return `${language.length}:${language}:${themeName}:${code}`;
117 }
118
117 -export function estimateHighlightedSize(html: string, code: string): number {
118 - return Math.max(html.length * 2, code.length * 3);
119 +/** Approximate UTF-16 bytes for a cache entry, counting its key, which holds the code. */
120 +export function estimateHighlightedSize(html: string, cacheKey: string): number {
121 + return (cacheKey.length + html.length) * 2;
122 }
apps/web/src/components/chat/MarkdownCodeBlock.tsx
+1 -1
@@ -354,7 +354,7 @@ function UncachedShikiCodeBlock({
354 highlightedCodeCache.set(
355 cacheKey,
356 highlightedHtml,
357 - estimateHighlightedSize(highlightedHtml, code),
357 + estimateHighlightedSize(highlightedHtml, cacheKey),
358 );
359 }
360 return result;