mirror of https://github.com/TriliumNext/Notes
Merge branch 'master' into ckeditor
commit
502e5cbbdf
@ -0,0 +1,10 @@
|
||||
DROP TABLE recent_notes;
|
||||
|
||||
CREATE TABLE `recent_notes` (
|
||||
'note_tree_id'TEXT NOT NULL PRIMARY KEY,
|
||||
`note_path` TEXT NOT NULL,
|
||||
`date_accessed` INTEGER NOT NULL ,
|
||||
is_deleted INT
|
||||
);
|
||||
|
||||
DELETE FROM sync WHERE entity_name = 'recent_notes';
|
||||
@ -0,0 +1,55 @@
|
||||
"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');
|
||||
const html = require('html');
|
||||
|
||||
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);
|
||||
|
||||
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(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 + '/' + pos + '-' + note.note_title + '.html', html.prettyPrint(note.note_text, {indent_size: 2}));
|
||||
|
||||
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 + '/' + pos + '-' + note.note_title;
|
||||
|
||||
fs.mkdirSync(childrenDir);
|
||||
|
||||
for (const child of children) {
|
||||
await exportNote(child.note_tree_id, childrenDir);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = router;
|
||||
@ -0,0 +1,102 @@
|
||||
"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');
|
||||
const utils = require('../../services/utils');
|
||||
const sync_table = require('../../services/sync_table');
|
||||
|
||||
router.get('/:directory/to/:parentNoteId', async (req, res, next) => {
|
||||
const directory = req.params.directory.replace(/[^0-9a-zA-Z_-]/gi, '');
|
||||
const parentNoteId = req.params.parentNoteId;
|
||||
|
||||
const dir = data_dir.EXPORT_DIR + '/' + directory;
|
||||
|
||||
await sql.doInTransaction(async () => await importNotes(dir, parentNoteId));
|
||||
|
||||
res.send({});
|
||||
});
|
||||
|
||||
async function importNotes(dir, parentNoteId) {
|
||||
const parent = await sql.getSingleResult("SELECT * FROM notes WHERE note_id = ?", [parentNoteId]);
|
||||
|
||||
if (!parent) {
|
||||
return;
|
||||
}
|
||||
|
||||
const fileList = fs.readdirSync(dir);
|
||||
|
||||
for (const file of fileList) {
|
||||
const path = dir + '/' + file;
|
||||
|
||||
if (fs.lstatSync(path).isDirectory()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!file.endsWith('.html')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const fileNameWithoutExt = file.substr(0, file.length - 5);
|
||||
|
||||
let noteTitle;
|
||||
let notePos;
|
||||
|
||||
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();
|
||||
|
||||
await sql.insert('notes_tree', {
|
||||
note_tree_id: noteTreeId,
|
||||
note_id: noteId,
|
||||
note_pid: parentNoteId,
|
||||
note_pos: notePos,
|
||||
is_expanded: 0,
|
||||
is_deleted: 0,
|
||||
date_modified: utils.nowTimestamp()
|
||||
});
|
||||
|
||||
await sync_table.addNoteTreeSync(noteTreeId);
|
||||
|
||||
await sql.insert('notes', {
|
||||
note_id: noteId,
|
||||
note_title: noteTitle,
|
||||
note_text: noteText,
|
||||
is_deleted: 0,
|
||||
is_protected: 0,
|
||||
date_created: utils.nowTimestamp(),
|
||||
date_modified: utils.nowTimestamp()
|
||||
});
|
||||
|
||||
await sync_table.addNoteSync(noteId);
|
||||
|
||||
const noteDir = dir + '/' + fileNameWithoutExt;
|
||||
|
||||
if (fs.existsSync(noteDir) && fs.lstatSync(noteDir).isDirectory()) {
|
||||
await importNotes(noteDir, noteId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = router;
|
||||
Loading…
Reference in New Issue