Skip to content

Build your personal knowledge base with Trilium Notes

feat(office): strip default hyperlink colors from the preview

Elian Doran2ac7953fa7b9
diff --git a/apps/client/src/services/office_renderer.spec.ts b/apps/client/src/services/office_renderer.spec.ts
index ce0ad3d79e..1026ae9a39 100644
--- a/apps/client/src/services/office_renderer.spec.ts
+++ b/apps/client/src/services/office_renderer.spec.ts
@@ -25,6 +25,36 @@ describe("renderOfficeToHtml", () => {
expect(serverGet).toHaveBeenCalledWith("attachments/a9/office-preview");
});
+ it("strips default hyperlink colors so the theme's link color applies, keeping other styling", async () => {
+ serverGet.mockResolvedValueOnce({
+ html: '<p><a href="https://example.com"><span style="color: #000080; font-family: Arial"><u>x</u></span></a>'
+ + '<a href="#b" style="color: #0563C1">y</a>'
+ + '<span style="color: #000080">keep</span></p>'
+ });
+
+ const html = await renderOfficeToHtml("notes", "n1");
+
+ // The default hyperlink character style's color is gone (both on runs inside the
+ // link and on the <a> itself)...
+ expect(html).not.toContain("#0563C1");
+ // ...while the rest of the run styling and colors outside links survive.
+ expect(html).toContain("font-family: Arial");
+ expect(html).toContain("<u>x</u>");
+ expect(html).toContain('<span style="color: #000080">keep</span>');
+ // An emptied style attribute is dropped entirely.
+ expect(html).toContain('<a href="#b">y</a>');
+ });
+
+ it("keeps a link color the author chose, as text notes do", async () => {
+ serverGet.mockResolvedValueOnce({
+ html: '<p><a href="https://triliumnotes.org/"><span style="color:#ea7500;">Trilium</span></a></p>'
+ });
+
+ const html = await renderOfficeToHtml("notes", "n1");
+
+ expect(html).toContain("#ea7500");
+ });
+
it("propagates a failed request and never sanitizes", async () => {
serverGet.mockRejectedValueOnce(new Error("500: conversion failed"));
await expect(renderOfficeToHtml("notes", "n1")).rejects.toThrow(/conversion failed/);
diff --git a/apps/client/src/services/office_renderer.ts b/apps/client/src/services/office_renderer.ts
index 803567756d..302eee8850 100644
--- a/apps/client/src/services/office_renderer.ts
+++ b/apps/client/src/services/office_renderer.ts
@@ -12,5 +12,40 @@ import server from "./server.js";
export async function renderOfficeToHtml(entityType: "notes" | "attachments", entityId: string): Promise<string> {
const { html } = await server.get<{ html: string }>(`${entityType}/${entityId}/office-preview`);
- return sanitizeNoteContentHtml(html);
+ return stripLinkColors(sanitizeNoteContentHtml(html));
}
+
+/**
+ * Removes the inline `color` from hyperlinks (and the styled runs inside them) when it is a
+ * word processor's default hyperlink color rather than an author's choice. The conversion
+ * reproduces the document's hyperlink character style (e.g. LibreOffice's navy "Internet
+ * Link"), which would override the theme's link color and can be unreadable on a dark theme —
+ * while a link the author deliberately colored keeps its color, matching how colored links
+ * behave in text notes. Other run styling (underline, fonts, colors outside links) is kept.
+ */
+function stripLinkColors(html: string): string {
+ const template = document.createElement("template");
+ template.innerHTML = html;
+
+ for (const el of template.content.querySelectorAll<HTMLElement>("a[style], a [style]")) {
+ const color = el.style.getPropertyValue("color").toLowerCase().replaceAll(" ", "");
+ if (!DEFAULT_HYPERLINK_COLORS.has(color)) {
+ continue;
+ }
+
+ el.style.removeProperty("color");
+ if (!el.getAttribute("style")) {
+ el.removeAttribute("style");
+ }
+ }
+
+ return template.innerHTML;
+}
+
+/** Each default in both hex and the rgb() form CSSStyleDeclaration may serialize it to. */
+const DEFAULT_HYPERLINK_COLORS = new Set([
+ "#000080", "rgb(0,0,128)", // LibreOffice "Internet Link"
+ "#0563c1", "rgb(5,99,193)", // Word (Office theme)
+ "#0000ff", "rgb(0,0,255)", // classic hyperlink blue
+ "#0000ee", "rgb(0,0,238)" // HTML user-agent default
+]);