Open the templater command with <%*
to make it a Javascript command
<%*
TFiles are objects that represent a note in Obsidian. Templater has an easy way to find a TFile for a note.
const file = tp.file.find_tfile(tp.date.now("YYYY-MM-DD"))
tp.system.prompt
will let us input some text that we can use later on in the template command. The prompt
method is asynchronous. This means we need to put an await
in front of it. There are quite a few async methods in Templater. Don't forget to await
them!
if (file) {
const loggedItem = await tp.system.prompt("What's Up?")
const time = tp.date.now("HH:mm")
Like when we fetched a random note, we can use the Obsidian API to read the contents of any note! app.vault.read
takes a TFile. All this code does is get the contents of the daily note, converts it to an array and adds our text from the prompt
const content = await app.vault.read(file).split("\n")
const index = content.indexOf("### Log")
content.splice(index + 1, , `- ${time} - ${loggedItem}`)
Finally, we replace the contents of the daily note file with the updated contents including our logged item.
await app.vault.modify(file, content.join("\n"))
If no file can be found, we can display a notification!
} else {
new Notification("No Daily Note Found!")
}
-%>
In Templater settings you can add templates to the command palette and then assign a hotkey to them.