Optimize streaming markdown code blocks
Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
1 parent
3a0623f1cd
commit
56ee51fac3
2 files changed
+51
-13
No files matched your search
@@ -407,6 +407,27 @@ describe("ChatMarkdown", () => {
|
||||
});
|
||||
|
||||
describe("code block chrome", () => {
|
||||
it("renders streaming code blocks without Shiki highlighting", async () => {
|
||||
const screen = await render(
|
||||
<ChatMarkdown
|
||||
text={'```ts title="streaming.ts"\nconst value = 1;\n```'}
|
||||
cwd="/repo/project"
|
||||
isStreaming
|
||||
/>,
|
||||
);
|
||||
|
||||
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",
|
||||
|
||||
@@ -601,8 +601,12 @@ function SuspenseShikiCodeBlock({
|
||||
isStreaming,
|
||||
}: SuspenseShikiCodeBlockProps) {
|
||||
const language = extractFenceLanguage(className);
|
||||
if (isStreaming) {
|
||||
return <PlainStreamingCodeBlock className={className} code={code} />;
|
||||
}
|
||||
|
||||
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 (
|
||||
<pre>
|
||||
<code className={className}>{code}</code>
|
||||
</pre>
|
||||
);
|
||||
}
|
||||
|
||||
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 (
|
||||
<div className="chat-markdown-shiki" dangerouslySetInnerHTML={{ __html: highlightedHtml }} />
|
||||
);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
Reference in new issue
Block a user