Skip to content

Build your personal knowledge base with Trilium Notes

OPML 2 import, closes #298, #286

zadam1359dd86c2bf
diff --git a/src/public/javascripts/services/utils.js b/src/public/javascripts/services/utils.js
index 1ea87e495f..ecd404d14e 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 72ce597d58..d0d66aafc5 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 '<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
};
\ No newline at end of file