Change yaml list with templater

I’m thinking that relying on the index of a frontmatter field is somewhat sketchy, so I think you’re better of handling the entire array of types. Locate the one you want to change, and change that before pushing it back to the frontmatter. This can be done in at least two different approaches, using Templater/Meta edit, or using the Obsidian api.

Using Templater & Meta edit

You’re almost there already using this approach, but as mentioned I would pull the frontmatter using tp.frontmatter.types (or tp.frontmatter["types"]), modify it, and then return it using the update() method.

It’ not guaranteed that you’ll get the definition format you want, and I’m not sure if Meta Edit can be persuaded to do your format.

Using the Obsidian API

There exists a method called app.FileManager.processfrontmatter() which can be used for changing the frontmatter “atomically”. There was an issue if your note had an empty frontmatter, but that should be resolved now. This thread show cases one way to use this method (albeit it’s also talking about the issue which now should be fixed).

The gist of it would be something like:

<%*
await app.fileManager.processFrontMatter(tp.config.target_file, (fm) => {
  const index = fm.types?.indexOf("l")
  if ( index != undefined ) fm.types[0] = "p"
})
%>

In my simple test, it seems to preserve the format of the frontmatter.

1 Like