From cc1a545e136a011bbc83eac81ad585b147385be4 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sun, 25 Feb 2024 08:07:17 +0200 Subject: [PATCH] server-ts: Port services/import/opml --- package-lock.json | 19 +++++++++ package.json | 1 + src/services/import/{opml.js => opml.ts} | 49 ++++++++++++++++-------- 3 files changed, 53 insertions(+), 16 deletions(-) rename src/services/import/{opml.js => opml.ts} (67%) diff --git a/package-lock.json b/package-lock.json index 972352ddd..6e82325d6 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 c0576bc0e..d23734411 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 2b99dbd85..66d2e944b 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'); +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; -/** - * @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) +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(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 `

${text.replace(/(?:\r\n|\r|\n)/g, '

')}

`; } -module.exports = { +export = { importOpml };