Programmatically create auto-updating links in YAML frontmatter

Hi all,

Recent Obsidian convert. I’ve been trying to create Templater templates where the related frontmatter property is automatically populated with a link to the folder note of the parent folder.

For example, if I create the new note:
/01.Action items/TODO.md

… then the frontmatter for this new note should contain:
related: [[/01.Action items/01.Action items.md]]

… where 01.Action items.md is a note I have created previously

Things I’ve tried

With considerable help from the @AlanG solution on this thread, I have a partial solution:

<%* 
// Add this at the end of your template file
const folders = tp.file.folder(true).split('/'); //breaks the folder path into an array
const parent = folders[folders.length - 1]; //gets the last element of the array
const foldernote =  tp.file.folder(true) + "/" + parent + ".md"

// Add/remove properties from the newly created note.
// The reason for the timeout is to wait until after the new note exists in your vault.
setTimeout(() => {
  // Get the path to the new file
  const newFile = tp.file.find_tfile(tp.file.path(true))
  // Process the frontmatter
  app.fileManager.processFrontMatter(newFile, (frontmatter) => {
    // Add a new field
    frontmatter['category'] = 'permanentnote'
	frontmatter['related'] = "[[" + foldernote + "]]"
    // Or delete the properties you don't want
    // delete frontmatter['Some frontmatter field']
  })
}, 300)
%>

This solution creates a valid link to the folder note in the parent folder, and associates it with the related property. However, if the path to the folder note is changed, then the link does not automatically update (as would be the case if I had created the link manually).

Is there a way that I can programmatically create links with Templater in such a way that Obsidian automatically keeps them up to date?

1 Like

As long as notes have unique names, Obsidian doesn’t need to know their path to link them.

So as long as folder notes have unique names, there’s no need to store or update their path in the referring note:

related: "[[01.Action items.md]]"

Edit: Notation of link corrected.

You’re presenting a related value of related: [[Note A.md]], which isn’t actually a link but rather a two-dimensional array with the value of Note A.md. If you rather use: related: "[[Note A]]" it’ll become a link recognised by Obsidian, so that if you change the name of that link later on it’ll be properly renamed automatically.

So add those quotes, and make it a link, and you’ll be in a much better place. :slight_smile:

1 Like

RTFM:

Internal links in Text and List type properties must be surrounded with quotes. Obsidian will automatically add these if you manually enter internal links into properties, but be careful to add them when using templating plugins.

Thanks for the suggestion. Luckily for me all folders are guaranteed to have unique names, as I am using the Johnny Decimal system.

For anyone else struggling with the same issue, this suggestion offered a partial solution. One further change was required: I am using the Foldernote plugin to simplify the creation of folder notes. I found that if I toggle off the Auto Rename option (meaning I would have to manually rename folder notes if I changed the name of a folder), then Obsidian automatically updates my template-generated links

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.