Skip to content

Build your personal knowledge base with Trilium Notes

export subtree to filesystem

azivner119d0858569a
diff --git a/package.json b/package.json
index b0dae40571..8433c09fa8 100644
--- a/package.json
+++ b/package.json
@@ -28,6 +28,7 @@
"ini": "^1.3.4",
"request": "^2.83.0",
"request-promise": "^4.2.2",
+ "rimraf": "^2.6.2",
"scrypt": "^6.0.3",
"serve-favicon": "~2.4.5",
"session-file-store": "^1.1.2",
diff --git a/routes/api/export.js b/routes/api/export.js
new file mode 100644
index 0000000000..d0770eae0b
--- /dev/null
+++ b/routes/api/export.js
@@ -0,0 +1,49 @@
+"use strict";
+
+const express = require('express');
+const router = express.Router();
+const rimraf = require('rimraf');
+const fs = require('fs');
+const sql = require('../../services/sql');
+const data_dir = require('../../services/data_dir');
+
+router.get('/:noteId/to/:directory', async (req, res, next) => {
+ const noteId = req.params.noteId;
+ const directory = req.params.directory.replace(/[^0-9a-zA-Z_-]/gi, '');
+
+ if (!fs.existsSync(data_dir.EXPORT_DIR)) {
+ fs.mkdirSync(data_dir.EXPORT_DIR);
+ }
+
+ const completeExportDir = data_dir.EXPORT_DIR + '/' + directory;
+
+ if (fs.existsSync(completeExportDir)) {
+ rimraf.sync(completeExportDir);
+ }
+
+ fs.mkdirSync(completeExportDir);
+
+ await exportNote(noteId, completeExportDir);
+
+ res.send({});
+});
+
+async function exportNote(noteId, dir) {
+ const note = await sql.getSingleResult("SELECT * FROM notes WHERE note_id = ?", [noteId]);
+
+ fs.writeFileSync(dir + '/' + note.note_title + '.html', note.note_text);
+
+ const children = await sql.getResults("SELECT * FROM notes_tree WHERE note_pid = ? AND is_deleted = 0", [noteId]);
+
+ if (children.length > 0) {
+ const childrenDir = dir + '/' + note.note_title;
+
+ fs.mkdirSync(childrenDir);
+
+ for (const child of children) {
+ await exportNote(child.note_id, childrenDir);
+ }
+ }
+}
+
+module.exports = router;
\ No newline at end of file
diff --git a/routes/routes.js b/routes/routes.js
index 2801a32b03..9b689a3f8a 100644
--- a/routes/routes.js
+++ b/routes/routes.js
@@ -17,6 +17,7 @@ const loginApiRoute = require('./api/login');
const eventLogRoute = require('./api/event_log');
const recentNotesRoute = require('./api/recent_notes');
const appInfoRoute = require('./api/app_info');
+const exportRoute = require('./api/export');
function register(app) {
app.use('/', indexRoute);
@@ -37,6 +38,7 @@ function register(app) {
app.use('/api/event-log', eventLogRoute);
app.use('/api/recent-notes', recentNotesRoute);
app.use('/api/app-info', appInfoRoute);
+ app.use('/api/export', exportRoute);
}
module.exports = {
diff --git a/services/data_dir.js b/services/data_dir.js
index afeef7c292..44e295042e 100644
--- a/services/data_dir.js
+++ b/services/data_dir.js
@@ -12,10 +12,12 @@ if (!fs.existsSync(TRILIUM_DATA_DIR)) {
const DOCUMENT_PATH = TRILIUM_DATA_DIR + "/document.db";
const BACKUP_DIR = TRILIUM_DATA_DIR + "/backup";
const LOG_DIR = TRILIUM_DATA_DIR + "/log";
+const EXPORT_DIR = TRILIUM_DATA_DIR + "/export";
module.exports = {
TRILIUM_DATA_DIR,
DOCUMENT_PATH,
BACKUP_DIR,
- LOG_DIR
+ LOG_DIR,
+ EXPORT_DIR
};
\ No newline at end of file