From 1359dd86c2bf2ee4781f1fc3412ed99025539a4b Mon Sep 17 00:00:00 2001 From: zadam Date: Sat, 16 Feb 2019 22:13:29 +0100 Subject: [PATCH] OPML 2 import, closes #298, #286 --- src/public/javascripts/services/utils.js | 1 - src/services/import/opml.js | 48 ++++++++++++++++-------- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src/public/javascripts/services/utils.js b/src/public/javascripts/services/utils.js index 1ea87e495..ecd404d14 100644 --- a/src/public/javascripts/services/utils.js +++ b/src/public/javascripts/services/utils.js @@ -145,7 +145,6 @@ function bindShortcut(keyboardShortcut, handler) { } $(document).bind('keydown', keyboardShortcut, e => { - console.log(e); handler(); e.preventDefault(); diff --git a/src/services/import/opml.js b/src/services/import/opml.js index 72ce597d5..d0d66aafc 100644 --- a/src/services/import/opml.js +++ b/src/services/import/opml.js @@ -22,20 +22,50 @@ async function importOpml(importContext, fileBuffer, parentNote) { }); }); - if (xml.opml.$.version !== '1.0' && xml.opml.$.version !== '1.1') { - return [400, 'Unsupported OPML version ' + xml.opml.$.version + ', 1.0 or 1.1 expected instead.']; + if (!['1.0', '1.1', '2.0'].includes(xml.opml.$.version)) { + return [400, 'Unsupported OPML version ' + xml.opml.$.version + ', 1.0, 1.1 or 2.0 expected instead.']; + } + + const opmlVersion = parseInt(xml.opml.$.version); + + async function importOutline(outline, parentNoteId) { + let title, content; + + if (opmlVersion === 1) { + title = outline.$.title; + content = toHtml(outline.$.text); + } + else if (opmlVersion === 2) { + title = outline.$.text; + content = outline.$._note; // _note is already HTML + } + else { + throw new Error("Unrecognized OPML version " + opmlVersion); + } + + const {note} = await noteService.createNote(parentNoteId, title, content); + + importContext.increaseProgressCount(); + + for (const childOutline of (outline.outline || [])) { + await importOutline(childOutline, note.noteId); + } + + return note; } const outlines = xml.opml.body[0].outline || []; let returnNote = null; for (const outline of outlines) { - const note = await importOutline(importContext, outline, parentNote.noteId); + const note = await importOutline(outline, parentNote.noteId); // first created note will be activated after import returnNote = returnNote || note; } + importContext.importFinished(returnNote.noteId); + return returnNote; } @@ -47,18 +77,6 @@ function toHtml(text) { return '

' + text.replace(/(?:\r\n|\r|\n)/g, '

') + '

'; } -async function importOutline(importContext, outline, parentNoteId) { - const {note} = await noteService.createNote(parentNoteId, outline.$.title, toHtml(outline.$.text)); - - importContext.increaseProgressCount(); - - for (const childOutline of (outline.outline || [])) { - await importOutline(childOutline, note.noteId); - } - - return note; -} - module.exports = { importOpml }; \ No newline at end of file