trilium/src/services/script.js

139 lines
3.9 KiB
JavaScript
Raw Normal View History

const sql = require('./sql');
const ScriptContext = require('./script_context');
const Repository = require('./repository');
async function executeNote(dataKey, note) {
if (!note.isJavaScript()) {
return;
}
2018-03-05 01:06:35 +08:00
const bundle = await getScriptBundle(note);
2018-03-04 23:32:53 +08:00
await executeBundle(dataKey, bundle);
}
async function executeBundle(dataKey, bundle, startNote) {
if (!startNote) {
// this is the default case, the only exception is when we want to preserve frontend startNote
startNote = bundle.note;
}
2018-03-04 23:32:53 +08:00
// last \r\n is necessary if script contains line comment on its last line
2018-03-05 01:06:35 +08:00
const script = "async function() {\r\n" + bundle.script + "\r\n}";
2018-03-04 23:32:53 +08:00
const ctx = new ScriptContext(dataKey, startNote, bundle.allNotes);
2018-03-04 23:32:53 +08:00
if (await bundle.note.hasAttribute('manual_transaction_handling')) {
2018-03-04 23:32:53 +08:00
return await execute(ctx, script, '');
}
else {
return await sql.doInTransaction(async () => execute(ctx, script, ''));
}
}
/**
* This method preserves frontend startNode - that's why we start execution from currentNote and override
* bundle's startNote.
*/
async function executeScript(dataKey, script, params, startNoteId, currentNoteId) {
const repository = new Repository(dataKey);
const startNote = await repository.getNote(startNoteId);
const currentNote = await repository.getNote(currentNoteId);
2018-03-07 13:17:18 +08:00
currentNote.content = `return await (${script}\r\n)(${getParams(params)})`;
currentNote.type = 'code';
currentNote.mime = 'application/javascript;env=backend';
const bundle = await getScriptBundle(currentNote);
return await executeBundle(dataKey, bundle, startNote);
2018-02-25 11:44:45 +08:00
}
2018-02-25 11:44:45 +08:00
async function execute(ctx, script, paramsStr) {
return await (function() { return eval(`const apiContext = this;\r\n(${script}\r\n)(${paramsStr})`); }.call(ctx));
}
function getParams(params) {
if (!params) {
return params;
}
return params.map(p => {
if (typeof p === "string" && p.startsWith("!@#Function: ")) {
return p.substr(13);
}
else {
return JSON.stringify(p);
}
}).join(",");
}
2018-03-07 13:17:18 +08:00
async function getScriptBundle(note, root = true, scriptEnv = null, includedNoteIds = []) {
2018-03-05 10:05:14 +08:00
if (!note.isJavaScript() && !note.isHtml() && note.type !== 'render') {
2018-03-05 01:06:35 +08:00
return;
}
2018-03-04 23:32:53 +08:00
2018-03-05 11:09:51 +08:00
if (await note.hasAttribute('disable_inclusion')) {
return;
}
2018-03-07 13:17:18 +08:00
if (root) {
scriptEnv = note.getScriptEnv();
}
if (note.type !== 'file' && scriptEnv !== note.getScriptEnv()) {
return;
}
2018-03-05 01:06:35 +08:00
const bundle = {
note: note,
script: '',
2018-03-05 10:05:14 +08:00
html: '',
2018-03-05 01:06:35 +08:00
allNotes: [note]
};
2018-03-03 22:11:41 +08:00
2018-03-05 01:06:35 +08:00
if (includedNoteIds.includes(note.noteId)) {
return bundle;
}
2018-03-03 22:11:41 +08:00
2018-03-05 01:06:35 +08:00
includedNoteIds.push(note.noteId);
2018-03-04 23:32:53 +08:00
2018-03-05 01:06:35 +08:00
const modules = [];
2018-03-03 22:11:41 +08:00
2018-03-05 01:06:35 +08:00
for (const child of await note.getChildren()) {
2018-03-07 13:17:18 +08:00
const childBundle = await getScriptBundle(child, false, scriptEnv, includedNoteIds);
2018-03-04 23:32:53 +08:00
2018-03-05 01:06:35 +08:00
if (childBundle) {
modules.push(childBundle.note);
bundle.script += childBundle.script;
2018-03-05 10:05:14 +08:00
bundle.html += childBundle.html;
2018-03-05 01:06:35 +08:00
bundle.allNotes = bundle.allNotes.concat(childBundle.allNotes);
}
2018-03-03 22:11:41 +08:00
}
2018-03-05 10:05:14 +08:00
if (note.isJavaScript()) {
bundle.script += `
apiContext.modules['${note.noteId}'] = {};
2018-03-07 13:17:18 +08:00
${root ? 'return ' : ''}await (async function(exports, module, api` + (modules.length > 0 ? ', ' : '') +
modules.map(child => sanitizeVariableName(child.title)).join(', ') + `) {
2018-03-05 01:06:35 +08:00
${note.content}
})({}, apiContext.modules['${note.noteId}'], apiContext.apis['${note.noteId}']` + (modules.length > 0 ? ', ' : '') +
modules.map(mod => `apiContext.modules['${mod.noteId}'].exports`).join(', ') + `);
2018-03-05 01:06:35 +08:00
`;
2018-03-05 10:05:14 +08:00
}
else if (note.isHtml()) {
bundle.html += note.content;
}
2018-03-04 23:32:53 +08:00
2018-03-05 01:06:35 +08:00
return bundle;
2018-03-03 22:11:41 +08:00
}
function sanitizeVariableName(str) {
return str.replace(/[^a-z0-9_]/gim, "");
}
module.exports = {
executeNote,
executeScript,
2018-03-07 13:17:18 +08:00
getScriptBundle
};