Skip to content
Source
Sign inSign up
pingdotgg/t3code

perf(web): preserve completed code-line DOM while streaming #11198

Mergedjuliusmarmingemerged 2 commits intolegend-perf/streaming-highlighting fromrefs/pull/11198/headmerged by juliusmarminge View on GitHub

Showing 8 changed files with 101 additions and 4 deletions (loading 4).

x
0 of 8 viewed, 8 left
26 additions and 0 deletionsapps/web/src/components/chat/HighlightedCodeLines.test.tsxAdded
View file
@@ -0,0 +1,26 @@
+import { getSharedHighlighter } from "@pierre/diffs";
+import { toHtml } from "hast-util-to-html";
+import { renderToStaticMarkup } from "react-dom/server";
+import { describe, expect, it } from "vite-plus/test";
+​
+import { createIncrementalHighlightedDocument } from "../../lib/incrementalHighlighting";
+import { HighlightedCodeLines } from "./HighlightedCodeLines";
+​
+describe("highlighted code lines", () => {
+ it("preserves Shiki HTML, including colors, escaping, whitespace, and blank lines", async () => {
+ const highlighter = await getSharedHighlighter({
+ langs: ["typescript"],
+ themes: ["pierre-dark", "pierre-light"],
+ preferredHighlighter: "shiki-wasm",
+ });
+ for (const theme of ["pierre-dark", "pierre-light"] as const) {
+ const highlight = createIncrementalHighlightedDocument(highlighter, "typescript", theme);
+ const code =
+ 'const html = "<img src=x onerror=alert(1)>";\n\n/* multi\nline */\n\tconst x = 1;\n';
+ for (let end = 0; end <= code.length; end++) {
+ const root = highlight(code.slice(0, end));
+ expect(renderToStaticMarkup(<HighlightedCodeLines root={root} />)).toBe(toHtml(root));
+ }
+ }
+ });
+});
48 additions and 0 deletionsapps/web/src/components/chat/HighlightedCodeLines.tsxAdded
View file
@@ -0,0 +1,48 @@
+import type { DiffsHighlighter } from "@pierre/diffs";