Skip to content

Commit

Permalink
OPML 2 import, closes #298, #286
Browse files Browse the repository at this point in the history
  • Loading branch information
zadam committed Feb 16, 2019
1 parent a1b610f commit 1359dd8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
1 change: 0 additions & 1 deletion src/public/javascripts/services/utils.js
Expand Up @@ -145,7 +145,6 @@ function bindShortcut(keyboardShortcut, handler) {
}

$(document).bind('keydown', keyboardShortcut, e => {
console.log(e);
handler();

e.preventDefault();
Expand Down
48 changes: 33 additions & 15 deletions src/services/import/opml.js
Expand Up @@ -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;
}

Expand All @@ -47,18 +77,6 @@ function toHtml(text) {
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 = {
importOpml
};

0 comments on commit 1359dd8

Please sign in to comment.