From b8eaff055ab1bd9890f51d8500c1a59de90422dc Mon Sep 17 00:00:00 2001 From: azivner Date: Mon, 5 Nov 2018 09:23:04 +0100 Subject: [PATCH] create notebook's "root note" for ENEX import --- README.md | 2 +- src/services/enex.js | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 905976d7f..c4272cbd7 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ See other pictures in [screenshot tour](https://github.com/zadam/trilium/wiki/Sc * [Scripting](https://github.com/zadam/trilium/wiki/Scripts) - see [Advanced showcases](https://github.com/zadam/trilium/wiki/Advanced-showcases) * Scales well in both usability and performance upwards of 100 000 notes * [Night theme](https://github.com/zadam/trilium/wiki/Themes) -* [Markdown import & export](https://github.com/zadam/trilium/wiki/Markdown) +* [Evernote](https://github.com/zadam/trilium/wiki/Evernote-import) and [Markdown import & export](https://github.com/zadam/trilium/wiki/Markdown) ## Builds diff --git a/src/services/enex.js b/src/services/enex.js index 41335990c..3b706b097 100644 --- a/src/services/enex.js +++ b/src/services/enex.js @@ -22,6 +22,16 @@ async function importEnex(file, parentNote) { const xmlBuilder = new xml2js.Builder({ headless: true }); const parser = new xml2js.Parser({ explicitArray: true }); + const rootNoteTitle = file.originalname.toLowerCase().endsWith(".enex") + ? file.originalname.substr(0, file.originalname.length - 5) + : file.originalname; + + // root note is new note into all ENEX/notebook's notes will be imported + const rootNote = (await noteService.createNote(parentNote.noteId, rootNoteTitle, "", { + type: 'text', + mime: 'text/html' + })).note; + // we're persisting notes as we parse the document, but these are run asynchronously and may not be finished // when we finish parsing. We use this to be sure that all saving has been finished before returning successfully. const saveNotePromises = []; @@ -195,7 +205,7 @@ async function importEnex(file, parentNote) { // following is workaround for this issue: https://github.com/Leonidas-from-XIV/node-xml2js/issues/484 content = extractContent(xmlObject['en-note']); - const resp = await noteService.createNote(parentNote.noteId, title, content, { + const resp = await noteService.createNote(rootNote.noteId, title, content, { attributes, dateCreated, type: 'text', @@ -234,8 +244,7 @@ async function importEnex(file, parentNote) { return new Promise((resolve, reject) => { // resolve only when we parse the whole document AND saving of all notes have been finished - // we resolve to parentNote because there's no single note to pick - saxStream.on("end", () => { Promise.all(saveNotePromises).then(() => resolve(parentNote)) }); + saxStream.on("end", () => { Promise.all(saveNotePromises).then(() => resolve(rootNote)) }); const bufferStream = new stream.PassThrough(); bufferStream.end(file.buffer);