100 lines
3.0 KiB
TypeScript
Raw Normal View History

"use strict";
import { getContentDisposition, stripTags } from "../utils.js";
import becca from "../../becca/becca.js";
import type TaskContext from "../task_context.js";
import type BBranch from "../../becca/entities/bbranch.js";
import type { Response } from "express";
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)) {
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();
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}`;
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) : "";
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 {
throw new Error(`Unrecognized OPML version ${opmlVersion}`);
2019-02-16 23:58:42 +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);
}
}
2025-01-09 18:07:02 +02:00
res.write("</outline>");
}
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");
res.write(`<?xml version="1.0" encoding="UTF-8"?>
<opml version="${version}">
<head>
<title>Trilium export</title>
</head>
<body>`);
2024-02-19 22:07:21 +02:00
if (branch.branchId) {
exportNoteInner(branch.branchId);
}
res.write(`</body>
</opml>`);
res.end();
2019-02-10 22:30:55 +01:00
2019-10-18 22:27:38 +02:00
taskContext.taskSucceeded();
}
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(/&nbsp;/g, " "); // nbsp isn't in XML standard (only HTML)
const stripped = stripTags(newLines);
const escaped = escapeXmlAttribute(stripped);
2025-01-09 18:07:02 +02:00
return escaped.replace(/\n/g, "&#10;");
}
2024-02-19 22:07:21 +02:00
function escapeXmlAttribute(text: string) {
2025-01-09 18:07:02 +02:00
return text.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&apos;");
}
export default {
exportToOpml
2020-06-20 12:31:38 +02:00
};