Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(import): preserve OneNote superscript/subscript and strikethrough
Elian Doran7e97f8772680
diff --git a/apps/server/src/services/import/onenote/converter.spec.ts b/apps/server/src/services/import/onenote/converter.spec.tsindex f7ea46a60b..864e1df531 100644--- a/apps/server/src/services/import/onenote/converter.spec.ts+++ b/apps/server/src/services/import/onenote/converter.spec.ts@@ -61,6 +61,16 @@ const FORMATTING_SAMPLE = `<html lang="en-US"></body></html>`;+// Real OneNote source (debug-captured): subscript/superscript are real <sub>/<sup> tags, while+// strikethrough is carried as a text-decoration inline style.+const SUPERSCRIPT_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">+ <p style="margin-top:0pt;margin-bottom:0pt">Normal <sub>sub</sub> <sup>sup</sup> <span style="text-decoration:line-through">strikethrough</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", () => {@@ -107,4 +117,11 @@ describe("convertPageHtml", () => {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>");});++ it("preserves superscript/subscript and converts strikethrough", () => {+ const out = converter.convertPageHtml(SUPERSCRIPT_SAMPLE);+ expect(out).toContain("<sub>sub</sub>");+ expect(out).toContain("<sup>sup</sup>");+ expect(out).toContain("<del>strikethrough</del>");+ });});diff --git a/apps/server/src/services/import/onenote/converter.ts b/apps/server/src/services/import/onenote/converter.tsindex 2a3823e190..0dae73230e 100644--- a/apps/server/src/services/import/onenote/converter.ts+++ b/apps/server/src/services/import/onenote/converter.ts@@ -45,9 +45,10 @@ 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.+ * OneNote carries bold/italic/underline/strikethrough as inline styles (`font-weight:bold`,+ * `font-style:italic`, `text-decoration:underline`/`line-through`), 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]")) {@@ -67,6 +68,11 @@ function convertInlineFormatting(scope: HTMLElement) {open.push("<u>");close.unshift("</u>");}+ // Trilium's editor represents strikethrough as <del> (see ckeditor5 StrikethroughAsDel), not <s>.+ if (/text-decoration\s*:[^;]*line-through/i.test(style)) {+ open.push("<del>");+ close.unshift("</del>");+ }if (open.length > 0) {el.set_content(`${open.join("")}${el.innerHTML}${close.join("")}`);