8000 fix(review): render large code files without highlighting by simon-inkie · Pull Request #42 · simon-inkie/inkie-deck · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions packages/deck/app/components/CodeView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ interface CodeViewProps {
lang: string;
}

// Keep large review files usable without highlighting the entire payload or
// creating a DOM node for every line. The server's 2 MB read cap is a security
// bound, not a safe client-side syntax-highlighting budget.
const MAX_HIGHLIGHT_CHARS = 128 * 1024;
const MAX_GUTTER_LINES = 4_000;

export function shouldRenderPlainCode(content: string): boolean {
if (content.length > MAX_HIGHLIGHT_CHARS) return true;

let lines = 1;
for (let i = 0; i < content.length; i++) {
if (content.charCodeAt(i) === 10 && ++lines > MAX_GUTTER_LINES) return true;
}
return false;
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -78,20 +94,40 @@ function highlightContent(content: string, lang: string): string {
* (TASK-563) can query them by line number without DOM changes here.
*/
export function CodeView({ content, lang }: CodeViewProps) {
const plain = useMemo(() => shouldRenderPlainCode(content), [content]);
const highlightedHtml = useMemo(
() => highlightContent(content, lang),
[content, lang]
() => plain ? null : highlightContent(content, lang),
[content, lang, plain]
);

// Split the highlighted HTML into per-line spans.
// highlight.js never emits raw newlines inside a span that wraps multiple
// lines for common languages, so splitting on \n is reliable. We join the
// lines as individual <span data-line> elements for future anchor support.
const lineCount = useMemo(
() => content.split("\n").length,
[content]
() => plain ? 0 : content.split("\n").length,
[content, plain]
);

if (plain) {
return (
<div
className="code-view"
style={{ background: "#1f1f1f", color: "#d4d4d4", fontFamily: "var(--font-mono)", fontSize: "0.8125rem", lineHeight: "1.55" }}
>
<div className="px-3 py-2 text-xs text-muted-foreground">
Large file: syntax highlighting and line numbers are off for responsiveness.
</div>
<pre
data-testid="large-code-plain"
style={{ margin: 0, padding: "0.75em", overflow: "auto", whiteSpace: "pre" }}
>
<code>{content}</code>
</pre>
</div>
);
}

return (
// Outer wrapper: .code-view scopes the VS Code Dark Modern hljs theme
// (see app.css). No border-radius - the full-bleed layout in ReviewPanel
Expand Down Expand Up @@ -157,7 +193,7 @@ export function CodeView({ content, lang }: CodeViewProps) {
<code
className={`hljs language-${lang}`}
style={{ background: "transparent", padding: 0, fontSize: "inherit" }}
dangerouslySetInnerHTML={{ __html: highlightedHtml }}
dangerouslySetInnerHTML={{ __html: highlightedHtml ?? "" }}
/>
</pre>
</div>
Expand Down
43 changes: 43 additions & 0 deletions packages/deck/test/code-view-large-file.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React from "react";
import { renderToStaticMarkup } from "react-dom/server";
import { describe, expect, it } from "vitest";
import { CodeView, shouldRenderPlainCode } from "~/components/CodeView";

describe("CodeView large-file path", () => {
it("switches at the character budget boundary", () => {
expect(shouldRenderPlainCode("x".repeat(128 * 1024))).toBe(false);
expect(shouldRenderPlainCode("x".repeat(128 * 1024 + 1))).toBe(true);
});

it("keeps normal files highlighted and line-numbered", () => {
const content = '{"ok":true}\n';
expect(shouldRenderPlainCode(content)).toBe(false);

const html = renderToStaticMarkup(<CodeView content={content} lang="json" />);
expect(html).toContain('class="hljs language-json"');
expect(html).toContain('aria-hidden="true"');
expect(html).not.toContain('data-testid="large-code-plain"');
});

it("skips highlighting and the gutter for a 1 MB JSON file", () => {
const content = JSON.stringify({ rows: "x".repeat(1_050_000), marker: "<script>hello</script>" });
expect(shouldRenderPlainCode(content)).toBe(true);

const html = renderToStaticMarkup(<CodeView content={content} lang="json" />);
expect(html).toContain('data-testid="large-code-plain"');
expect(html).not.toContain('aria-hidden="true"');
expect(html).not.toContain('class="hljs language-json"');
expect(html).toContain('&lt;script&gt;hello&lt;/script&gt;');
expect(html).not.toContain('<script>hello</script>');
});

it("skips the gutter for a smaller file with too many lines", () => {
const content = "line\n".repeat(4_001);
expect(content.length).toBeLessThan(128 * 1024);
expect(shouldRenderPlainCode(content)).toBe(true);

const html = renderToStaticMarkup(<CodeView content={content} lang="plaintext" />);
expect(html).toContain('data-testid="large-code-plain"');
expect(html).not.toContain('aria-hidden="true"');
});
});
Loading
0