Skip to content

Build your personal knowledge base with Trilium Notes

feat(import): preserve OneNote bold/italic/underline on import

Elian Doran398cb6206cf6
diff --git a/apps/server/src/services/import/onenote/converter.spec.ts b/apps/server/src/services/import/onenote/converter.spec.ts
index 25c43ee4b6..f7ea46a60b 100644
--- a/apps/server/src/services/import/onenote/converter.spec.ts
+++ b/apps/server/src/services/import/onenote/converter.spec.ts
@@ -1,8 +1,16 @@
+import { SANITIZER_DEFAULT_ALLOWED_TAGS } from "@triliumnext/commons";
+import { cls, options as optionService } from "@triliumnext/core";
import { parse } from "node-html-parser";
-import { describe, expect, it } from "vitest";
+import { beforeAll, describe, expect, it } from "vitest";
import converter from "./converter.js";
+beforeAll(() => {
+ // The shared fixture DB predates `<u>` being allowlisted; align the sanitizer's allowed tags with
+ // the current default so the test asserts the end result a fresh install would store.
+ cls.init(() => optionService.setOption("allowedHtmlTags", JSON.stringify(SANITIZER_DEFAULT_ALLOWED_TAGS)));
+});
+
// A representative OneNote page as returned by the Graph API: margin-0 paragraphs, a numbered and
// two bulleted lists, a trailing empty list item, and bare block-level <br> elements that OneNote
// uses for vertical spacing between blocks.
@@ -37,6 +45,22 @@ const SAMPLE = `<html lang="en-US">
</body>
</html>`;
+// Real OneNote source (debug-captured): bold/italic/underline are carried as inline styles on
+// <span>, individually and combined on a single span.
+const FORMATTING_SAMPLE = `<html lang="en-US">
+ <body data-absolute-enabled="true" style="font-family:Calibri;font-size:11pt">
+ <div style="position:absolute;left:54px;top:134px;width:624px">
+ <p style="margin-top:0pt;margin-bottom:0pt">Normal text.</p>
+ <p style="margin-top:0pt;margin-bottom:0pt"><span style="font-weight:bold">Bold text.</span></p>
+ <p style="margin-top:0pt;margin-bottom:0pt"><span style="font-style:italic">Italic text.</span></p>
+ <p style="margin-top:0pt;margin-bottom:0pt"><span style="text-decoration:underline">Under line text.</span></p>
+ <p style="margin-top:0pt;margin-bottom:0pt"><span style="font-weight:bold;font-style:italic">Bold and italic text.</span></p>
+ <p style="margin-top:0pt;margin-bottom:0pt"><span style="font-weight:bold;text-decoration:underline">Bold and underline text</span></p>
+ <p style="margin-top:0pt;margin-bottom:0pt"><span style="font-weight:bold;font-style:italic;text-decoration:underline">Bold, italic and underline text</span></p>
+ </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", () => {
@@ -73,4 +97,14 @@ describe("convertPageHtml", () => {
expect(out).toContain("[x] done");
expect(out).toContain("[ ] todo");
});
+
+ it("converts inline-style formatting (bold/italic/underline) to semantic tags", () => {
+ const out = converter.convertPageHtml(FORMATTING_SAMPLE);
+ expect(out).toContain("<strong>Bold text.</strong>");
+ expect(out).toContain("<em>Italic text.</em>");
+ expect(out).toContain("<u>Under line text.</u>");
+ expect(out).toContain("<strong><em>Bold and italic text.</em></strong>");
+ expect(out).toContain("<strong><u>Bold and underline text</u></strong>");
+ expect(out).toContain("<strong><em><u>Bold, italic and underline text</u></em></strong>");
+ });
});
diff --git a/apps/server/src/services/import/onenote/converter.ts b/apps/server/src/services/import/onenote/converter.ts
index 8999f78c07..2a3823e190 100644
--- a/apps/server/src/services/import/onenote/converter.ts
+++ b/apps/server/src/services/import/onenote/converter.ts
@@ -26,6 +26,7 @@ export function convertPageHtml(rawHtml: string): string {
const scope = root.querySelector("body") ?? root;
convertTodoTags(scope);
+ convertInlineFormatting(scope);
removeEmptyListItems(scope);
removeBlockLevelBreaks(scope);
@@ -43,6 +44,36 @@ function convertTodoTags(scope: HTMLElement) {
}
}
+/**
+ * OneNote carries bold/italic/underline as inline styles (`font-weight:bold`, `font-style:italic`,
+ * `text-decoration:underline`), which the sanitizer strips. Wrap the styled element's content in the
+ * equivalent semantic tags (which survive sanitization) so the formatting is preserved.
+ */
+function convertInlineFormatting(scope: HTMLElement) {
+ for (const el of scope.querySelectorAll("[style]")) {
+ const style = el.getAttribute("style") ?? "";
+ const open: string[] = [];
+ const close: string[] = [];
+
+ if (/font-weight\s*:\s*bold/i.test(style)) {
+ open.push("<strong>");
+ close.unshift("</strong>");
+ }
+ if (/font-style\s*:\s*italic/i.test(style)) {
+ open.push("<em>");
+ close.unshift("</em>");
+ }
+ if (/text-decoration\s*:[^;]*underline/i.test(style)) {
+ open.push("<u>");
+ close.unshift("</u>");
+ }
+
+ if (open.length > 0) {
+ el.set_content(`${open.join("")}${el.innerHTML}${close.join("")}`);
+ }
+ }
+}
+
/** 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")) {