Easier Blogging in Obsidian With Templater
Just wanted to publish my Templater template that I use for creating new posts in the blog/
directory of my Obsidian vault:
<%*
let title = tp.file.title
if (title.startsWith("Untitled")) {
title = await tp.system.prompt("Title");
}
await tp.file.rename(title)
tR += "---"
%>
title: <%* tR += title %>
author: Ian S. Pringle
public: false
draft: true
created: <% tp.file.creation_date("YYYY-MM-DDTHH:mm:ssZ") %>
updated: <% tp.file.creation_date("YYYY-MM-DDTHH:mm:ssZ") %>
tags: []
---
# <%* tR += title %>
<% tp.file.rename(tp.user.slugify(title)) %>
<% tp.file.cursor() %>
Really there are three main parts and all but one are so basic as to be pointless to explain.
File Creation
An annoying thing about Templater is that the tp.file.title
method/variable does not actually equal the title of your post when a template is triggered at file start. This means that you end up just getting a bunch of Untitled
‘s all over your file because at the time of creation that was the title of your file. To get around this we just have to do a little JavaScripting:
let title = tp.file.title
if (title.startsWith("Untitled")) {
title = await tp.system.prompt("Title");
}
await tp.file.rename(title)
tR += "---"
What this says is “get the title, but if it’s ‘Untitled’ then get a prompt asking for the title of the file and then set title
to that prompt’s value.” With this you can then start using the value of title
anywhere you want in your template and it will be the actual file’s title.
I am currently undecided on how to handle my filenames, right now I’m writing “slugified” filenames for my blog posts. I should probably stop and maybe rename them, but until I take the time to figure out if that’ll work, I’m also renaming the file and “slugifing” the filename.