Obsidian: Templater Converters

This post is, in a sense, a natural follow up to my Path-based Commands in Obsidian post from around this time last year. In that post I demonstrated how you could trigger command line tools from within Obsidian using the Templater plugin. In this post I’m flipping that around, and rather than going external, I am focusing on the internal. The text within an Obsidian note. However, rather than adding new content, I am going to tell you a little about how I am also using Templater to convert existing content, transforming it in a variety of ways to effectively build my own text processing functionality within Obsidian.

Templater

The Templater plugin for Obsidian is one that enables you to go far beyond the core Templates plugin that comes with Obsidian. With a powerful syntax engine, it allows you to not only insert static text, or text based on a set of tokens, it also allows you much more advanced interactions based around the JavaScript model that underpins not only the plugin, but also Obsidian itself.

If you have not already installed and set up the Templater plugin, then it would be worth while doing so before continuing.

Templater Templates

Simple text in a Templater template (a Markdown file) will output the same text when the file is processed as a template for insertion. There is however a syntax to run sets of code that will then generate dynamic content to include in the template.

<% and %> are used to delimit simpler syntax commands, while more complex sets of JavaScript code are wrapped in <%* and %>.

The execution of dynamic content is a key requirement of Templater to allow us to dynamically process content.

The Basic Principle

As noted above, Templater can generate content based on the JavaScript model it utilises, and within this model, there is one key component around which the conversion templates are based. This snippet of code.

tp.file.selection()

tp is the Templater object, file refers to the current Markdown file in Obsidian, and selection() is a function that retrieves the currently selected text. Once we have access to the string that represents the current selection, we can manipulate that with additional JavaScript code and then replace the selection with the processed text.

A Simple Example

If for example we wanted to convert the selected text to upper case text, then we could utilise a standard JavaScript function (toUpperCase) to help us with that, and we can create a template like this.

<% tp.file.selection().normalize().toUpperCase() %>

The selected text when the template is invoked is converted to upper case, and then placed back into the Obsidian note, replacing the selection.

The Template Collection

I have gathered and commented thirty conversion templates to share with everyone, and I expect to add more over time. You can download these templates from a GitHub repository I created to host them:

The repository provides some guidance on how to ‘install’ the templates, and also explains a little bit more of my particular set up for them, such as why they have what might appear to be an unusual prefix for the template file names.

Frequent Use Conversions

Triggering templates from the command palette is very useful, but sometimes you may find you would rather have a quicker way of triggering it because you use it so frequently. Arguably the best way to speed things up is to trigger the template via a keyboard shortcut. The Hotkeys for Templates plugin allows you to do just that.

Doing this can make the templates feel even more like built in commands, so if there are ones you use frequently it is worth consideration.

Conclusion

Templater is one of the most versatile plugins for Obsidian, and it is amazing how it can be used to effectively build your own functionality within Obsidian, without you having to resort to building an entire plugin. Particularly so if you want to do something relatively straight forward.

It often feels to me like Templater is a plugin through which you can build your own mini-plugins, and I hope that I continue to discover new and innovative uses through my own practice and that of others in the ever expanding Obsidian community.

If you have not already done so, please take a look through the GitHub repository and the templates it contains. I would hope that at least a few of them might be useful to you.

Author: Stephen Millard
Tags: | obsidian |

Buy me a coffeeBuy me a coffee



Related posts that you may also like to read