Files
notesnook/apps/web/src/utils/streams/export-stream.ts

98 lines
3.1 KiB
TypeScript
Raw Normal View History

/*
This file is part of the Notesnook project (https://notesnook.com/)
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2024-01-27 16:53:56 +05:00
import { exportNote } from "../../common/export";
import { makeUniqueFilename } from "./utils";
import { ZipFile } from "./zip-stream";
2024-01-17 21:21:05 +05:00
import { Note } from "@notesnook/core";
2024-01-27 16:53:56 +05:00
export class ExportStream extends TransformStream<Note, ZipFile> {
constructor(
format: "pdf" | "md" | "txt" | "html" | "md-frontmatter",
2024-01-27 16:53:56 +05:00
signal?: AbortSignal
) {
const counters: Record<string, number> = {};
super({
2024-01-27 16:53:56 +05:00
async transform(note, controller) {
2024-01-17 18:26:33 +05:00
try {
if (signal?.aborted) {
2024-01-27 16:53:56 +05:00
controller.terminate();
2024-01-17 18:26:33 +05:00
return;
}
2024-01-27 16:53:56 +05:00
if (!note || format === "pdf") return;
2024-01-17 18:26:33 +05:00
2024-01-27 16:53:56 +05:00
const result = await exportNote(note, format);
if (!result) return;
2024-01-17 18:26:33 +05:00
2024-01-27 16:53:56 +05:00
const { filename, content } = result;
2024-01-17 18:26:33 +05:00
const notebooks = [
2024-01-27 16:53:56 +05:00
...(
2023-08-21 16:24:17 +05:00
db.relations
?.to({ id: note.id, type: "note" }, "notebook")
.resolved() || []
2024-01-27 16:53:56 +05:00
).map((n) => ({ title: n.title, topics: [] })),
2024-01-17 18:26:33 +05:00
...(note.notebooks || []).map(
(ref: { id: string; topics: string[] }) => {
const notebook = db.notebooks?.notebook(ref.id);
const topics: any[] = notebook?.topics.all || [];
return {
title: notebook?.title,
topics: ref.topics
.map((topicId: string) =>
topics.find((topic) => topic.id === topicId)
)
.filter(Boolean)
};
}
)
];
const filePaths: Array<string> =
notebooks.length > 0
? notebooks
.map((notebook) => {
if (notebook.topics.length > 0)
return notebook.topics.map((topic: { title: string }) =>
2024-01-27 16:53:56 +05:00
[notebook.title, topic.title, filename].join("/")
2024-01-17 18:26:33 +05:00
);
2024-01-27 16:53:56 +05:00
return [notebook.title, filename].join("/");
2024-01-17 18:26:33 +05:00
})
.flat()
2024-01-27 16:53:56 +05:00
: [filename];
2024-01-17 18:26:33 +05:00
filePaths.forEach((filePath) => {
controller.enqueue({
path: makeUniqueFilename(filePath, counters),
2024-01-27 16:53:56 +05:00
data: content,
2023-08-21 16:24:17 +05:00
mtime: new Date(note.dateEdited),
ctime: new Date(note.dateCreated)
2024-01-17 18:26:33 +05:00
});
});
2024-01-17 18:26:33 +05:00
} catch (e) {
controller.error(e);
}
}
});
}
}