diff --git a/apps/web/src/components/ChatMarkdown.browser.tsx b/apps/web/src/components/ChatMarkdown.browser.tsx index a93a6d231f..619ea87de7 100644 --- a/apps/web/src/components/ChatMarkdown.browser.tsx +++ b/apps/web/src/components/ChatMarkdown.browser.tsx @@ -407,6 +407,27 @@ describe("ChatMarkdown", () => { }); describe("code block chrome", () => { + it("renders streaming code blocks without Shiki highlighting", async () => { + const screen = await render( + , + ); + + try { + expect(document.querySelector(".chat-markdown-shiki")).toBeNull(); + const code = document.querySelector("pre code.language-ts"); + expect(code?.textContent).toContain("const value = 1;"); + expect(document.querySelector(".chat-markdown-codeblock-title")?.textContent).toBe( + "streaming.ts", + ); + } finally { + await screen.unmount(); + } + }); + it("shows icon-only language titles, text fallbacks, and filename overrides", async () => { const source = [ "```ts", diff --git a/apps/web/src/components/ChatMarkdown.tsx b/apps/web/src/components/ChatMarkdown.tsx index 3aba45249f..37284127ba 100644 --- a/apps/web/src/components/ChatMarkdown.tsx +++ b/apps/web/src/components/ChatMarkdown.tsx @@ -601,8 +601,12 @@ function SuspenseShikiCodeBlock({ isStreaming, }: SuspenseShikiCodeBlockProps) { const language = extractFenceLanguage(className); + if (isStreaming) { + return ; + } + const cacheKey = createHighlightCacheKey(code, language, themeName); - const cachedHighlightedHtml = !isStreaming ? highlightedCodeCache.get(cacheKey) : null; + const cachedHighlightedHtml = highlightedCodeCache.get(cacheKey); if (cachedHighlightedHtml != null) { return ( @@ -619,17 +623,29 @@ function SuspenseShikiCodeBlock({ language={language} themeName={themeName} cacheKey={cacheKey} - isStreaming={isStreaming} /> ); } +function PlainStreamingCodeBlock({ + className, + code, +}: { + className: string | undefined; + code: string; +}) { + return ( +
+      {code}
+    
+ ); +} + interface UncachedShikiCodeBlockProps { code: string; language: string; themeName: DiffThemeName; cacheKey: string; - isStreaming: boolean; } function UncachedShikiCodeBlock({ @@ -637,7 +653,6 @@ function UncachedShikiCodeBlock({ language, themeName, cacheKey, - isStreaming, }: UncachedShikiCodeBlockProps) { const highlighter = use(getHighlighterPromise(language)); const highlightedHtml = useMemo(() => { @@ -654,21 +669,23 @@ function UncachedShikiCodeBlock({ } }, [code, highlighter, language, themeName]); - useEffect(() => { - if (!isStreaming) { - highlightedCodeCache.set( - cacheKey, - highlightedHtml, - estimateHighlightedSize(highlightedHtml, code), - ); - } - }, [cacheKey, code, highlightedHtml, isStreaming]); + useHighlightedCodeCache(cacheKey, highlightedHtml, code); return (
); } +function useHighlightedCodeCache(cacheKey: string, highlightedHtml: string, code: string) { + useEffect(() => { + highlightedCodeCache.set( + cacheKey, + highlightedHtml, + estimateHighlightedSize(highlightedHtml, code), + ); + }, [cacheKey, code, highlightedHtml]); +} + interface MarkdownFileLinkProps { href: string; targetPath: string;