2018-11-16 12:12:04 +01:00
|
|
|
"use strict";
|
|
|
|
|
2025-01-02 13:47:44 +01:00
|
|
|
import { getContentDisposition, stripTags } from "../utils.js";
|
2024-07-18 21:35:17 +03:00
|
|
|
import becca from "../../becca/becca.js";
|
|
|
|
import TaskContext from "../task_context.js";
|
|
|
|
import BBranch from "../../becca/entities/bbranch.js";
|
2025-01-09 18:36:24 +02:00
|
|
|
import type { Response } from "express";
|
2018-11-16 12:12:04 +01:00
|
|
|
|
2024-02-19 22:07:21 +02:00
|
|
|
function exportToOpml(taskContext: TaskContext, branch: BBranch, version: string, res: Response) {
|
2025-01-09 18:07:02 +02:00
|
|
|
if (!["1.0", "2.0"].includes(version)) {
|
2022-12-21 15:19:05 +01:00
|
|
|
throw new Error(`Unrecognized OPML version ${version}`);
|
2019-02-16 23:58:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const opmlVersion = parseInt(version);
|
2019-02-16 23:33:40 +01:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
const note = branch.getNote();
|
2018-11-16 12:12:04 +01:00
|
|
|
|
2024-02-19 22:07:21 +02:00
|
|
|
function exportNoteInner(branchId: string) {
|
2021-05-02 11:23:58 +02:00
|
|
|
const branch = becca.getBranch(branchId);
|
2025-01-09 18:07:02 +02:00
|
|
|
if (!branch) {
|
|
|
|
throw new Error("Unable to find branch.");
|
|
|
|
}
|
2024-02-19 22:07:21 +02:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
const note = branch.getNote();
|
2025-01-09 18:07:02 +02:00
|
|
|
if (!note) {
|
|
|
|
throw new Error("Unable to find note.");
|
|
|
|
}
|
2018-11-19 09:34:05 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
if (note.hasOwnedLabel("excludeFromExport")) {
|
2018-11-19 09:34:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
const title = `${branch.prefix ? `${branch.prefix} - ` : ""}${note.title}`;
|
2018-11-16 12:12:04 +01:00
|
|
|
|
2019-02-16 23:58:42 +01:00
|
|
|
if (opmlVersion === 1) {
|
|
|
|
const preparedTitle = escapeXmlAttribute(title);
|
2025-01-09 18:07:02 +02:00
|
|
|
const preparedContent = note.hasStringContent() ? prepareText(note.getContent() as string) : "";
|
2018-11-16 12:12:04 +01:00
|
|
|
|
2019-02-16 23:58:42 +01:00
|
|
|
res.write(`<outline title="${preparedTitle}" text="${preparedContent}">\n`);
|
2025-01-09 18:07:02 +02:00
|
|
|
} else if (opmlVersion === 2) {
|
2019-02-16 23:58:42 +01:00
|
|
|
const preparedTitle = escapeXmlAttribute(title);
|
2025-01-09 18:07:02 +02:00
|
|
|
const preparedContent = note.hasStringContent() ? escapeXmlAttribute(note.getContent() as string) : "";
|
2019-02-16 23:58:42 +01:00
|
|
|
|
|
|
|
res.write(`<outline text="${preparedTitle}" _note="${preparedContent}">\n`);
|
2025-01-09 18:07:02 +02:00
|
|
|
} else {
|
2022-12-21 15:19:05 +01:00
|
|
|
throw new Error(`Unrecognized OPML version ${opmlVersion}`);
|
2019-02-16 23:58:42 +01:00
|
|
|
}
|
2018-11-16 12:12:04 +01:00
|
|
|
|
2019-10-18 22:27:38 +02:00
|
|
|
taskContext.increaseProgressCount();
|
2019-02-10 22:30:55 +01:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
for (const child of note.getChildBranches()) {
|
2024-02-19 22:07:21 +02:00
|
|
|
if (child?.branchId) {
|
|
|
|
exportNoteInner(child.branchId);
|
|
|
|
}
|
2018-11-16 12:12:04 +01:00
|
|
|
}
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
res.write("</outline>");
|
2018-11-16 12:12:04 +01:00
|
|
|
}
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
const filename = `${branch.prefix ? `${branch.prefix} - ` : ""}${note.title}.opml`;
|
2019-02-16 23:58:42 +01:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
res.setHeader("Content-Disposition", getContentDisposition(filename));
|
|
|
|
res.setHeader("Content-Type", "text/x-opml");
|
2018-11-16 12:12:04 +01:00
|
|
|
|
|
|
|
res.write(`<?xml version="1.0" encoding="UTF-8"?>
|
2019-03-03 19:43:30 +01:00
|
|
|
<opml version="${version}">
|
2018-11-16 12:12:04 +01:00
|
|
|
<head>
|
|
|
|
<title>Trilium export</title>
|
|
|
|
</head>
|
|
|
|
<body>`);
|
|
|
|
|
2024-02-19 22:07:21 +02:00
|
|
|
if (branch.branchId) {
|
|
|
|
exportNoteInner(branch.branchId);
|
|
|
|
}
|
2018-11-16 12:12:04 +01:00
|
|
|
|
|
|
|
res.write(`</body>
|
|
|
|
</opml>`);
|
|
|
|
res.end();
|
2019-02-10 22:30:55 +01:00
|
|
|
|
2019-10-18 22:27:38 +02:00
|
|
|
taskContext.taskSucceeded();
|
2018-11-16 12:12:04 +01:00
|
|
|
}
|
|
|
|
|
2024-02-19 22:07:21 +02:00
|
|
|
function prepareText(text: string) {
|
2025-01-09 18:07:02 +02:00
|
|
|
const newLines = text.replace(/(<p[^>]*>|<br\s*\/?>)/g, "\n").replace(/ /g, " "); // nbsp isn't in XML standard (only HTML)
|
2018-11-16 12:12:04 +01:00
|
|
|
|
2025-01-02 13:47:44 +01:00
|
|
|
const stripped = stripTags(newLines);
|
2018-11-16 12:12:04 +01:00
|
|
|
|
|
|
|
const escaped = escapeXmlAttribute(stripped);
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
return escaped.replace(/\n/g, " ");
|
2018-11-16 12:12:04 +01:00
|
|
|
}
|
|
|
|
|
2024-02-19 22:07:21 +02:00
|
|
|
function escapeXmlAttribute(text: string) {
|
2025-01-09 18:07:02 +02:00
|
|
|
return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
2018-11-16 12:12:04 +01:00
|
|
|
}
|
|
|
|
|
2024-07-18 21:42:44 +03:00
|
|
|
export default {
|
2018-11-16 12:12:04 +01:00
|
|
|
exportToOpml
|
2020-06-20 12:31:38 +02:00
|
|
|
};
|