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): render OneNote note tags as task lists and emoji prefixes
Elian Doran302d9091a0cb
diff --git a/apps/server/src/services/import/onenote/converter.spec.ts b/apps/server/src/services/import/onenote/converter.spec.tsindex 6131128847..595fa7072c 100644--- a/apps/server/src/services/import/onenote/converter.spec.ts+++ b/apps/server/src/services/import/onenote/converter.spec.ts@@ -140,6 +140,45 @@ const TAGS_SAMPLE = `<html lang="en-US"></body></html>`;+// Real OneNote source (web-OneNote Graph export): the decorative (non-checkbox) note tags. Each is a+// paragraph carrying a `data-tag` that OneNote renders as an icon — we render it as an emoji prefix.+const EMOJI_TAGS_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 data-tag="important" style="margin-top:0pt;margin-bottom:0pt">Important</p>+ <p data-tag="question" style="margin-top:0pt;margin-bottom:0pt">Question</p>+ <p data-tag="idea" style="margin-top:0pt;margin-bottom:0pt">Idea</p>+ <p data-tag="password" style="margin-top:0pt;margin-bottom:0pt">Password</p>+ <p data-tag="phone-number" style="margin-top:0pt;margin-bottom:0pt">Phone number</p>+ </div>+ </body>+</html>`;++// Real OneNote source (web-OneNote Graph export): checkbox note tags other than plain to-do+// (priorities, discussions, meetings, client requests). All render as task-list items, and the+// `:completed` status checks the box.+const CHECKBOX_TAGS_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 data-tag="to-do-priority-1" style="margin-top:0pt;margin-bottom:0pt">Priority 1</p>+ <p data-tag="schedule-meeting:completed" style="margin-top:0pt;margin-bottom:0pt">Schedule meeting</p>+ <p data-tag="discuss-with-manager" style="margin-top:0pt;margin-bottom:0pt">Discuss with Manager</p>+ <p data-tag="client-request" style="margin-top:0pt;margin-bottom:0pt">Client Request</p>+ </div>+ </body>+</html>`;++// Real OneNote source (web-OneNote Graph export): a paragraph can carry several comma-separated tags+// at once — a checkbox tag combined with decorative ones, or several decorative ones together.+const MULTI_TAGS_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 data-tag="to-do,important,question" style="margin-top:0pt;margin-bottom:0pt">Todo plus star plus question</p>+ <p data-tag="movie-to-see,book-to-read" style="margin-top:0pt;margin-bottom:0pt">Movie and book</p>+ </div>+ </body>+</html>`;+// Real OneNote source (debug-captured): a free-form page with four absolutely-positioned text boxes// whose document order differs from their visual (top-to-bottom) reading order.const ALIGNMENT_SAMPLE = `<html lang="en-US">@@ -315,6 +354,49 @@ describe("convertPageHtml", () => {expect(out).not.toContain("[x]");});+ it("treats every checkbox-style tag (priority/meeting/discussion/request) as a task item", () => {+ const out = converter.convertPageHtml(CHECKBOX_TAGS_SAMPLE);+ const root = parse(out);++ // All four checkbox-style tags collapse into a single task list with one item each.+ expect(root.querySelectorAll("ul.todo-list")).toHaveLength(1);+ expect(root.querySelectorAll(`input[type="checkbox"]`)).toHaveLength(4);+ // Only the schedule-meeting:completed item is checked.+ expect(root.querySelectorAll("input[checked]")).toHaveLength(1);+ expect(out).toContain("Client Request");+ // The raw data-tag attribute is consumed, not left on the output.+ expect(out).not.toContain("data-tag");+ });++ it("renders decorative tags as an emoji prefix, leaving the paragraph in place", () => {+ const out = converter.convertPageHtml(EMOJI_TAGS_SAMPLE);+ const root = parse(out);++ // No checkbox tags here, so nothing becomes a task list.+ expect(root.querySelectorAll("ul.todo-list")).toHaveLength(0);+ expect(root.querySelectorAll("p")).toHaveLength(5);+ expect(out).toContain("⭐ Important");+ expect(out).toContain("❓ Question");+ expect(out).toContain("💡 Idea");+ expect(out).toContain("🔑 Password");+ expect(out).toContain("📞 Phone number");+ expect(out).not.toContain("data-tag");+ });++ it("supports multiple comma-separated tags on one paragraph", () => {+ const out = converter.convertPageHtml(MULTI_TAGS_SAMPLE);+ const root = parse(out);++ // The checkbox tag turns its paragraph into a task item, prefixed with the decorative emoji.+ expect(root.querySelectorAll("ul.todo-list")).toHaveLength(1);+ expect(root.querySelectorAll(`input[type="checkbox"]`)).toHaveLength(1);+ expect(root.querySelectorAll("input[checked]")).toHaveLength(0);+ expect(out).toContain("⭐❓ Todo plus star plus question");++ // The decorative-only paragraph stays a <p>, prefixed with both emoji.+ expect(out).toContain("🎬📚 Movie and book");+ });+it("converts inline-style formatting (bold/italic/underline) to semantic tags", () => {const out = converter.convertPageHtml(FORMATTING_SAMPLE);expect(out).toContain("<strong>Bold text.</strong>");diff --git a/apps/server/src/services/import/onenote/converter.ts b/apps/server/src/services/import/onenote/converter.tsindex 48df9326d9..167a1ecf67 100644--- a/apps/server/src/services/import/onenote/converter.ts+++ b/apps/server/src/services/import/onenote/converter.ts@@ -30,6 +30,52 @@ const BLOCK_CONTAINERS = new Set(["body", "div", "section", "article", "blockquo/** OneNote uses the CSS alpha keywords; CKEditor (and the sanitizer) use the latin equivalents. */const LIST_STYLE_MAP = new Map<string, string>([["lower-alpha", "lower-latin"], ["upper-alpha", "upper-latin"]]);+/**+ * OneNote's checkbox-style note tags — the ones it renders with a tick box and that carry a+ * `:completed` status (see https://learn.microsoft.com/en-us/graph/onenote-note-tags). Each becomes a+ * CKEditor task-list item, checked when its status is `completed`. Every other built-in tag is purely+ * decorative and rendered as an emoji prefix instead (TAG_EMOJI).+ */+const CHECKBOX_TAGS = new Set<string>([+ "to-do",+ "to-do-priority-1",+ "to-do-priority-2",+ "discuss-with-person-a",+ "discuss-with-person-b",+ "discuss-with-manager",+ "schedule-meeting",+ "call-back",+ "client-request"+]);++/**+ * OneNote's decorative (non-checkbox) note tags mapped to a representative emoji, keyed on the tag+ * *shape* (the part before any `:status`). Covers the full built-in vocabulary; a shape not listed+ * here (e.g. a user's custom tag) simply renders with no prefix.+ */+const TAG_EMOJI = new Map<string, string>([+ ["important", "⭐"],+ ["critical", "❗"],+ ["question", "❓"],+ ["highlight", "🖍️"],+ ["definition", "📖"],+ ["remember-for-later", "📌"],+ ["remember-for-blog", "✍️"],+ ["idea", "💡"],+ ["password", "🔑"],+ ["contact", "👤"],+ ["address", "🏠"],+ ["phone-number", "📞"],+ ["web-site-to-visit", "🌐"],+ ["source-for-article", "📰"],+ ["send-in-email", "📧"],+ ["movie-to-see", "🎬"],+ ["book-to-read", "📚"],+ ["music-to-listen-to", "🎵"],+ ["project-a", "🅰️"],+ ["project-b", "🅱️"]+]);+/** OneNote's highlight/font palette uses the 16 basic CSS color names; map them to hex (see below). */const NAMED_COLORS = new Map<string, string>([["yellow", "#ffff00"], ["lime", "#00ff00"], ["aqua", "#00ffff"], ["fuchsia", "#ff00ff"],@@ -43,12 +89,12 @@ export function convertPageHtml(rawHtml: string): string {const scope = root.querySelector("body") ?? root;sortPositionedOutlines(scope);- convertTodoTags(scope);+ convertTags(scope);convertInlineFormatting(scope);normalizeNamedColors(scope);unwrapListItemParagraphs(scope);normalizeListMarkers(scope);-normalizeTableBorders(scope);+ normalizeTableBorders(scope);removeEmptyListItems(scope);removeBlockLevelBreaks(scope);@@ -80,19 +126,45 @@ function sortPositionedOutlines(scope: HTMLElement) {}/**- * OneNote marks checkboxes as `<p data-tag="to-do">` / `"to-do:completed"` paragraphs. Group runs of- * consecutive to-do paragraphs into a single CKEditor task list (<ul class="todo-list">…), matching- * the structure Trilium's TodoList editor plugin produces — completed items use a checked checkbox.+ * OneNote represents note tags as a `data-tag` attribute on a <p> — a comma-separated list of+ * `shape[:status]` values (e.g. `to-do:completed`, `important`, `movie-to-see,book-to-read`).+ *+ * Checkbox-style tags (CHECKBOX_TAGS — `to-do` and its priority/discussion/meeting siblings) become a+ * CKEditor task list, checked when their status is `completed`; runs of consecutive ones collapse into+ * one list, matching the structure Trilium's TodoList plugin produces. Every other tag is decorative,+ * so we prefix its paragraph with a representative emoji (TAG_EMOJI). A paragraph can carry both+ * (e.g. `to-do,important`): it becomes a task item whose text is emoji-prefixed.*/-function convertTodoTags(scope: HTMLElement) {- const todos = scope.querySelectorAll("p[data-tag]").filter((el) => {- const tag = el.getAttribute("data-tag");- return tag === "to-do" || tag === "to-do:completed";- });+function convertTags(scope: HTMLElement) {+ const tagged = scope.querySelectorAll("p[data-tag]");- // Collect adjacent to-do paragraphs so each run becomes one list.+ // Prefix each tagged paragraph's decorative-tag emoji in place, and record whether it is a+ // checkbox paragraph (and if so, whether it's completed) so adjacent ones can be grouped.+ const completedByCheckbox = new Map<HTMLElement, boolean>();+ for (const p of tagged) {+ const tags = (p.getAttribute("data-tag") ?? "").split(",").map((entry) => {+ const [shape, status] = entry.trim().split(":");+ return { shape, status };+ });+ p.removeAttribute("data-tag");++ const emojis = tags.map((tag) => TAG_EMOJI.get(tag.shape)).filter((emoji): emoji is string => Boolean(emoji));+ if (emojis.length > 0) {+ p.set_content(`${emojis.join("")} ${p.innerHTML}`);+ }++ const checkbox = tags.find((tag) => CHECKBOX_TAGS.has(tag.shape));+ if (checkbox) {+ completedByCheckbox.set(p, checkbox.status === "completed");+ }+ }++ // Collect adjacent checkbox paragraphs so each run becomes one list.const runs: HTMLElement[][] = [];- for (const p of todos) {+ for (const p of tagged) {+ if (!completedByCheckbox.has(p)) {+ continue;+ }const currentRun = runs[runs.length - 1];if (currentRun && currentRun[currentRun.length - 1].nextElementSibling === p) {currentRun.push(p);@@ -103,7 +175,7 @@ function convertTodoTags(scope: HTMLElement) {for (const run of runs) {const items = run.map((p) => {- const checked = p.getAttribute("data-tag") === "to-do:completed" ? ` checked="checked"` : "";+ const checked = completedByCheckbox.get(p) ? ` checked="checked"` : "";return `<li><label class="todo-list__label"><input type="checkbox"${checked}><span class="todo-list__label__description">${p.innerHTML}</span></label></li>`;});run[0].insertAdjacentHTML("beforebegin", `<ul class="todo-list">${items.join("")}</ul>`);