Create Astro front matter with Templater in Obsidian

Aron Schüler Published


In this blogpost I want to show how I create Astro front matter with Templater in Obsidian.

About Obsidian and Templater

Obsidian is a local-first knowledge base that I use for my notes and writing. It is a markdown editor with a lot of features that make it a great tool for writing. One of the features is the ability to use plugins. One of the plugins I use is Templater. Templater is a plugin that allows you to use templates to generate content using JavaScript. While you can use it to automate anything while writing inside Obsidian, I create Astro front matter for my blog posts with it.

Installing Templater

The Templater plugin can be installed from the Obsidian settings. Go to “Community plugins”, click on the “Browse” button next to Community plugins and search for “Templater” by SilentVoid. Click on the result and on the “Install” button and you are good to go. You can also install it through this link: Install Templater

Using Templater

As previously mentioned, Templater allows you to use templates to generate content using JavaScript. You can use it to automate anything while writing inside Obsidian. I use it to create Astro front matter for my blog posts.

But there are other great exmaples for templates using Templater in Obsidian. Many of the great ones are listed in the Github Discussions of the plugin. Mine is listed there too, the upvotes and nice comments were the reason to write this post. You can find it here: https://github.com/SilentVoid13/Templater/discussions/categories/templates-showcase

The template I use to create Astro front matter is just a markdown file that contains a bit of javascript inline code and is stored in the Templater plugin folder. You need to configure the template folder in the Obsidian settings, under “Community Plugins -> Templater -> Template folder location”.

The content of the template

I called the template Blog Template.md and it looks like this:

---
title: <% tp.file.title %>
author: aron
type: post
pubDate: <% tp.file.creation_date("YYYY-MM-DDTHH:mm:ssZ") %>
lastmod: <% tp.file.last_modified_date("YYYY-MM-DDTHH:mm:ssZ") %>
url: <% tp.file.creation_date("/YYYY/MM/DD/") %><% tp.file.title.toLowerCase().replaceAll(" ", "-") %>
description: <% tp.system.prompt("Enter excerpt", "") %>
tags:
<%*
    let tags = await tp.system.prompt("Enter tags, comma separated", "")
    tags = tags.toLowerCase().split(",").map(val => `  - ${val}`).join("\n")
%><% tags %>
---

Template explanation

The template contains a few placeholders that are replaced by Templater when the template is used. The javascript and insertions are surrounded by <% and %>. The template contains a few placeholders that are replaced by Templater when the template is used. Let’s go through them.

  1. The title, used for the posts title (who could’ve guessed!) is replaced by the filename of the file that is created from the template.
  2. The pubDate, short for publication date, shown in the post and in the overview, is replaced by the creation date of the file in the format YYYY-MM-DDTHH:mm:ssZ.
  3. The lastmod is set to the initial data of publication. This can be adapted later on when the file is changed.
  4. The url is the url of the post. It is generated from the creation date and the title of the post.
  5. The description is the excerpt of the post. We set these in a prompt that is shown when a file is created with the template. It is important for SEO and social media to have a good excerpt. Also, it is shown in the overview of the blog, so it should be descriptive to capture the readers attention.
  6. The tags are the tags of the post. They are also set in a prompt that is shown when a file is created with the template. They are comma separated and converted to lowercase. With tags, the readers can find similar posts and navigate the blog.

Using the template

To use the template, the first step is to create a new file inside Obsidian and give it a good title. Then, open the Obsidian command line with Meta+P on macOS, and insert “Open Insert Template modal” or just press option+E. Select the template you want to use and fill out the inputs for excerpt and then the tags. After that, the script will create Astro front matter and I can just start writing the post. This makes it easy to create new posts and I don’t have to think about the front matter and the format of it. I can just start writing.

Summary

In Summary, using some kind of automation to create Astro front matter for my blog posts makes it easier to create new posts. I don’t have to think about the format of the front matter and can just start writing. This makes it easier to write and publish new posts and that just lowers the barrier to write new posts (like this one). If you want to read more about Astro, be sure to check out more posts about Astro on my blog.


Related Posts

Find posts on similar topics:


Support me via buymeacoffee

If I helped you solve a problem or you just like my content, consider supporting me by buying me a coffee.

Buy me a coffee

Comments