jon-heard / obsidian-inline-scripts

Obsidian plugin: Type text shortcuts that expand into javascript generated text.
MIT License
134 stars 4 forks source link

Global function for all shortcuts #11

Closed MohrJonas closed 2 years ago

MohrJonas commented 2 years ago

Let's say I have a function toCamelCase() that takes a string as input and returns it in camelCase. I now want to use this function inside multiple shortcuts. Is there a way to define global functions for use in multiple shortcuts (Basically a kind of import)? Thanks.

jon-heard commented 2 years ago

Yes there is. If you have a shortcut that has no Test string, then it's what is called a "helper script". A helper script's Expansion string is prepended to the Expansion strings of all shortcuts defined after it. Thus, you could put toCamelCase() inside a helper script shortcut, and then call it in any shortcuts defined below the helper script.

Helper scripts are limited to within the shortcut-file (or the list of shortcuts within the plugin's settings). Thus, you can't define a helper script in shortcut-file A, then use it in shortcut-file B. Instead, you'd have to rewrite it in shortcut-file B.

You can read more about helper scripts here.