Skip to content

Commit

Permalink
docs: clarify that user scripts can return a result, clarify that js …
Browse files Browse the repository at this point in the history
…files will likely need to be created outside of Obsidian
  • Loading branch information
Zachatoo committed Jan 27, 2024
1 parent 978fe17 commit f759eb4
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions docs/src/user-functions/script-user-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,26 @@ Let's say you specified the `Scripts` folder as your script folder in Templater'

Templater will load all JavaScript (`.js` files) scripts in the `Scripts` folder.

You can then create your script named `Scripts/my_script.js` (The `.js` extension is required) for example.
You can then create your script named `Scripts/my_script.js` (the `.js` extension is required) for example. You will likely have to create the file outside of Obsidian, as Obsidian only creates markdown files.

You will then be able to call your scripts as user functions. The function name corresponds to the script file name.

Scripts should follow the [CommonJS module specification](https://flaviocopes.com/commonjs/), and export a single function.

Let's have an example with our previous script `my_script.js`.

Note that instead of outputting directly to the console, as we did earlier, a user script needs to `return` its output:

```javascript
function my_function (msg) {
return `Message from my script: ${msg}`;
}
module.exports = my_function;
```


In our previous example, a complete command invocation would look like this:
In this example, a complete command invocation would look like this:

```javascript
<% tp.user.my_script("Hello World!") %>
```

Which would print `Message from my script: Hello World!` in the console.
Which would output `Message from my script: Hello World!`.

## Global namespace

Expand Down

0 comments on commit f759eb4

Please sign in to comment.