Skip to content

Build your personal knowledge base with Trilium Notes

feat(onenote): correct over-darkened table cell shading on import

Elian Doran0523cda648b1
diff --git a/apps/server/src/services/import/onenote/converter.spec.ts b/apps/server/src/services/import/onenote/converter.spec.ts
index 12db599f7b..d1ea8f8ea9 100644
--- a/apps/server/src/services/import/onenote/converter.spec.ts
+++ b/apps/server/src/services/import/onenote/converter.spec.ts
@@ -440,6 +440,24 @@ describe("convertPageHtml", () => {
expect(parse(converter.convertPageHtml(sample)).querySelector("colgroup")).toBeFalsy();
});
+ it("recovers OneNote's over-darkened table cell shading by reflecting its lightness", () => {
+ // OneNote's Graph export returns cell shading as a dark shade of the colour it displays (same
+ // hue and saturation, inverted lightness): a cell shown as #b6d9a1 comes back as #375623.
+ // Reflecting the lightness recovers the light tint (#bddca9, a barely-perceptible delta off).
+ const sample = `<body><div><table><tr><td style="background-color:#375623"><span lang="en-US">A</span></td><td>B</td></tr></table></div></body>`;
+ const out = converter.convertPageHtml(sample);
+
+ expect(out).toContain("background-color:#bddca9");
+ expect(out).not.toContain("#375623");
+ });
+
+ it("leaves an already-light table cell shading untouched", () => {
+ // A light background is plausible shading as-is (and correctly-exported colours arrive light),
+ // so only dark cell backgrounds are reflected.
+ const sample = `<body><div><table><tr><td style="background-color:#b6d9a1">A</td><td>B</td></tr></table></div></body>`;
+ expect(converter.convertPageHtml(sample)).toContain("background-color:#b6d9a1");
+ });
+
it("converts OneNote to-do tags into a Trilium task list", () => {
const out = converter.convertPageHtml(TAGS_SAMPLE);
const root = parse(out);
diff --git a/apps/server/src/services/import/onenote/converter.ts b/apps/server/src/services/import/onenote/converter.ts
index cb4080b043..5986b0a73a 100644
--- a/apps/server/src/services/import/onenote/converter.ts
+++ b/apps/server/src/services/import/onenote/converter.ts
@@ -115,6 +115,7 @@ export function convertPageHtml(rawHtml: string): string {
unwrapListItemParagraphs(scope);
normalizeListMarkers(scope);
normalizeTableBorders(scope);
+ correctTableShadingColors(scope);
removeEmptyListItems(scope);
removeBlockLevelBreaks(scope);
resizeTables(scope);
@@ -606,6 +607,98 @@ function columnPercentages(widths: number[]): number[] {
});
}
+/**
+ * OneNote's Graph API exports table cell shading as a dark *shade* of the colour OneNote actually
+ * displays: it keeps the hue and saturation but inverts the lightness (e.g. a cell shown as #b6d9a1
+ * comes back as #375623). Left as-is the cell imports far too dark — commonly dark-on-dark and
+ * unreadable. So for a cell whose background is dark, reflect its lightness (L → 1 − L, hue/saturation
+ * untouched) to recover the displayed light tint; light backgrounds (plausible shading as-is, and the
+ * form correctly-exported colours arrive in) are left alone. Runs after normalizeNamedColors, so a
+ * named background has already become the hex this keys on.
+ */
+function correctTableShadingColors(scope: HTMLElement) {
+ for (const cell of scope.querySelectorAll("td, th")) {
+ const style = parseStyle(cell.getAttribute("style") ?? "");
+ const rgb = parseHexColor(style.get("background-color") ?? "");
+ if (!rgb) {
+ continue;
+ }
+ const [hue, saturation, lightness] = rgbToHsl(rgb);
+ if (lightness >= 0.5) {
+ continue;
+ }
+ style.set("background-color", hslToHex(hue, saturation, 1 - lightness));
+ cell.setAttribute("style", serializeStyle(style));
+ }
+}
+
+/** Parses a `#rgb` or `#rrggbb` colour to [r, g, b] (0-255), or null if it isn't a hex colour. */
+function parseHexColor(value: string): [number, number, number] | null {
+ const match = value.trim().match(/^#([0-9a-f]{3}|[0-9a-f]{6})$/i);
+ if (!match) {
+ return null;
+ }
+ const hex = match[1].length === 3 ? match[1].replace(/(.)/g, "$1$1") : match[1];
+ return [parseInt(hex.slice(0, 2), 16), parseInt(hex.slice(2, 4), 16), parseInt(hex.slice(4, 6), 16)];
+}
+
+/** Converts an [r, g, b] (0-255) colour to [h, s, l], each in [0, 1]. */
+function rgbToHsl([r, g, b]: [number, number, number]): [number, number, number] {
+ r /= 255;
+ g /= 255;
+ b /= 255;
+ const max = Math.max(r, g, b);
+ const min = Math.min(r, g, b);
+ const lightness = (max + min) / 2;
+ const delta = max - min;
+ if (delta === 0) {
+ return [0, 0, lightness];
+ }
+ const saturation = lightness > 0.5 ? delta / (2 - max - min) : delta / (max + min);
+ let hue: number;
+ switch (max) {
+ case r:
+ hue = (g - b) / delta + (g < b ? 6 : 0);
+ break;
+ case g:
+ hue = (b - r) / delta + 2;
+ break;
+ default:
+ hue = (r - g) / delta + 4;
+ break;
+ }
+ return [hue / 6, saturation, lightness];
+}
+
+/** Converts [h, s, l] (each in [0, 1]) back to a `#rrggbb` hex colour. */
+function hslToHex(h: number, s: number, l: number): string {
+ const channel = (value: number) => Math.round(value * 255).toString(16).padStart(2, "0");
+ if (s === 0) {
+ return `#${channel(l).repeat(3)}`;
+ }
+ const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
+ const p = 2 * l - q;
+ const component = (t: number) => {
+ if (t < 0) {
+ t += 1;
+ }
+ if (t > 1) {
+ t -= 1;
+ }
+ if (t < 1 / 6) {
+ return p + (q - p) * 6 * t;
+ }
+ if (t < 1 / 2) {
+ return q;
+ }
+ if (t < 2 / 3) {
+ return p + (q - p) * (2 / 3 - t) * 6;
+ }
+ return p;
+ };
+ return `#${channel(component(h + 1 / 3))}${channel(component(h))}${channel(component(h - 1 / 3))}`;
+}
+
/** Drops list items that hold nothing but whitespace/<br> (OneNote's "exited the list" remnant). */
function removeEmptyListItems(scope: HTMLElement) {
for (const li of scope.querySelectorAll("li")) {