Skip to content

Build your personal knowledge base with Trilium Notes

export with pos in filename and import so we preserve note order

azivnerddd216c92e0b
diff --git a/routes/api/export.js b/routes/api/export.js
index d0770eae0b..7a26ce8af8 100644
--- a/routes/api/export.js
+++ b/routes/api/export.js
@@ -23,25 +23,30 @@ router.get('/:noteId/to/:directory', async (req, res, next) => {
fs.mkdirSync(completeExportDir);
- await exportNote(noteId, completeExportDir);
+ const noteTreeId = await sql.getSingleValue('SELECT note_tree_id FROM notes_tree WHERE note_id = ?', [noteId]);
+
+ await exportNote(noteTreeId, completeExportDir);
res.send({});
});
-async function exportNote(noteId, dir) {
- const note = await sql.getSingleResult("SELECT * FROM notes WHERE note_id = ?", [noteId]);
+async function exportNote(noteTreeId, dir) {
+ const noteTree = await sql.getSingleResult("SELECT * FROM notes_tree WHERE note_tree_id = ?", [noteTreeId]);
+ const note = await sql.getSingleResult("SELECT * FROM notes WHERE note_id = ?", [noteTree.note_id]);
+
+ const pos = (noteTree.note_pos + '').padStart(4, '0');
- fs.writeFileSync(dir + '/' + note.note_title + '.html', note.note_text);
+ fs.writeFileSync(dir + '/' + pos + '-' + note.note_title + '.html', note.note_text);
- const children = await sql.getResults("SELECT * FROM notes_tree WHERE note_pid = ? AND is_deleted = 0", [noteId]);
+ const children = await sql.getResults("SELECT * FROM notes_tree WHERE note_pid = ? AND is_deleted = 0", [note.note_id]);
if (children.length > 0) {
- const childrenDir = dir + '/' + note.note_title;
+ const childrenDir = dir + '/' + pos + '-' + note.note_title;
fs.mkdirSync(childrenDir);
for (const child of children) {
- await exportNote(child.note_id, childrenDir);
+ await exportNote(child.note_tree_id, childrenDir);
}
}
}
diff --git a/routes/api/import.js b/routes/api/import.js
index fd32cf4bf7..f9e251a6f2 100644
--- a/routes/api/import.js
+++ b/routes/api/import.js
@@ -40,14 +40,30 @@ async function importNotes(dir, parentNoteId) {
continue;
}
- const noteTitle = file.substr(0, file.length - 5);
- const noteText = fs.readFileSync(path, "utf8");
+ const fileNameWithoutExt = file.substr(0, file.length - 5);
+
+ let noteTitle;
+ let notePos;
- let maxPos = await sql.getSingleValue("SELECT MAX(note_pos) FROM notes_tree WHERE note_pid = ? AND is_deleted = 0", [parentNoteId]);
- if (!maxPos) {
- maxPos = 1;
+ const match = fileNameWithoutExt.match(/^([0-9]{4})-(.*)$/);
+ if (match) {
+ notePos = parseInt(match[1]);
+ noteTitle = match[2];
+ }
+ else {
+ let maxPos = await sql.getSingleValue("SELECT MAX(note_pos) FROM notes_tree WHERE note_pid = ? AND is_deleted = 0", [parentNoteId]);
+ if (maxPos) {
+ notePos = maxPos + 1;
+ }
+ else {
+ notePos = 0;
+ }
+
+ noteTitle = fileNameWithoutExt;
}
+ const noteText = fs.readFileSync(path, "utf8");
+
const noteId = utils.newNoteId();
const noteTreeId = utils.newNoteHistoryId();
@@ -55,7 +71,7 @@ async function importNotes(dir, parentNoteId) {
note_tree_id: noteTreeId,
note_id: noteId,
note_pid: parentNoteId,
- note_pos: maxPos + 1,
+ note_pos: notePos,
is_expanded: 0,
is_deleted: 0,
date_modified: utils.nowTimestamp()
@@ -75,7 +91,7 @@ async function importNotes(dir, parentNoteId) {
await sync_table.addNoteSync(noteId);
- const noteDir = dir + '/' + noteTitle;
+ const noteDir = dir + '/' + fileNameWithoutExt;
if (fs.existsSync(noteDir) && fs.lstatSync(noteDir).isDirectory()) {
await importNotes(noteDir, noteId);