Skip to content

Build your personal knowledge base with Trilium Notes

perf(office): skip the link-color pass where it cannot match

Elian Doran7c4b5a09fb71
diff --git a/apps/client/src/services/office_renderer.spec.ts b/apps/client/src/services/office_renderer.spec.ts
index 02098b8b2e..dfe7f447c0 100644
--- a/apps/client/src/services/office_renderer.spec.ts
+++ b/apps/client/src/services/office_renderer.spec.ts
@@ -85,4 +85,37 @@ describe("renderOfficeToHtml", () => {
expect(truncated.css).toBe("");
expect(sanitizeNoteContentHtml).toHaveBeenCalledWith("<style>.a{b:c}");
});
+
+ it("skips the link pass for a spreadsheet, which cannot carry a styled link", async () => {
+ // A spreadsheet's anchors are written bare, and the fragment arrives with a stylesheet;
+ // running the pass would parse and re-serialize the whole grid to find nothing.
+ serverGet.mockResolvedValueOnce(
+ '<style>\n.spreadsheet-table .sst-1{text-align:right}\n</style>\n'
+ + '<table class="spreadsheet-table"><td><a href="https://x.test">y</a></td></table>');
+
+ const { html } = await renderOfficeToHtml("notes", "n1");
+
+ // Untouched by the pass: what the sanitizer returned is what comes back.
+ expect(html).toBe('SANITIZED:<table class="spreadsheet-table"><td><a href="https://x.test">y</a></td></table>');
+ });
+
+ it("skips the link pass for a fragment holding no link at all", async () => {
+ serverGet.mockResolvedValueOnce('<div class="container"><p style="color: #000080">not a link</p></div>');
+
+ const { html } = await renderOfficeToHtml("notes", "n1");
+
+ expect(html).toBe('SANITIZED:<div class="container"><p style="color: #000080">not a link</p></div>');
+ });
+
+ it("still runs the link pass for a converted document that has one", async () => {
+ // No stylesheet, and an anchor: the officeparser path, where the word processor's own
+ // hyperlink color is what has to go.
+ serverGet.mockResolvedValueOnce(
+ '<div class="container"><a href="#a" style="color: #000080">x</a></div>');
+
+ const { html } = await renderOfficeToHtml("notes", "n1");
+
+ expect(html).not.toContain("#000080");
+ expect(html).toContain('<a href="#a">x</a>');
+ });
});
diff --git a/apps/client/src/services/office_renderer.ts b/apps/client/src/services/office_renderer.ts
index 1a8db14103..bc6ea2a88c 100644
--- a/apps/client/src/services/office_renderer.ts
+++ b/apps/client/src/services/office_renderer.ts
@@ -21,8 +21,15 @@ export async function renderOfficeToHtml(entityType: "notes" | "attachments", en
// `raw` keeps the response a plain string; the route sends the fragment as the body.
const body = await server.get<string>(`${entityType}/${entityId}/office-preview`, undefined, true);
const { css, html } = splitStylesheet(body);
+ const sanitized = sanitizeNoteContentHtml(html);
- return { css, html: stripLinkColors(sanitizeNoteContentHtml(html)) };
+ // stripLinkColors parses and re-serializes the whole fragment, which on a spreadsheet is
+ // megabytes, so it only runs where it can find something. A stylesheet marks the native
+ // spreadsheet renderer, the only pipeline that emits one and one that writes anchors with no
+ // styling at all; and a fragment holding no anchor has nothing to strip either way.
+ const strippable = !css && /<a[\s>]/.test(sanitized);
+
+ return { css, html: strippable ? stripLinkColors(sanitized) : sanitized };
}
export interface OfficePreview {