Templater: How to get a list of md files styled with the data from YAML properties to retrieve a file name

What I’m trying to do

Hello! I am creating a templater template to allow me to efficiently take literature notes. What I have been unable to do is to code a function to allow me to search within my biblatex references for the right reference to tag the note I am working on. To clarify what I mean, let me explain my setup briefly:

  • Biblatex references correspond each to a markdown file in a folder called “references” within my obsidian folder. The name of the markdown file is the same as that of the biblatex key.
  • These markdown files that correspond to biblatex references are all tagged in this way: tags: [reference/book, bibkey/22364ah, etc, etc], where reference is a tag used to identify the md note as a reference note, bookis the biblatex entry type (book, mvbook, inbook, etc.), and 22364ah is the actual biblatex key for the reference (and also the filename itself).
  • Following the format of the Obsidian citations plugin, my md notes corresponding to biblatex references all have within the YAML properties the usual biblatex fields such as author: ["Doe, John"], title: "The Greatest Book Ever", etc.

With this setup in mind, what I want is this: When creating a new permanent note with templater I want to be shown a list of my references, so that I can select one so that these are added as tags of the form bibkey/this, bibkey/that, etc to the permanent note I am creating.

Things I have tried

I was able to get partial success by using this code:

var annotation = (await tp.system.suggester((item) => item.basename, app.vault.getMarkdownFiles(), false, "Select source of annotation/citation")).basename;

With this, I would be shown a list of all md files in my vault so that I can pick up the right one to be assigned to variable annotation. Then, I use this variable to create the appropriate tag.

Although this function works, it has several limitations I would like to address:

  • It searches all the notes and not a specific subset of notes (I would rather search only those tagged “reference” or those in the “references” folder).
  • It only shows the note title, but it is difficult to choose the right note without more context. Ideally, I would like it to show some information related to a file from its YAML properties—say, the value for author, date and title (when they’re set) at the very minimum—to make it easier to know what file I am looking for.

So, if you want to filter based on a tag you are going to have to integrate Dataview.

The following should produce what you want. Feel free to play around with the order of the the title, author, ect.

const dv = app.plugins.plugins.dataview.api;

const func = (item) => `${item.file.name}${item.author ? " - " + item.author : ""}${item.date ? " - " + item.date : ""}`;
const annotation = (await tp.system.suggester(func, dv.pages('#reference or "reference"'), false, "Select source of annotation/citation")).file.name;

tR += annotation

Relevant links

P.S. It’s good form to not use var anymore. If the variable is unchanging, use const instead, and if it might change, use let instead. more info

1 Like

Thank you so much for the code! It definitely works. I have zero knowledge of JavaScript, but for whatever reason Obsidian would complain that annotation was undefined when running the template, so I had to change const annotation to var annotation for it to run properly.

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