Logo Trilium Rocks!

Embedding resources

Obviously when developing it can be nice to keep modules separate. Trilium allows us to do this in a few different ways.

Child Scripts#

If you create a child note of your script that is also the same type of script (frontend/backend), Trilium will automatically execute it and make it available to the parent script. Obviously if the child script does not export a value of any kind then having it available in the parent script is not useful, but it is still executed so any side effects of the child script will still occur.

Child Files#

If you upload a JavaScript file as a child note of your script, Trilium will recognize the MIME type of the file and include it in your parent script. It is important to note that Trilium is unable to distinguish between frontend/backend when it comes to attached files. If it is JavaScript, it will be included in the parent.

Usage#

Normalized Globals#

As discussed in the last section, any child script will be included as part of the bundle's parameters using a normalized name. Basically, take the note title of the child note you are expecting to use, and replace any characters that would be invalid for a JavaScript variable with empty string. If you aren't sure, anything that isn't a standard letter, number, or underscore will be removed. If you're really not sure, the next section might be your best option.

Require#

Trilium provides a fake require() function that allows you to require any child note by the title of the note. In this case this is the entire unsanitized title. This means that for a note called highlight.min.js the global would be highlightminjs but you would still do require("highlight.min.js"). This is a better option for ensuring you're using the right name, and also for external linters since there is no unexpecting globals being used.

Manual (Not Recommended)#

You can use the API to get a specific note's contents and perform an eval on that content. This is a really bad way to do things when Trilium already provides guardrails for you. The only case this might be warranted is when you want to read a JSON note for data and use JSON.parse. In theory this could also be used for plain text resources as well.