Skip to content

Commit a24f7a1

Browse files
authored
fix(web): code blocks always show their own highlighted source (#34527)
Co-authored-by: maria-rcks <[email protected]>
1 parent b18fefd commit a24f7a1

3 files changed

Lines changed: 23 additions & 5 deletions

File tree

‎apps/web/src/components/ChatMarkdown.browser.test.tsx‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,21 @@ it("reuses the parse of a remounted message that has stable extra plugins", asyn
275275
expect(parsed).toHaveBeenCalledTimes(1);
276276
});
277277

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+
278293
describe("ChatMarkdown favicon privacy", () => {
279294
it("suppresses private link images while preserving public links across updates", async () => {
280295
setup();

‎apps/web/src/components/chat/MarkdownCodeBlock.logic.ts‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Children, isValidElement, type ReactNode } from "react";
22
import type { ExtraProps as ReactMarkdownExtraProps } from "react-markdown";
33

4-
import { fnv1a32, type DiffThemeName } from "../../lib/diffRendering";
4+
import type { DiffThemeName } from "../../lib/diffRendering";
55

66
const CODE_FENCE_LANGUAGE_REGEX = /(?:^|\s)language-([^\s]+)/;
77

@@ -106,14 +106,17 @@ export function extractCodeBlock(
106106
};
107107
}
108108

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.
109111
export function createHighlightCacheKey(
110112
code: string,
111113
language: string,
112114
themeName: DiffThemeName,
113115
): string {
114-
return `${fnv1a32(code).toString(36)}:${code.length}:${language}:${themeName}`;
116+
return `${language.length}:${language}:${themeName}:${code}`;
115117
}
116118

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;
119122
}

‎apps/web/src/components/chat/MarkdownCodeBlock.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ function UncachedShikiCodeBlock({
354354
highlightedCodeCache.set(
355355
cacheKey,
356356
highlightedHtml,
357-
estimateHighlightedSize(highlightedHtml, code),
357+
estimateHighlightedSize(highlightedHtml, cacheKey),
358358
);
359359
}
360360
return result;

0 commit comments

Comments
 (0)