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(server): tag imported OneNote pages with their Graph page id
Elian Doran0438931023ee
diff --git a/apps/server/src/services/import/onenote/importer.spec.ts b/apps/server/src/services/import/onenote/importer.spec.tsindex e538d7aaf2..c806d92f29 100644--- a/apps/server/src/services/import/onenote/importer.spec.ts+++ b/apps/server/src/services/import/onenote/importer.spec.ts@@ -1,6 +1,21 @@-import { describe, expect, it } from "vitest";+import { becca, cls } from "@triliumnext/core";+import { beforeAll, describe, expect, it, vi } from "vitest";-import { mapWithConcurrency, resolveSubpageParents } from "./importer.js";+import sqlInit from "../../sql_init.js";+import graph from "./graph.js";+import { importSelection, mapWithConcurrency, resolveSubpageParents } from "./importer.js";++vi.mock("./graph.js", () => ({+ default: {+ getAccount: vi.fn(),+ listNotebooks: vi.fn(),+ listPages: vi.fn(),+ getPageContent: vi.fn(),+ getResource: vi.fn()+ }+}));++const graphMock = vi.mocked(graph);describe("resolveSubpageParents", () => {it("keeps top-level pages directly under the section", () => {@@ -56,3 +71,36 @@ describe("mapWithConcurrency", () => {expect(await mapWithConcurrency([], 4, async (x) => x)).toEqual([]);});});++describe("importSelection (real DB)", () => {+ beforeAll(async () => {+ sqlInit.initializeDb();+ await sqlInit.dbReady;+ });++ it("labels every imported page note with its Graph page id", async () => {+ graphMock.listPages.mockResolvedValue([+ { id: "1-abc", title: "Page One", level: 0 },+ { id: "1-def", title: "Page Two", level: 1 }+ ]);+ graphMock.getPageContent.mockResolvedValue({ html: "<p>hello</p>", inkml: "" });++ await cls.init(() => importSelection({+ accessToken: "token",+ parentNoteId: "root",+ sections: [{ id: "sec-1", title: "Section", groupPath: [], notebookId: "nb-1", notebookTitle: "Notebook" }],+ taskId: "task-page-id-label"+ }));++ const pageOne = Object.values(becca.notes).find((note) => note.title === "Page One");+ const pageTwo = Object.values(becca.notes).find((note) => note.title === "Page Two");+ // The Graph page id enables a future "retry failed pages" / re-import dedup pass to map an+ // imported note back to its OneNote page.+ expect(pageOne?.getOwnedLabelValue("oneNotePageId")).toBe("1-abc");+ expect(pageTwo?.getOwnedLabelValue("oneNotePageId")).toBe("1-def");++ // Container notes (section, notebook) are not OneNote pages and must not carry the label.+ const sectionNote = Object.values(becca.notes).find((note) => note.title === "Section");+ expect(sectionNote?.getOwnedLabelValue("oneNotePageId")).toBeNull();+ });+});diff --git a/apps/server/src/services/import/onenote/importer.ts b/apps/server/src/services/import/onenote/importer.tsindex 2eb1e6395c..62ef8ba87e 100644--- a/apps/server/src/services/import/onenote/importer.ts+++ b/apps/server/src/services/import/onenote/importer.ts@@ -17,6 +17,12 @@ import { inkmlToSvg } from "./inkml.js";import { type LinkTarget, rewritePageLinks } from "./links.js";interface FetchedPage {+ /**+ * The Graph API page id, preserved on the imported note as `#oneNotePageId` so a later pass+ * (re-import dedup, retrying failed pages) can map the note back to its OneNote page. Distinct+ * from {@link pageId}, the GUID OneNote uses in `onenote:` links.+ */+ id: string;title: string;/** OneNote's indentation level in the page list: 0 for a top-level page, 1+ for a subpage. */level: number;@@ -87,7 +93,7 @@ export async function importSelection({ accessToken, parentNoteId, sections, tasconst { html: rawHtml, inkml } = await graph.getPageContent(accessToken, page.id);const html = converter.convertPageHtml(rawHtml);const resources = await downloadPageResources(accessToken, page.title, html);- 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 });+ fetchedPages.push({ id: page.id, 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({@@ -173,6 +179,7 @@ function createNotes(parentNoteId: string, sections: FetchedSection[], debug: boisProtected});pageNoteIds[index] = pageNote.noteId;+ pageNote.addLabel("oneNotePageId", page.id);// 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