Skip to content

Build your personal knowledge base with Trilium Notes

inclusion of scripts based on script environment

azivnerd26170762bc2
diff --git a/src/entities/note.js b/src/entities/note.js
index 79469e8450..9947aeb044 100644
--- a/src/entities/note.js
+++ b/src/entities/note.js
@@ -32,6 +32,18 @@ class Note extends Entity {
return (this.type === "code" || this.type === "file") && this.mime === "text/html";
}
+ getScriptEnv() {
+ if (this.isHtml() || (this.isJavaScript() && this.mime.endsWith('env=frontend'))) {
+ return "frontend";
+ }
+
+ if (this.isJavaScript() && this.mime.endsWith('env=backend')) {
+ return "backend";
+ }
+
+ return null;
+ }
+
async getAttributes() {
return this.repository.getEntities("SELECT * FROM attributes WHERE noteId = ? AND isDeleted = 0", [this.noteId]);
}
diff --git a/src/services/script.js b/src/services/script.js
index 495330dd80..d3ce411931 100644
--- a/src/services/script.js
+++ b/src/services/script.js
@@ -60,7 +60,7 @@ async function getRenderScript(note) {
+ bundle.html;
}
-async function getScriptBundle(note, includedNoteIds = []) {
+async function getScriptBundle(note, scriptEnv, includedNoteIds = []) {
if (!note.isJavaScript() && !note.isHtml() && note.type !== 'render') {
return;
}
@@ -69,6 +69,10 @@ async function getScriptBundle(note, includedNoteIds = []) {
return;
}
+ if (!scriptEnv) {
+ scriptEnv = note.getScriptEnv();
+ }
+
const bundle = {
note: note,
script: '',
@@ -82,10 +86,16 @@ async function getScriptBundle(note, includedNoteIds = []) {
includedNoteIds.push(note.noteId);
+ bundle.script = `api.__modules['${note.noteId}'] = { __noteId: '${note.noteId}', __env: '${note.getScriptEnv()}'};`;
+
+ if (note.type !== 'file' && scriptEnv !== note.getScriptEnv()) {
+ return bundle;
+ }
+
const modules = [];
for (const child of await note.getChildren()) {
- const childBundle = await getScriptBundle(child, includedNoteIds);
+ const childBundle = await getScriptBundle(child, scriptEnv, includedNoteIds);
if (childBundle) {
modules.push(childBundle.note);
@@ -97,7 +107,6 @@ async function getScriptBundle(note, includedNoteIds = []) {
if (note.isJavaScript()) {
bundle.script += `
-api.__modules['${note.noteId}'] = {};
await (async function(exports, module, api, startNote, currentNote` + (modules.length > 0 ? ', ' : '') +
modules.map(child => child.title).join(', ') + `) {
${note.content}