Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
correct handling of inclusion of dependencies
azivnerfda414615003
diff --git a/src/entities/note.js b/src/entities/note.jsindex ac9f99a415..f8787011dc 100644--- a/src/entities/note.js+++ b/src/entities/note.js@@ -23,6 +23,10 @@ class Note extends Entity {return this.type === "code" && this.mime === "application/json";}+ isJavaScript() {+ return this.type === "code" && this.mime === "application/javascript";+ }+async getAttributes() {return this.repository.getEntities("SELECT * FROM attributes WHERE noteId = ? AND isDeleted = 0", [this.noteId]);}diff --git a/src/public/javascripts/utils.js b/src/public/javascripts/utils.jsindex 9b8de60bd5..eec8fa13ea 100644--- a/src/public/javascripts/utils.js+++ b/src/public/javascripts/utils.js@@ -116,8 +116,7 @@ async function stopWatch(what, func) {}function executeScript(script) {- // last \r\n is necessary if script contains line comment on its last line- eval("(async function() {" + script + "\r\n})()");+ eval(script);}function formatValueWithWhitespace(val) {diff --git a/src/routes/api/script.js b/src/routes/api/script.jsindex 85042a30e6..ad09dfe12a 100644--- a/src/routes/api/script.js+++ b/src/routes/api/script.js@@ -31,26 +31,28 @@ router.get('/startup', auth.checkApiAuth, wrap(async (req, res, next) => {}));router.get('/subtree/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => {- const noteId = req.params.noteId;-const repository = new Repository(req);+ const noteId = req.params.noteId;- const noteScript = (await repository.getNote(noteId)).content;-- const subTreeScripts = await getSubTreeScripts(noteId, [noteId], repository);-- res.send(subTreeScripts + noteScript);+ res.send(await getNoteWithSubtreeScript(noteId, repository));}));async function getNoteWithSubtreeScript(noteId, repository) {- const noteScript = (await repository.getNote(noteId)).content;+ const note = await repository.getNote(noteId);++ let noteScript = note.content;++ if (note.isJavaScript()) {+ // last \r\n is necessary if script contains line comment on its last line+ noteScript = "(async function() {" + noteScript + "\r\n})()";+ }- const subTreeScripts = await getSubTreeScripts(noteId, [noteId], repository);+ const subTreeScripts = await getSubTreeScripts(noteId, [noteId], repository, note.isJavaScript());return subTreeScripts + noteScript;}-async function getSubTreeScripts(parentId, includedNoteIds, repository) {+async function getSubTreeScripts(parentId, includedNoteIds, repository, isJavaScript) {const children = await repository.getEntities(`SELECT notes.*FROM notes JOIN note_tree USING(noteId)@@ -69,7 +71,7 @@ async function getSubTreeScripts(parentId, includedNoteIds, repository) {script += await getSubTreeScripts(child.noteId, includedNoteIds, repository);- if (child.mime === 'application/javascript') {+ if (!isJavaScript && child.mime === 'application/javascript') {child.content = '<script>' + child.content + '</script>';}