Skip to content

Build your personal knowledge base with Trilium Notes

feat(edit-docs): automatically write documentation on start-up

Elian Dorandf2a9aed44d8
diff --git a/electron-docs-main.ts b/electron-docs-main.ts
index 875748bc3f..3c9ab8033e 100644
--- a/electron-docs-main.ts
+++ b/electron-docs-main.ts
@@ -1,9 +1,45 @@
+import fs from "fs/promises";
+import fsExtra from "fs-extra";
+import path from "path";
+
+const NOTE_ID_USER_GUIDE = "pOsGYCXsbNQG";
+
async function startElectron() {
await import("./electron-main.js");
}
async function main() {
await startElectron();
+ await exportData();
+}
+
+async function exportData() {
+ const zipFilePath = "output.zip";
+ const destRootPath = path.join("src", "public", "app", "doc_notes", "en", "User Guide");
+
+ await fsExtra.remove(destRootPath);
+ await fsExtra.mkdir(destRootPath);
+
+ // First export as zip.
+ const { exportToZipFile } = (await import("./src/services/export/zip.js")).default;
+ await exportToZipFile(NOTE_ID_USER_GUIDE, "html", zipFilePath);
+
+ setTimeout(async () => {
+ // Then extract the zip.
+ const { readZipFile, readContent } = (await import("./src/services/import/zip.js"));
+ await readZipFile(await fs.readFile(zipFilePath), async (zip, entry) => {
+ // We ignore directories since they can appear out of order anyway.
+ if (!entry.fileName.endsWith("/")) {
+ const destPath = path.join(destRootPath, entry.fileName);
+ const fileContent = await readContent(zip, entry);
+
+ await fsExtra.mkdirs(path.dirname(destPath));
+ await fs.writeFile(destPath, fileContent);
+ }
+
+ zip.readEntry();
+ });
+ }, 1000);
}
await main();
diff --git a/src/services/import/zip.ts b/src/services/import/zip.ts
index 5251b95468..49cde38f6e 100644
--- a/src/services/import/zip.ts
+++ b/src/services/import/zip.ts
@@ -605,7 +605,7 @@ function streamToBuffer(stream: Stream): Promise<Buffer> {
return new Promise((res, rej) => stream.on("end", () => res(Buffer.concat(chunks))));
}
-function readContent(zipfile: yauzl.ZipFile, entry: yauzl.Entry): Promise<Buffer> {
+export function readContent(zipfile: yauzl.ZipFile, entry: yauzl.Entry): Promise<Buffer> {
return new Promise((res, rej) => {
zipfile.openReadStream(entry, function (err, readStream) {
if (err) rej(err);
@@ -616,8 +616,8 @@ function readContent(zipfile: yauzl.ZipFile, entry: yauzl.Entry): Promise<Buffer
});
}
-function readZipFile(buffer: Buffer, processEntryCallback: (zipfile: yauzl.ZipFile, entry: yauzl.Entry) => void) {
- return new Promise((res, rej) => {
+export function readZipFile(buffer: Buffer, processEntryCallback: (zipfile: yauzl.ZipFile, entry: yauzl.Entry) => Promise<void>) {
+ return new Promise<void>((res, rej) => {
yauzl.fromBuffer(buffer, { lazyEntries: true, validateEntrySizes: false }, function (err, zipfile) {
if (err) rej(err);
if (!zipfile) throw new Error("Unable to read zip file.");