2024-01-05 22:05:04 +05:00
|
|
|
/*
|
|
|
|
|
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-02-05 11:23:28 +05:00
|
|
|
import { db } from "../../common/db";
|
2024-01-27 16:53:56 +05:00
|
|
|
import { exportNote } from "../../common/export";
|
2024-01-05 22:05:04 +05:00
|
|
|
import { makeUniqueFilename } from "./utils";
|
|
|
|
|
import { ZipFile } from "./zip-stream";
|
2024-01-17 21:21:05 +05:00
|
|
|
import { Note } from "@notesnook/core";
|
2024-01-05 22:05:04 +05:00
|
|
|
|
2024-01-27 16:53:56 +05:00
|
|
|
export class ExportStream extends TransformStream<Note, ZipFile> {
|
2024-01-05 22:05:04 +05:00
|
|
|
constructor(
|
|
|
|
|
format: "pdf" | "md" | "txt" | "html" | "md-frontmatter",
|
2024-01-27 16:53:56 +05:00
|
|
|
signal?: AbortSignal
|
2024-01-05 22:05:04 +05:00
|
|
|
) {
|
|
|
|
|
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-17 21:43:21 +05:00
|
|
|
if (format === "pdf") return;
|
2024-01-17 18:26:33 +05:00
|
|
|
|
2024-01-17 21:43:21 +05:00
|
|
|
const result = await exportNote(note, { format });
|
2024-01-27 16:53:56 +05:00
|
|
|
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
|
|
|
|
2024-02-05 11:23:28 +05:00
|
|
|
const notebooks = await db.relations
|
|
|
|
|
.to({ id: note.id, type: "note" }, "notebook")
|
|
|
|
|
.get();
|
2024-01-17 18:26:33 +05:00
|
|
|
|
2024-02-05 11:23:28 +05:00
|
|
|
const filePaths: string[] = [];
|
|
|
|
|
for (const { fromId: notebookId } of notebooks) {
|
|
|
|
|
const crumbs = (await db.notebooks.breadcrumbs(notebookId)).map(
|
|
|
|
|
(n) => n.title
|
|
|
|
|
);
|
|
|
|
|
filePaths.push([...crumbs, filename].join("/"));
|
|
|
|
|
}
|
|
|
|
|
if (filePaths.length <= 0) filePaths.push(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-05 22:05:04 +05:00
|
|
|
});
|
2024-01-17 18:26:33 +05:00
|
|
|
} catch (e) {
|
|
|
|
|
controller.error(e);
|
2024-01-05 22:05:04 +05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|