const sax = require("sax"); const stream = require('stream'); const xml2js = require('xml2js'); const log = require("./log"); const utils = require("./utils"); const noteService = require("./notes"); const imageService = require("./image"); // date format is e.g. 20181121T193703Z function parseDate(text) { // insert - and : to make it ISO format text = text.substr(0, 4) + "-" + text.substr(4, 2) + "-" + text.substr(6, 2) + "T" + text.substr(9, 2) + ":" + text.substr(11, 2) + ":" + text.substr(13, 2) + "Z"; return text; } let note = {}; let resource; async function importEnex(file, parentNote) { const saxStream = sax.createStream(true); 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 = []; async function parseXml(text) { return new Promise(function(resolve, reject) { parser.parseString(text, function (err, result) { if (err) { reject(err); } else { resolve(result); } }); }); } function extractContent(enNote) { // [] thing is workaround for https://github.com/Leonidas-from-XIV/node-xml2js/issues/484 let content = xmlBuilder.buildObject([enNote]); content = content.substr(3, content.length - 7).trim(); // workaround for https://github.com/ckeditor/ckeditor5-list/issues/116 content = content.replace(/
  • \s+
    /g, "
  • "); content = content.replace(/<\/div>\s+<\/li>/g, "
  • "); // workaround for https://github.com/ckeditor/ckeditor5-list/issues/115 content = content.replace(/