I guess I just needed to go to the woods for the weekend to come back with a better idea of how to make Templater do what I wanted, which was make Obsidian notes using Denote’s naming convention with a single command. On Friday I got things as far as “make a Denote-formatted note,” but I couldn’t completely automate the file renaming. This afternoon I finally figured that out.
Here’s the user function I made, same as the last post:
// slugify.js -- placed in a directory where Templater knows to look for user functions.
function slugify(title,identifier,tags) {
title = title.replace(/^\s+|\s+$/g, ''); // trim leading/trailing white space
title = title.toLowerCase(); // convert string to lowercase
title = title.replace(/[^a-z0-9 -]/g, '') // remove any non-alphanumeric characters
.replace(/\s+/g, '-') // replace spaces with hyphens
.replace(/-+/g, '-'); // remove consecutive hyphens
tags = tags.replace(/ /g,"_");
tags = tags.toLowerCase();
slug = identifier + "--" + title + "__" + tags;
return slug;
}
module.exports = slugify
All it does is make a Denote-compatible filename out of stuff passed from the target file.
… and this is the actual Templater template:
<%* let title = tp.file.title;
let identifier = tp.file.creation_date("YYYYMMDDTHHmmss");
let tags = await tp.system.prompt("Enter tags, space delimited", "");
let slug = tp.user.slugify(title,identifier,tags);%>
---
title: <% tp.file.title %>
date: <% tp.file.creation_date("YYYY-MM-DDTHH:mm:ssZ") %>
tags: <% tags %>
identifier: "<% identifier %>"
---
<%* await tp.file.rename(slug); -%>
It:
- sets the value of title based on the filename of the note. That’s gathered when I add a new note using QuickAdd, which requests a note title.
- Makes a Denote-style identifier.
- Captures the tags from user input.
- passes title, identifier, and tags to that slugify.js function to make a legal Denote filename
- adds those values to the template placeholders in the frontmatter
- renames the file to match the output of the slugify function
I have it wired up to the CMD-CTRL n
shortcut via the QuickAdd plugin. So when I make a new note in Obsidian using that shortcut, I’m prompted for a directory, prompted for a note title, Templater puts the file down on the disk, I’m prompted for tags, then the note file is renamed. I could move the function into the template, but it’s useful to have it separated out so I can run it on pages outside the context of making a new note; such as when I’ve changed tags or title on a note. I’ve bound it as a standalone command to opt-cmd-r
for quick Denote-compatible renaming.
Utility? Well.
Links are still the challenge here. Obsidian is oblivious to Denote’s denote:
link format, which is how Denote relates backlinks and inter-note linking. And you can use obsidian.el to search for notes and follow Obsidian links from within Emacs.
So … the main utility here was “science project.”
And there’s some utility in setting things up to use Denote’s filename convention: It does a lot to make note content easier to migrate over time.
And I guess there was some utility in pushing the whole thing far enough to accept that Denote is simply not a good fit if you want your notes to be editable, interlinked, and available via mobile: None of the mobile org apps understand Denote’s link format, either.
I’m not sure which way I’ll go now that I’ve bottomed things out:
It was pretty cool to be sitting in a cafe on the Oregon coast this weekend, toggle Tailscale on my phone, and have read access to all my Denote notes via the web tool I built. And with SyncThing I can use my Drafts Denote action for capture.