Templater multiple choice Tags

Things I have tried

<%*
const cache = app.metadataCache; const tags = Object.keys(cache.getTags()); tags.forEach(tag => {tR += + ${tag}\n});
if (query.length > 0) { let choice = await tp.system.suggester($tags, $tags, false, “Tags”)};
%>

What I’m trying to do

I would like to have in my template a query of all already used tags per multiple choice for selection

Thanks in advance.

1 Like

Try the following template, and see if it does something matching your requirements:

<%*
const dv = app.plugins.plugins.dataview.api
const allTags = Object.entries(app.metadataCache.getTags() )
   .sort( (a, b) => a[0].localeCompare(b[0]) ) // Sorted alphabetically
   // .sort( (a, b) => b[1] - a[1], "desc" ) // Sorted related to frequency

let selectMore = true
let selectedTags = []
while (selectMore) {
  let choice = await tp.system.suggester((t) => t[0] + "(" + t[1] + ")", allTags, false, "[Select more tags (ESC when finished)] - " + selectedTags.join(", "))
  if (!choice) {
    selectMore = false
  } else {
    selectedTags.push(choice[0])
  }
}

tR = "Selected tags: " + selectedTags.join(", ") 
%>

Trigger the template, and start selecting tags. Keep on selecting tags until you got the list you want, and then hit Esc to exit the loop, and get the tags selected inserted into your current note. You could of course do whatever you want with the selectedTags list after the loop.

Uncomment the .sort() you want according to how you want to sort the tags. You can just as normal, do the partial searches within the suggester to select the next tag, and when you select any tag it shows the currently selected tags with a prompt on selecting more or ending the selection by pressing the ESC key.

7 Likes

Not OP, but this worked perfectly for my needs. Thank you!!!

I have been looking for something like this for quite some time – thank you!

Unfortunately, I am not a coder, so the question might be naive, but: how do I change the code to filter only tags from a specific folder? Must be somewhere in the definition of the allTags constant, right?

I have tried to enter the folder within the app.metadataCache.getTags("folder 1/subfolder 2") but that did not work.

Help is highly appreciated! Thanks!

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