I’ve been trying to change the frontmatter from within a Templater template, and it mostly goes OK, with the exception on when the frontmatter is present, but empty like in the following file:
---
---
Empty frontmatter
In this particular case I can’t seem to insert a new value. Is this just me doing it the wrong way, or is this a bug/feature of either Obsidian and/or Templater? Could someone enlighten me, as to what to do with this, or how to insert a value into the empty frontmatter?
Files used for testing
I’ve got three files to show this behavior:
---
Something: Somevalue
---
NiceFrontmatter.md
---
---
EmptyFrontmatter.md
NoFrontmatter.md
Each block is a file named like the body text
My Templater template to trigger behavior
<%*
const filenames = [
'NiceFrontmatter',
'NoFrontmatter',
'EmptyFrontmatter' ]
for (let name of filenames) {
const theFile = await tp.file.find_tfile(name)
const suggestion = tp.date.now("HH:mm:ss SSS")
await app.fileManager.processFrontMatter(theFile, (fm) => {
console.log(suggestion, theFile.name, fm)
if (fm == null) {
console.log('Big red flag')
fm = { 'Nothing' : 'No value' }
} else {
fm['Created'] = suggestion
}
})
console.log("done")
}
%>
Result after inserting the template
A screenshot of the Console from Developers Tool:
Both NiceFrontmatter.md and NoFrontmatter.md got the Created: 19:27:18 ... values, but as shown in the screenshot the EmptyFrontmatter.md has got fm == null and fails miserably when trying to set the fm (which is to be expected). I’ve also tried returning a new dictionary, like in return {} (or similar), but that fails as well.
Thanks a lot! And I understand it correctly, that I don’t need to do anything else, but just wait for that upcoming release?
For those impatient, I found a workaround where I throw a custom error message from within app.fileManager.processFrontmatter, and then read the entire file, do a regex replace, before writing the modified file back again. Not for the faint of heart, but it seems to work.
A workaround using try-catch -> app.read/app.modify
For those impatient, the following seems to work as a workaround:
Not entirely sure if my mapping will work in all cases, so care should be taken especially if using arrays or links or more complex objects to be inserted in the frontmatter.
@joethei, could this issue also be effecting the iOS app? I have a custom plugin that adds front-matter to notes that typically don’t already have front-matter. While it’s a bit hard to troubleshoot, I’m fairly confident the following code is throwing an error on iOS: