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): add debug option to attach original OneNote HTML
Elian Doranf58e810f948f
diff --git a/apps/client/src/services/onenote_import.ts b/apps/client/src/services/onenote_import.tsindex e9ddaab3b5..f77444295d 100644--- a/apps/client/src/services/onenote_import.ts+++ b/apps/client/src/services/onenote_import.ts@@ -44,7 +44,7 @@ function getNotebooks() {return server.get<{ notebooks: OneNoteNotebook[] }>("onenote-import/notebooks");}-function runImport(payload: { parentNoteId: string; sections: OneNoteSectionSelection[]; taskId: string }) {+function runImport(payload: { parentNoteId: string; sections: OneNoteSectionSelection[]; taskId: string; debug?: boolean }) {return server.post<{ noteId: string }>("onenote-import/import", payload);}diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 2d8ad9e733..381a64d78b 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -245,7 +245,9 @@"no_notebooks": "No notebooks were found in this account.","select_sections": "Select the sections to import:","import": "Import selected",- "import_started": "OneNote import started…"+ "import_started": "OneNote import started…",+ "attach_source": "Attach original HTML (debug)",+ "attach_source_hint": "Saves each page's unmodified OneNote HTML as an attachment on the imported note, for comparing against the converted result."},"include_note": {"dialog_title": "Include note",diff --git a/apps/client/src/widgets/dialogs/import_providers/onenote.tsx b/apps/client/src/widgets/dialogs/import_providers/onenote.tsxindex 574c8e9a45..1ad59f6038 100644--- a/apps/client/src/widgets/dialogs/import_providers/onenote.tsx+++ b/apps/client/src/widgets/dialogs/import_providers/onenote.tsx@@ -16,6 +16,7 @@ function OneNotePanel({ parentNoteId, closeDialog }: ImportProviderPanelProps) {const [account, setAccount] = useState<OneNoteAccount | null>(null);const [notebooks, setNotebooks] = useState<OneNoteNotebook[]>([]);const [selectedIds, setSelectedIds] = useState<Set<string>>(new Set());+ const [debug, setDebug] = useState(false);const [importing, setImporting] = useState(false);const pollRef = useRef<ReturnType<typeof setInterval> | null>(null);@@ -111,14 +112,14 @@ function OneNotePanel({ parentNoteId, closeDialog }: ImportProviderPanelProps) {setImporting(true);try {- await onenoteImport.runImport({ parentNoteId, sections, taskId: randomString(10) });+ await onenoteImport.runImport({ parentNoteId, sections, taskId: randomString(10), debug });toast.showMessage(t("onenote_import.import_started"));closeDialog();} catch (e) {toast.showError(e instanceof Error ? e.message : String(e));setImporting(false);}- }, [notebooks, selectedIds, parentNoteId, closeDialog]);+ }, [notebooks, selectedIds, parentNoteId, debug, closeDialog]);if (phase === "checking") {return <div className="onenote-panel"><LoadingSpinner /></div>;@@ -167,6 +168,13 @@ function OneNotePanel({ parentNoteId, closeDialog }: ImportProviderPanelProps) {)}<div className="onenote-actions">+ <FormCheckbox+ name="onenote-debug"+ label={t("onenote_import.attach_source")}+ hint={t("onenote_import.attach_source_hint")}+ currentValue={debug}+ onChange={setDebug}+ /><Buttontext={t("onenote_import.import")}kind="primary"diff --git a/apps/server/src/routes/api/onenote_import.ts b/apps/server/src/routes/api/onenote_import.tsindex 05ec215476..493935d1d0 100644--- a/apps/server/src/routes/api/onenote_import.ts+++ b/apps/server/src/routes/api/onenote_import.ts@@ -93,7 +93,7 @@ async function runImport(req: Request) {return [401, "Not connected to OneNote."];}- const { parentNoteId, sections, taskId } = req.body as { parentNoteId: string; sections: SectionSelection[]; taskId: string };+ const { parentNoteId, sections, taskId, debug } = req.body as { parentNoteId: string; sections: SectionSelection[]; taskId: string; debug?: boolean };if (!parentNoteId || !taskId) {throw new ValidationError("parentNoteId and taskId are required.");}@@ -102,7 +102,7 @@ async function runImport(req: Request) {}becca.getNoteOrThrow(parentNoteId);- const noteId = await importer.importSelection({ accessToken, parentNoteId, sections, taskId });+ const noteId = await importer.importSelection({ accessToken, parentNoteId, sections, taskId, debug: !!debug });return { noteId };}diff --git a/apps/server/src/services/import/onenote/importer.ts b/apps/server/src/services/import/onenote/importer.tsindex dff718df88..7994a21805 100644--- a/apps/server/src/services/import/onenote/importer.ts+++ b/apps/server/src/services/import/onenote/importer.ts@@ -20,6 +20,8 @@ export interface SectionSelection {interface FetchedPage {title: string;html: string;+ /** The unmodified HTML returned by the Graph API, kept only when debug mode is on. */+ rawHtml: string;}interface FetchedSection {@@ -28,7 +30,7 @@ interface FetchedSection {pages: FetchedPage[];}-export async function importSelection({ accessToken, parentNoteId, sections, taskId }: { accessToken: string; parentNoteId: string; sections: SectionSelection[]; taskId: string }): Promise<string> {+export async function importSelection({ accessToken, parentNoteId, sections, taskId, debug = false }: { accessToken: string; parentNoteId: string; sections: SectionSelection[]; taskId: string; debug?: boolean }): Promise<string> {const taskContext = TaskContext.getInstance(taskId, "importNotes", { safeImport: true });// Phase 1: pull everything over the network first, so note creation can run in a single@@ -39,21 +41,21 @@ export async function importSelection({ accessToken, parentNoteId, sections, tasconst fetchedPages: FetchedPage[] = [];for (const page of pages) {const rawHtml = await graph.getPageContent(accessToken, page.id);- fetchedPages.push({ title: page.title, html: converter.convertPageHtml(rawHtml) });+ fetchedPages.push({ title: page.title, html: converter.convertPageHtml(rawHtml), rawHtml });taskContext.increaseProgressCount();}fetched.push({ title: section.title, notebookTitle: section.notebookTitle, pages: fetchedPages });}// Phase 2: create the note tree.- const rootNoteId = sql.transactional(() => createNotes(parentNoteId, fetched));+ const rootNoteId = sql.transactional(() => createNotes(parentNoteId, fetched, debug));taskContext.taskSucceeded({ parentNoteId, importedNoteId: rootNoteId });return rootNoteId;}-function createNotes(parentNoteId: string, sections: FetchedSection[]): string {+function createNotes(parentNoteId: string, sections: FetchedSection[], debug: boolean): string {const parentNote = becca.getNoteOrThrow(parentNoteId);const isProtected = parentNote.isProtected && protectedSession.isProtectedSessionAvailable();@@ -74,7 +76,7 @@ function createNotes(parentNoteId: string, sections: FetchedSection[]): string {const sectionNote = createFolder(notebookNoteId, section.title);for (const page of section.pages) {- noteService.createNewNote({+ const { note: pageNote } = noteService.createNewNote({parentNoteId: sectionNote.noteId,title: page.title,content: page.html,@@ -82,6 +84,17 @@ function createNotes(parentNoteId: string, sections: FetchedSection[]): string {mime: "text/html",isProtected});++ // Debug aid: keep the unmodified Graph HTML alongside the converted note so the two can+ // be compared when diagnosing conversion issues.+ if (debug) {+ pageNote.saveAttachment({+ role: "file",+ mime: "text/html",+ title: "OneNote source.html",+ content: page.rawHtml+ });+ }}}