-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathopml.ts
109 lines (87 loc) · 2.85 KB
/
opml.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"use strict";
import noteService from "../../services/notes.js";
import xml2js from "xml2js";
import protectedSessionService from "../protected_session.js";
import htmlSanitizer from "../html_sanitizer.js";
import type TaskContext from "../task_context.js";
import type BNote from "../../becca/entities/bnote.js";
const parseString = xml2js.parseString;
interface OpmlXml {
opml: OpmlBody;
}
interface OpmlBody {
$: {
version: string;
};
body: OpmlOutline[];
}
interface OpmlOutline {
$: {
title: string;
text: string;
_note: string;
};
outline: OpmlOutline[];
}
async function importOpml(taskContext: TaskContext, fileBuffer: string | Buffer, parentNote: BNote) {
const xml = await new Promise<OpmlXml>(function (resolve, reject) {
parseString(fileBuffer, function (err: any, result: OpmlXml) {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
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);
function importOutline(outline: OpmlOutline, parentNoteId: string) {
let title, content;
if (opmlVersion === 1) {
title = outline.$.title;
content = toHtml(outline.$.text);
if (!title || !title.trim()) {
// https://github.com/zadam/trilium/issues/1862
title = outline.$.text;
content = "";
}
} else if (opmlVersion === 2) {
title = outline.$.text;
content = outline.$._note; // _note is already HTML
} else {
throw new Error(`Unrecognized OPML version ${opmlVersion}`);
}
content = htmlSanitizer.sanitize(content || "");
const { note } = noteService.createNewNote({
parentNoteId,
title,
content,
type: "text",
isProtected: parentNote.isProtected && protectedSessionService.isProtectedSessionAvailable()
});
taskContext.increaseProgressCount();
for (const childOutline of outline.outline || []) {
importOutline(childOutline, note.noteId);
}
return note;
}
const outlines = xml.opml.body[0].outline || [];
let returnNote = null;
for (const outline of outlines) {
const note = importOutline(outline, parentNote.noteId);
// the first created note will be activated after import
returnNote = returnNote || note;
}
return returnNote;
}
function toHtml(text: string) {
if (!text) {
return "";
}
return `<p>${text.replace(/(?:\r\n|\r|\n)/g, "</p><p>")}</p>`;
}
export default {
importOpml
};