Skip to content

Build your personal knowledge base with Trilium Notes

feat(import): preserve OneNote table borders, including hidden borders

Elian Doran450206a71945
diff --git a/apps/server/src/services/import/onenote/converter.spec.ts b/apps/server/src/services/import/onenote/converter.spec.ts
index 5568c1ce06..6131128847 100644
--- a/apps/server/src/services/import/onenote/converter.spec.ts
+++ b/apps/server/src/services/import/onenote/converter.spec.ts
@@ -191,6 +191,27 @@ const LISTS_SAMPLE = `<html lang="en-US">
</body>
</html>`;
+// Real OneNote source (debug-captured): a table with visible borders and one with hidden borders
+// (border:0px). OneNote carries borders as a `border` shorthand + border-collapse.
+const TABLE_SAMPLE = `<html lang="en-US">
+ <body data-absolute-enabled="true" style="font-family:Calibri;font-size:11pt">
+ <div style="position:absolute;left:48px;top:115px;width:624px">
+ <table style="border:1px solid;border-collapse:collapse">
+ <tr><td style="background-color:#d8d8d8;border:1px solid">A</td><td style="border:1px solid">B</td></tr>
+ <tr><td style="border:1px solid">1</td><td style="border:1px solid">Content</td></tr>
+ </table>
+ </div>
+ <div style="position:absolute;left:211px;top:382px;width:624px">
+ <p style="margin-top:0pt;margin-bottom:0pt">Hidden borders</p>
+ <br />
+ <table style="border:0px">
+ <tr><td style="background-color:#d8d8d8;border:0px">A</td><td style="border:0px">B</td></tr>
+ <tr><td style="border:0px">1</td><td style="border:0px">Content</td></tr>
+ </table>
+ </div>
+ </body>
+</html>`;
+
// Tests assert the end result (the HTML actually stored on the note, i.e. after sanitization).
describe("convertPageHtml", () => {
it("strips OneNote's block-level <br> spacing and empty list items, keeping real content", () => {
@@ -252,6 +273,30 @@ describe("convertPageHtml", () => {
expect(out).toContain("Normal bullet");
});
+ it("drops visible table borders and maps OneNote's hidden (0px) borders to transparent", () => {
+ const out = converter.convertPageHtml(TABLE_SAMPLE);
+ const root = parse(out);
+ const tables = root.querySelectorAll("table");
+
+ // Visible-border table: border shorthand / collapse dropped (CKEditor draws default borders).
+ expect(tables[0].getAttribute("style") ?? "").not.toContain("border");
+ // Hidden-border table: border:0px -> transparent border.
+ expect(tables[1].getAttribute("style")).toContain("border-color:transparent");
+ expect(tables[1].getAttribute("style")).toContain("border-style:solid");
+
+ const visibleCells = tables[0].querySelectorAll("td");
+ expect(visibleCells[0].getAttribute("style")).toContain("background-color:#d8d8d8");
+ expect(visibleCells[0].getAttribute("style") ?? "").not.toContain("border");
+
+ const hiddenCells = tables[1].querySelectorAll("td");
+ expect(hiddenCells[0].getAttribute("style")).toContain("background-color:#d8d8d8");
+ expect(hiddenCells[0].getAttribute("style")).toContain("border-color:transparent");
+ expect(hiddenCells[1].getAttribute("style")).toContain("border-color:transparent");
+
+ expect(out).not.toContain("border:0px");
+ expect(out).not.toContain("border-collapse");
+ });
+
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 af4e06e09c..48df9326d9 100644
--- a/apps/server/src/services/import/onenote/converter.ts
+++ b/apps/server/src/services/import/onenote/converter.ts
@@ -48,6 +48,7 @@ export function convertPageHtml(rawHtml: string): string {
normalizeNamedColors(scope);
unwrapListItemParagraphs(scope);
normalizeListMarkers(scope);
+normalizeTableBorders(scope);
removeEmptyListItems(scope);
removeBlockLevelBreaks(scope);
@@ -269,6 +270,34 @@ function normalizeListMarkers(scope: HTMLElement) {
}
}
+/**
+ * OneNote carries cell/table borders as a `border` shorthand and `border-collapse`, which the
+ * sanitizer drops. Visible borders are left to CKEditor's default table styling, but a zero-width /
+ * none border must be made explicit, so map `border:0px` to `border-color:transparent` (the CKEditor
+ * representation of a hidden border), keeping any background-color.
+ */
+function normalizeTableBorders(scope: HTMLElement) {
+ for (const el of scope.querySelectorAll("table, td, th")) {
+ const style = parseStyle(el.getAttribute("style") ?? "");
+ const border = style.get("border");
+ style.delete("border");
+ style.delete("border-collapse");
+
+ if (border !== undefined && /^0(px|pt|em)?$|\bnone\b|\bhidden\b/.test(border)) {
+ style.set("border-color", "transparent");
+ if (el.tagName?.toLowerCase() === "table") {
+ style.set("border-style", "solid");
+ }
+ }
+
+ if (style.size === 0) {
+ el.removeAttribute("style");
+ } else {
+ el.setAttribute("style", serializeStyle(style));
+ }
+ }
+}
+
/** 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")) {
diff --git a/packages/trilium-core/src/services/sanitizer.ts b/packages/trilium-core/src/services/sanitizer.ts
index ca62f83df6..0a4af492b1 100644
--- a/packages/trilium-core/src/services/sanitizer.ts
+++ b/packages/trilium-core/src/services/sanitizer.ts
@@ -72,10 +72,11 @@ export function sanitizeHtml(dirtyHtml: string) {
height: sizeRegex
},
table: {
- "border-color": colorRegex,
+ "border-color": [...colorRegex, /^\s*transparent\s*$/],
"border-style": [/^\s*(none|hidden|dotted|dashed|solid|double|groove|ridge|inset|outset)\s*$/]
},
td: {
+ "border-color": [...colorRegex, /^\s*transparent\s*$/],
border: [
/^\s*\d+(?:px|em|%)\s*(none|hidden|dotted|dashed|solid|double|groove|ridge|inset|outset)\s*(#(0x)?[0-9a-fA-F]+|rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)|hsl\(\s*(\d{1,3})\s*,\s*(\d{1,3})%\s*,\s*(\d{1,3})%\))\s*$/
]