Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(share): also bound syntax highlighting by character count
Elian Doran22c0b146178c
diff --git a/apps/server/src/share/content_renderer.spec.ts b/apps/server/src/share/content_renderer.spec.tsindex 0426ab0dc3..45a8c00d05 100644--- a/apps/server/src/share/content_renderer.spec.ts+++ b/apps/server/src/share/content_renderer.spec.ts@@ -250,5 +250,11 @@ describe("content_renderer", () => {expect(shouldSyntaxHighlight(Array(500).fill("x").join("\n"))).toBe(true);expect(shouldSyntaxHighlight(Array(501).fill("x").join("\n"))).toBe(false);});++ it("rejects a single huge line that stays under the line limit", () => {+ // No newlines, so the line check never trips — the character ceiling must catch it.+ expect(shouldSyntaxHighlight("x".repeat(50_000))).toBe(true);+ expect(shouldSyntaxHighlight("x".repeat(50_001))).toBe(false);+ });});});diff --git a/apps/server/src/share/content_renderer.ts b/apps/server/src/share/content_renderer.tsindex fbffc150a1..7e5b0a7a9c 100644--- a/apps/server/src/share/content_renderer.ts+++ b/apps/server/src/share/content_renderer.ts@@ -30,6 +30,13 @@ const templateCache: Map<string, string> = new Map();*/const HIGHLIGHT_MAX_LINE_COUNT = 500;+/**+ * Maximum number of characters a code block may have before server-side syntax highlighting is+ * skipped. The line-count cutoff alone does not protect against a single very long line (e.g.+ * minified code), so a separate character ceiling guards `highlightAuto`'s size-driven cost.+ */+const HIGHLIGHT_MAX_CHAR_COUNT = 50_000;+/*** Represents the output of the content renderer.*/@@ -553,9 +560,13 @@ function renderMarkdown(result: Result, note: SNote | BNote) {* Whether a code block is small enough to syntax-highlight server-side. Highlighting (especially* `highlightAuto`, which probes every registered language) scales with content size and would block* the single Node event loop on very large code, so blocks beyond {@link HIGHLIGHT_MAX_LINE_COUNT}- * lines are left unhighlighted.+ * lines or {@link HIGHLIGHT_MAX_CHAR_COUNT} characters are left unhighlighted.*/export function shouldSyntaxHighlight(code: string) {+ if (code.length > HIGHLIGHT_MAX_CHAR_COUNT) {+ return false;+ }+let lineCount = 1;for (let i = 0; i < code.length; i++) {if (code.charCodeAt(i) === 10 /* \n */ && ++lineCount > HIGHLIGHT_MAX_LINE_COUNT) {