OPML 2 import, closes #298, #286

This commit is contained in:
zadam 2019-02-16 22:13:29 +01:00
parent a1b610fc50
commit 1359dd86c2
2 changed files with 33 additions and 16 deletions

View File

@ -145,7 +145,6 @@ function bindShortcut(keyboardShortcut, handler) {
} }
$(document).bind('keydown', keyboardShortcut, e => { $(document).bind('keydown', keyboardShortcut, e => {
console.log(e);
handler(); handler();
e.preventDefault(); e.preventDefault();

View File

@ -22,20 +22,50 @@ async function importOpml(importContext, fileBuffer, parentNote) {
}); });
}); });
if (xml.opml.$.version !== '1.0' && xml.opml.$.version !== '1.1') { if (!['1.0', '1.1', '2.0'].includes(xml.opml.$.version)) {
return [400, 'Unsupported OPML version ' + xml.opml.$.version + ', 1.0 or 1.1 expected instead.']; 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 || []; const outlines = xml.opml.body[0].outline || [];
let returnNote = null; let returnNote = null;
for (const outline of outlines) { 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 // first created note will be activated after import
returnNote = returnNote || note; returnNote = returnNote || note;
} }
importContext.importFinished(returnNote.noteId);
return returnNote; return returnNote;
} }
@ -47,18 +77,6 @@ function toHtml(text) {
return '<p>' + text.replace(/(?:\r\n|\r|\n)/g, '</p><p>') + '</p>'; return '<p>' + text.replace(/(?:\r\n|\r|\n)/g, '</p><p>') + '</p>';
} }
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 = { module.exports = {
importOpml importOpml
}; };