Skip to content

Build your personal knowledge base with Trilium Notes

feat(import): preserve OneNote subpage hierarchy

Elian Dorancb82caf5272c
diff --git a/apps/server/src/services/import/onenote/graph.ts b/apps/server/src/services/import/onenote/graph.ts
index 8b30d4d24f..563d504528 100644
--- a/apps/server/src/services/import/onenote/graph.ts
+++ b/apps/server/src/services/import/onenote/graph.ts
@@ -34,6 +34,8 @@ export interface OneNotePage {
title: string;
createdDateTime?: string;
lastModifiedDateTime?: string;
+ /** OneNote's indentation level in the page list: 0 for a top-level page, 1+ for a subpage. */
+ level: number;
/** The page-id GUID OneNote uses in `onenote:` links to this page; used to resolve cross-page links. */
pageId?: string;
}
@@ -101,13 +103,14 @@ function appendQuery(url: string, query: string): string {
export async function listPages(accessToken: string, sectionId: string): Promise<OneNotePage[]> {
// `links` carries the page's own `onenote:` client URL, from which we recover the page-id GUID that
// cross-page links reference (see links.ts).
- const url = `/me/onenote/sections/${sectionId}/pages?$select=id,title,createdDateTime,lastModifiedDateTime,links&$orderby=order&pagelevel=true`;
+ const url = `/me/onenote/sections/${sectionId}/pages?$select=id,title,createdDateTime,lastModifiedDateTime,level,links&$orderby=order&pagelevel=true`;
const raw = await graphGetAll<RawPage>(accessToken, url);
return raw.map((p) => ({
id: p.id,
title: p.title || "Untitled",
createdDateTime: p.createdDateTime,
lastModifiedDateTime: p.lastModifiedDateTime,
+ level: p.level ?? 0,
pageId: extractPageId(p.links?.oneNoteClientUrl?.href) ?? undefined
}));
}
@@ -201,6 +204,7 @@ interface RawPage {
title?: string;
createdDateTime?: string;
lastModifiedDateTime?: string;
+ level?: number;
links?: { oneNoteClientUrl?: { href?: string } };
}
diff --git a/apps/server/src/services/import/onenote/importer.spec.ts b/apps/server/src/services/import/onenote/importer.spec.ts
new file mode 100644
index 0000000000..09b6948d59
--- /dev/null
+++ b/apps/server/src/services/import/onenote/importer.spec.ts
@@ -0,0 +1,31 @@
+import { describe, expect, it } from "vitest";
+
+import { resolveSubpageParents } from "./importer.js";
+
+describe("resolveSubpageParents", () => {
+ it("keeps top-level pages directly under the section", () => {
+ // No indentation: every page is a root (parent index -1).
+ expect(resolveSubpageParents([0, 0, 0])).toEqual([-1, -1, -1]);
+ expect(resolveSubpageParents([])).toEqual([]);
+ });
+
+ it("nests subpages and sub-subpages under the nearest shallower page", () => {
+ // Two subpages share the first page as parent.
+ expect(resolveSubpageParents([0, 1, 1])).toEqual([-1, 0, 0]);
+ // A sub-subpage chains under its subpage.
+ expect(resolveSubpageParents([0, 1, 2])).toEqual([-1, 0, 1]);
+ });
+
+ it("re-parents siblings correctly when stepping back out of nesting", () => {
+ // 0:root, 1→0, 2→1, then back to level 1 (→0) and a new root.
+ expect(resolveSubpageParents([0, 1, 2, 1, 0])).toEqual([-1, 0, 1, 0, -1]);
+ // Each top-level page owns its own subpage.
+ expect(resolveSubpageParents([0, 1, 0, 1])).toEqual([-1, 0, -1, 2]);
+ });
+
+ it("falls back to the section root for malformed indentation", () => {
+ // Leading subpage with no parent, and a level jump that skips level 1.
+ expect(resolveSubpageParents([1, 0])).toEqual([-1, -1]);
+ expect(resolveSubpageParents([0, 2])).toEqual([-1, -1]);
+ });
+});
diff --git a/apps/server/src/services/import/onenote/importer.ts b/apps/server/src/services/import/onenote/importer.ts
index f7a36d61d5..9701e57999 100644
--- a/apps/server/src/services/import/onenote/importer.ts
+++ b/apps/server/src/services/import/onenote/importer.ts
@@ -32,6 +32,8 @@ export interface SectionSelection {
interface FetchedPage {
title: string;
+ /** OneNote's indentation level in the page list: 0 for a top-level page, 1+ for a subpage. */
+ level: number;
/** The OneNote page-id GUID, used to resolve cross-page links to the note created for this page. */
pageId?: string;
html: string;
@@ -98,7 +100,7 @@ export async function importSelection({ accessToken, parentNoteId, sections, tas
const { html: rawHtml, inkml } = await graph.getPageContent(accessToken, page.id);
const html = converter.convertPageHtml(rawHtml);
const resources = await downloadPageResources(accessToken, html);
- fetchedPages.push({ title: page.title, pageId: page.pageId, html, rawHtml, rawInkml: inkml, inkSvg: inkmlToSvg(inkml), resources, createdDateTime: page.createdDateTime, lastModifiedDateTime: page.lastModifiedDateTime });
+ fetchedPages.push({ title: page.title, level: page.level, pageId: page.pageId, html, rawHtml, rawInkml: inkml, inkSvg: inkmlToSvg(inkml), resources, createdDateTime: page.createdDateTime, lastModifiedDateTime: page.lastModifiedDateTime });
taskContext.increaseProgressCount();
}
fetched.push({
@@ -154,15 +156,23 @@ function createNotes(parentNoteId: string, sections: FetchedSection[], debug: bo
const sectionNote = createFolder(notebookNoteId, section.title);
applyOriginalDates(sectionNote, section.createdDateTime, section.lastModifiedDateTime);
- for (const page of section.pages) {
+ // OneNote pages carry an indentation level (subpages); preserve it by parenting each page under
+ // its OneNote parent page rather than flattening every page under the section. Pages arrive in
+ // display order (depth-first), so a page's parent always precedes it and already has a note.
+ const parentIndexByPage = resolveSubpageParents(section.pages.map((p) => p.level));
+ const pageNoteIds: string[] = [];
+
+ for (const [index, page] of section.pages.entries()) {
+ const parentIndex = parentIndexByPage[index];
const { note: pageNote } = noteService.createNewNote({
- parentNoteId: sectionNote.noteId,
+ parentNoteId: parentIndex >= 0 ? (pageNoteIds[parentIndex] ?? sectionNote.noteId) : sectionNote.noteId,
title: page.title,
content: page.html,
type: "text",
mime: "text/html",
isProtected
});
+ pageNoteIds[index] = pageNote.noteId;
// Resources and ink both need the page note to exist (attachments hang off it), so build
// the per-page content now: swap Graph URLs for local attachment references, then append
@@ -219,6 +229,25 @@ function createNotes(parentNoteId: string, sections: FetchedSection[], debug: bo
return rootNote.noteId;
}
+/**
+ * Maps OneNote's flat, display-ordered page list (each page tagged with an indentation `level`) to a
+ * parent-child tree. Returns, for each page, the index of its parent page — the nearest preceding page
+ * exactly one level shallower — or -1 when the page is top-level and belongs directly under the section.
+ *
+ * OneNote orders pages depth-first, so a subpage always follows its parent; we track the most recent
+ * page seen at each level and discard deeper levels once we step back out, so siblings resolve to the
+ * same parent. A subpage whose expected parent level is missing (malformed input) falls back to -1.
+ */
+export function resolveSubpageParents(levels: number[]): number[] {
+ const lastIndexAtLevel: number[] = [];
+ return levels.map((level, index) => {
+ const parentIndex = level > 0 ? (lastIndexAtLevel[level - 1] ?? -1) : -1;
+ lastIndexAtLevel[level] = index;
+ lastIndexAtLevel.length = level + 1;
+ return parentIndex;
+ });
+}
+
/**
* Applies OneNote's original created/modified timestamps (ISO 8601) to an imported note, falling back
* to the creation date when the modification date is absent. A no-op when neither is available.