August 1, 2022 obsidian templater js automation ☕️ buy me a coffee
I’ve recently started using Obsidian’s Daily Notes feature to keep track of what I’ve been doing. From the start I wanted to use Templater to dynamically create my daily notes such that each note to link to the previous one. And I’m not talking a simple -1d
offset. I’m talking properly analysing the available files to then pick the most recent one akin to a Singly Linked List
⚠️ First thing first, this isn’t a Templater tutorial so head to the documentation to learn how to do this yourself.
🧑💻 Go here for the complete code
Firstly, let’s scrape the file system through the app.vault
api and exclude any notes without Daily Notes
folder and let’s also exclude today’s (hard-coded for now) notes name:
files = app.vault.getMarkdownFiles()
.filter(file => file.path.includes("Daily Notes"))
.filter(file => !file.path.includes("2022-08-01"));
Next we’ll evaluate how long ago the file was created based on it’s basename
now = Date.now().valueOf();
files = files.map(file => {
file.parsed_time = Date.parse(file.basename);
return file;
});
Finally we’ll sort the array and select the most recent one
files = files.sort((a, b) => a.parsed_time < b.parsed_time);
Within a template we just call:
<% tp.user.tag_previous_daily_note(tp.file.title, "YOUR DAILY NOTE PATH") %>
tag_previous_daily_note.js
function tag_previous_daily_note(filename, path) {
files = app.vault.getMarkdownFiles().filter(file => file.path.includes(path)).filter(file => !file.path.includes(filename));
now = Date.now().valueOf();
files = files.map(file => {
file.parsed_time = Date.parse(file.basename);
return file;
});
files = files.sort((a, b) => a.parsed_time < b.parsed_time);
return files[0].basename;
}
module.exports = tag_previous_daily_note;
daily_notes_template.md
---
created: <% tp.file.creation_date() %>
tags: daily/<% tp.date.now("YYYY/MMMM") %>
---
# <%tp.file.title%>
---
**Previous** [[<% tp.user.tag_previous_daily_note(tp.file.title,"Daily Notes")%>]]
---
<% tp.file.cursor(1) %>