Skip to content

Build your personal knowledge base with Trilium Notes

fixed render notes

azivner8e95b080dad5
diff --git a/src/entities/note.js b/src/entities/note.js
index 9947aeb044..a9a364adfb 100644
--- a/src/entities/note.js
+++ b/src/entities/note.js
@@ -37,6 +37,10 @@ class Note extends Entity {
return "frontend";
}
+ if (this.type === 'render') {
+ return "frontend";
+ }
+
if (this.isJavaScript() && this.mime.endsWith('env=backend')) {
return "backend";
}
diff --git a/src/public/javascripts/api.js b/src/public/javascripts/api.js
index 266304ed1c..f7d0e745a0 100644
--- a/src/public/javascripts/api.js
+++ b/src/public/javascripts/api.js
@@ -1,12 +1,12 @@
-function ApiContext(startNote, allNotes) {
+function ScriptContext(startNote, allNotes) {
return {
modules: {},
notes: toObject(allNotes, note => [note.noteId, note]),
- apis: toObject(allNotes, note => [note.noteId, Api(startNote, note)]),
+ apis: toObject(allNotes, note => [note.noteId, ScriptApi(startNote, note)]),
};
}
-function Api(startNote, currentNote) {
+function ScriptApi(startNote, currentNote) {
const $pluginButtons = $("#plugin-buttons");
async function activateNote(notePath) {
diff --git a/src/public/javascripts/note_editor.js b/src/public/javascripts/note_editor.js
index c685ed3867..84e3a1fa14 100644
--- a/src/public/javascripts/note_editor.js
+++ b/src/public/javascripts/note_editor.js
@@ -217,9 +217,11 @@ const noteEditor = (function() {
if (currentNote.detail.type === 'render') {
$noteDetailRender.show();
- const html = await server.get('script/render/' + getCurrentNoteId());
+ const bundle = await server.get('script/bundle/' + getCurrentNoteId());
- $noteDetailRender.html(html);
+ $noteDetailRender.html(bundle.html);
+
+ executeBundle(bundle);
}
else if (currentNote.detail.type === 'file') {
$noteDetailAttachment.show();
diff --git a/src/public/javascripts/utils.js b/src/public/javascripts/utils.js
index 69bb04d7f3..a1f6c3c744 100644
--- a/src/public/javascripts/utils.js
+++ b/src/public/javascripts/utils.js
@@ -116,9 +116,7 @@ async function stopWatch(what, func) {
}
async function executeBundle(bundle) {
- const apiContext = ApiContext(bundle.note, bundle.allNotes);
-
- console.log((`const apiContext = this; (async function() { ${bundle.script}\r\n})()`));
+ const apiContext = ScriptContext(bundle.note, bundle.allNotes);
return await (function() { return eval(`const apiContext = this; (async function() { ${bundle.script}\r\n})()`); }.call(apiContext));
}
diff --git a/src/routes/api/script.js b/src/routes/api/script.js
index f12d1edd2f..a94233a9ce 100644
--- a/src/routes/api/script.js
+++ b/src/routes/api/script.js
@@ -50,12 +50,4 @@ router.get('/bundle/:noteId', auth.checkApiAuth, wrap(async (req, res, next) =>
res.send(bundle);
}));
-router.get('/render/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => {
- const repository = new Repository(req);
- const note = await repository.getNote(req.params.noteId);
- const bundle = await script.getRenderScript(note);
-
- res.send(bundle);
-}));
-
module.exports = router;
\ No newline at end of file
diff --git a/src/services/scheduler.js b/src/services/scheduler.js
index a67c84ebc8..3c6d4d85a4 100644
--- a/src/services/scheduler.js
+++ b/src/services/scheduler.js
@@ -8,6 +8,7 @@ async function runNotesWithAttribute(runAttrValue) {
SELECT notes.*
FROM notes
JOIN attributes ON attributes.noteId = notes.noteId
+ AND attributes.isDeleted = 0
AND attributes.name = 'run'
AND attributes.value = ?
WHERE
diff --git a/src/services/script.js b/src/services/script.js
index 895fdd72b4..aafca51473 100644
--- a/src/services/script.js
+++ b/src/services/script.js
@@ -40,7 +40,8 @@ async function executeScript(dataKey, script, params, startNoteId, currentNoteId
const startNote = await repository.getNote(startNoteId);
const currentNote = await repository.getNote(currentNoteId);
- currentNote.content = `(${script}\r\n)(${getParams(params)})`;
+ currentNote.content = `return await (${script}\r\n)(${getParams(params)})`;
+ currentNote.type = 'code';
currentNote.mime = 'application/javascript;env=backend';
const bundle = await getScriptBundle(currentNote);
@@ -67,14 +68,7 @@ function getParams(params) {
}).join(",");
}
-async function getRenderScript(note) {
- const bundle = await getScriptBundle(note);
-
- return `<script type="text/javascript">(async function() {\r\nconst api = Api();\r\n${bundle.script}\r\n})();</script>`
- + bundle.html;
-}
-
-async function getScriptBundle(note, scriptEnv, includedNoteIds = []) {
+async function getScriptBundle(note, root = true, scriptEnv = null, includedNoteIds = []) {
if (!note.isJavaScript() && !note.isHtml() && note.type !== 'render') {
return;
}
@@ -83,7 +77,7 @@ async function getScriptBundle(note, scriptEnv, includedNoteIds = []) {
return;
}
- if (!scriptEnv) {
+ if (root) {
scriptEnv = note.getScriptEnv();
}
@@ -107,7 +101,7 @@ async function getScriptBundle(note, scriptEnv, includedNoteIds = []) {
const modules = [];
for (const child of await note.getChildren()) {
- const childBundle = await getScriptBundle(child, scriptEnv, includedNoteIds);
+ const childBundle = await getScriptBundle(child, false, scriptEnv, includedNoteIds);
if (childBundle) {
modules.push(childBundle.note);
@@ -120,7 +114,7 @@ async function getScriptBundle(note, scriptEnv, includedNoteIds = []) {
if (note.isJavaScript()) {
bundle.script += `
apiContext.modules['${note.noteId}'] = {};
-await (async function(exports, module, api` + (modules.length > 0 ? ', ' : '') +
+${root ? 'return ' : ''}await (async function(exports, module, api` + (modules.length > 0 ? ', ' : '') +
modules.map(child => sanitizeVariableName(child.title)).join(', ') + `) {
${note.content}
})({}, apiContext.modules['${note.noteId}'], apiContext.apis['${note.noteId}']` + (modules.length > 0 ? ', ' : '') +
@@ -141,6 +135,5 @@ function sanitizeVariableName(str) {
module.exports = {
executeNote,
executeScript,
- getScriptBundle,
- getRenderScript
+ getScriptBundle
};
\ No newline at end of file