trilium/routes/api/script.js

81 lines
2.7 KiB
JavaScript
Raw Normal View History

"use strict";
const express = require('express');
const router = express.Router();
const auth = require('../../services/auth');
const wrap = require('express-promise-wrap').wrap;
const sql = require('../../services/sql');
const notes = require('../../services/notes');
const protected_session = require('../../services/protected_session');
const attributes = require('../../services/attributes');
const script = require('../../services/script');
router.post('/exec/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => {
const noteId = req.params.noteId;
const ret = await script.executeScript(noteId, req, req.body.script, req.body.params);
res.send(ret);
}));
router.get('/startup', auth.checkApiAuth, wrap(async (req, res, next) => {
const noteIds = await attributes.getNoteIdsWithAttribute("run_on_startup");
const scripts = [];
for (const noteId of noteIds) {
scripts.push(await getNoteWithSubtreeScript(noteId, req));
}
res.send(scripts);
}));
router.get('/subtree/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => {
const noteId = req.params.noteId;
2018-01-29 08:30:14 +08:00
const noteScript = (await notes.getNoteById(noteId, req)).content;
const subTreeScripts = await getSubTreeScripts(noteId, [noteId], req);
res.send(subTreeScripts + noteScript);
}));
async function getNoteWithSubtreeScript(noteId, req) {
2018-01-29 08:30:14 +08:00
const noteScript = (await notes.getNoteById(noteId, req)).content;
const subTreeScripts = await getSubTreeScripts(noteId, [noteId], req);
return subTreeScripts + noteScript;
}
async function getSubTreeScripts(parentId, includedNoteIds, dataKey) {
2018-01-29 08:30:14 +08:00
const children = await sql.getAll(`SELECT notes.noteId, notes.title, notes.content, notes.isProtected, notes.mime
FROM notes JOIN note_tree USING(noteId)
WHERE note_tree.isDeleted = 0 AND notes.isDeleted = 0
AND note_tree.parentNoteId = ? AND notes.type = 'code'
2018-01-24 12:41:22 +08:00
AND (notes.mime = 'application/javascript' OR notes.mime = 'text/html')`, [parentId]);
protected_session.decryptNotes(dataKey, children);
let script = "\r\n";
for (const child of children) {
2018-01-29 08:30:14 +08:00
if (includedNoteIds.includes(child.noteId)) {
return;
}
2018-01-29 08:30:14 +08:00
includedNoteIds.push(child.noteId);
2018-01-29 08:30:14 +08:00
script += await getSubTreeScripts(child.noteId, includedNoteIds, dataKey);
if (child.mime === 'application/javascript') {
2018-01-29 08:30:14 +08:00
child.content = '<script>' + child.content + '</script>';
}
2018-01-29 08:30:14 +08:00
script += child.content + "\r\n";
}
return script;
}
module.exports = router;