Skip to content

Build your personal knowledge base with Trilium Notes

server-ts: Port services/import/opml

Elian Dorancc1a545e136a
diff --git a/package-lock.json b/package-lock.json
index 972352ddd3..6e82325d6e 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -101,6 +101,7 @@
"@types/sanitize-html": "^2.11.0",
"@types/turndown": "^5.0.4",
"@types/ws": "^8.5.10",
+ "@types/xml2js": "^0.4.14",
"cross-env": "7.0.3",
"electron": "25.9.8",
"electron-builder": "24.6.4",
@@ -1811,6 +1812,15 @@
"@types/node": "*"
}
},
+ "node_modules/@types/xml2js": {
+ "version": "0.4.14",
+ "resolved": "https://registry.npmjs.org/@types/xml2js/-/xml2js-0.4.14.tgz",
+ "integrity": "sha512-4YnrRemBShWRO2QjvUin8ESA41rH+9nQGLUGZV/1IDhi3SL9OhdpNC/MrulTWuptXKwhx/aDxE7toV0f/ypIXQ==",
+ "dev": true,
+ "dependencies": {
+ "@types/node": "*"
+ }
+ },
"node_modules/@types/yauzl": {
"version": "2.9.2",
"resolved": "https://registry.npmjs.org/@types/yauzl/-/yauzl-2.9.2.tgz",
@@ -16550,6 +16560,15 @@
"@types/node": "*"
}
},
+ "@types/xml2js": {
+ "version": "0.4.14",
+ "resolved": "https://registry.npmjs.org/@types/xml2js/-/xml2js-0.4.14.tgz",
+ "integrity": "sha512-4YnrRemBShWRO2QjvUin8ESA41rH+9nQGLUGZV/1IDhi3SL9OhdpNC/MrulTWuptXKwhx/aDxE7toV0f/ypIXQ==",
+ "dev": true,
+ "requires": {
+ "@types/node": "*"
+ }
+ },
"@types/yauzl": {
"version": "2.9.2",
"resolved": "https://registry.npmjs.org/@types/yauzl/-/yauzl-2.9.2.tgz",
diff --git a/package.json b/package.json
index c0576bc0e2..d237344118 100644
--- a/package.json
+++ b/package.json
@@ -124,6 +124,7 @@
"@types/sanitize-html": "^2.11.0",
"@types/turndown": "^5.0.4",
"@types/ws": "^8.5.10",
+ "@types/xml2js": "^0.4.14",
"cross-env": "7.0.3",
"electron": "25.9.8",
"electron-builder": "24.6.4",
diff --git a/src/services/import/opml.js b/src/services/import/opml.ts
similarity index 67%
rename from src/services/import/opml.js
rename to src/services/import/opml.ts
index 2b99dbd85a..66d2e944b5 100644
--- a/src/services/import/opml.js
+++ b/src/services/import/opml.ts
@@ -1,20 +1,37 @@
"use strict";
-const noteService = require('../../services/notes');
-const parseString = require('xml2js').parseString;
-const protectedSessionService = require('../protected_session');
-const htmlSanitizer = require('../html_sanitizer');
-
-/**
- * @param {TaskContext} taskContext
- * @param {Buffer} fileBuffer
- * @param {BNote} parentNote
- * @returns {Promise<*[]|*>}
- */
-async function importOpml(taskContext, fileBuffer, parentNote) {
- const xml = await new Promise(function(resolve, reject)
+import noteService = require('../../services/notes');
+import xml2js = require("xml2js");
+import protectedSessionService = require('../protected_session');
+import htmlSanitizer = require('../html_sanitizer');
+import TaskContext = require('../task_context');
+import BNote = require('../../becca/entities/bnote');
+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: Buffer, parentNote: BNote) {
+ const xml = await new Promise<OpmlXml>(function(resolve, reject)
{
- parseString(fileBuffer, function (err, result) {
+ parseString(fileBuffer, function (err: any, result: OpmlXml) {
if (err) {
reject(err);
}
@@ -30,7 +47,7 @@ async function importOpml(taskContext, fileBuffer, parentNote) {
const opmlVersion = parseInt(xml.opml.$.version);
- function importOutline(outline, parentNoteId) {
+ function importOutline(outline: OpmlOutline, parentNoteId: string) {
let title, content;
if (opmlVersion === 1) {
@@ -83,7 +100,7 @@ async function importOpml(taskContext, fileBuffer, parentNote) {
return returnNote;
}
-function toHtml(text) {
+function toHtml(text: string) {
if (!text) {
return '';
}
@@ -91,6 +108,6 @@ function toHtml(text) {
return `<p>${text.replace(/(?:\r\n|\r|\n)/g, '</p><p>')}</p>`;
}
-module.exports = {
+export = {
importOpml
};