diff --git a/src/routes/api/export.js b/src/routes/api/export.js index 440eecb8d..a69462e9d 100644 --- a/src/routes/api/export.js +++ b/src/routes/api/export.js @@ -2,7 +2,7 @@ const zipExportService = require('../../services/export/zip.js'); const singleExportService = require('../../services/export/single.js'); -const opmlExportService = require('../../services/export/opml.js'); +const opmlExportService = require('../../services/export/opml'); const becca = require('../../becca/becca'); const TaskContext = require('../../services/task_context'); const log = require('../../services/log'); diff --git a/src/routes/api/import.js b/src/routes/api/import.js index 19b54e8e9..5d296ac88 100644 --- a/src/routes/api/import.js +++ b/src/routes/api/import.js @@ -1,7 +1,7 @@ "use strict"; const enexImportService = require('../../services/import/enex.js'); -const opmlImportService = require('../../services/import/opml.js'); +const opmlImportService = require('../../services/import/opml'); const zipImportService = require('../../services/import/zip.js'); const singleImportService = require('../../services/import/single.js'); const cls = require('../../services/cls'); diff --git a/src/services/export/opml.js b/src/services/export/opml.ts similarity index 71% rename from src/services/export/opml.js rename to src/services/export/opml.ts index 2faa6d513..63bdf14fd 100644 --- a/src/services/export/opml.js +++ b/src/services/export/opml.ts @@ -1,9 +1,12 @@ "use strict"; -const utils = require('../utils'); -const becca = require('../../becca/becca'); +import utils = require('../utils'); +import becca = require('../../becca/becca'); +import TaskContext = require('../task_context'); +import BBranch = require('../../becca/entities/bbranch'); +import { Response } from 'express'; -function exportToOpml(taskContext, branch, version, res) { +function exportToOpml(taskContext: TaskContext, branch: BBranch, version: string, res: Response) { if (!['1.0', '2.0'].includes(version)) { throw new Error(`Unrecognized OPML version ${version}`); } @@ -12,9 +15,12 @@ function exportToOpml(taskContext, branch, version, res) { const note = branch.getNote(); - function exportNoteInner(branchId) { + function exportNoteInner(branchId: string) { const branch = becca.getBranch(branchId); + if (!branch) { throw new Error("Unable to find branch."); } + const note = branch.getNote(); + if (!note) { throw new Error("Unable to find note."); } if (note.hasOwnedLabel('excludeFromExport')) { return; @@ -24,13 +30,13 @@ function exportToOpml(taskContext, branch, version, res) { if (opmlVersion === 1) { const preparedTitle = escapeXmlAttribute(title); - const preparedContent = note.hasStringContent() ? prepareText(note.getContent()) : ''; + const preparedContent = note.hasStringContent() ? prepareText(note.getContent() as string) : ''; res.write(`\n`); } else if (opmlVersion === 2) { const preparedTitle = escapeXmlAttribute(title); - const preparedContent = note.hasStringContent() ? escapeXmlAttribute(note.getContent()) : ''; + const preparedContent = note.hasStringContent() ? escapeXmlAttribute(note.getContent() as string) : ''; res.write(`\n`); } @@ -41,7 +47,9 @@ function exportToOpml(taskContext, branch, version, res) { taskContext.increaseProgressCount(); for (const child of note.getChildBranches()) { - exportNoteInner(child.branchId); + if (child?.branchId) { + exportNoteInner(child.branchId); + } } res.write(''); @@ -60,7 +68,9 @@ function exportToOpml(taskContext, branch, version, res) { `); - exportNoteInner(branch.branchId); + if (branch.branchId) { + exportNoteInner(branch.branchId); + } res.write(` `); @@ -69,7 +79,7 @@ function exportToOpml(taskContext, branch, version, res) { taskContext.taskSucceeded(); } -function prepareText(text) { +function prepareText(text: string) { const newLines = text.replace(/(]*>|)/g, '\n') .replace(/ /g, ' '); // nbsp isn't in XML standard (only HTML) @@ -80,7 +90,7 @@ function prepareText(text) { return escaped.replace(/\n/g, ' '); } -function escapeXmlAttribute(text) { +function escapeXmlAttribute(text: string) { return text.replace(/&/g, '&') .replace(//g, '>') @@ -88,6 +98,6 @@ function escapeXmlAttribute(text) { .replace(/'/g, '''); } -module.exports = { +export = { exportToOpml }; diff --git a/src/services/task_context.ts b/src/services/task_context.ts index 78ab6b10c..7d2cdc448 100644 --- a/src/services/task_context.ts +++ b/src/services/task_context.ts @@ -65,7 +65,7 @@ class TaskContext { }); } - taskSucceeded(result: string) { + taskSucceeded(result?: string) { ws.sendMessageToAllClients({ type: 'taskSucceeded', taskId: this.taskId,