Skip to content
Source
Sign inSign up
pingdotgg/t3code

perf(web): resume syntax highlighting from completed lines #11196

Mergedjuliusmarmingemerged 1 commit intolegend-perf/incremental-markdown fromrefs/pull/11196/headmerged by juliusmarminge View on GitHub

Showing 3 changed files with 169 additions and 2 deletions.

x
0 of 3 viewed, 3 left
9 additions and 2 deletionsapps/web/src/components/ChatMarkdown.tsxModified
View file
@@ -123,6 +123,7 @@
import { LRUCache } from "../lib/lruCache";
import { getSyntaxHighlighterPromise } from "../lib/syntaxHighlighting";
import { GitHubIcon } from "./Icons";
+import { createIncrementalHighlighter } from "../lib/incrementalHighlighting";
import { RenderErrorBoundary } from "./RenderErrorBoundary";
import { useTheme } from "../hooks/useTheme";
import { getClientSettings, useClientSettings } from "../hooks/useSettings";
@@ -1050,9 +1051,15 @@
isStreaming,
}: UncachedShikiCodeBlockProps) {
const highlighter = use(getSyntaxHighlighterPromise(language));
+ const incrementalHighlight = useMemo(
+ () => (isStreaming ? createIncrementalHighlighter(highlighter, language, themeName) : null),
+ [highlighter, isStreaming, language, themeName],
+ );
const highlightedHtml = useMemo(() => {
try {
- return highlighter.codeToHtml(code, { lang: language, theme: themeName });
+ return incrementalHighlight
+ ? incrementalHighlight(code)
+ : highlighter.codeToHtml(code, { lang: language, theme: themeName });
} catch (error) {
// Log highlighting failures for debugging while falling back to plain text
console.warn(
@@ -1062,7 +1069,7 @@
// If highlighting fails for this language, render as plain text
return highlighter.codeToHtml(code, { lang: "text", theme: themeName });