Exporting dataviews to plain markdown with templater

The requirement to do single or multiple values of the wanted_tags, do cause the query to be a little more hairy, but it seems like the following template should work:

<%*
const dv = app.plugins.plugins.dataview.api

const query = `
LIST aliases
FROM [[]]
FLATTEN all(map(choice(typeof(this.wanted_tags) = "string",
        list(this.wanted_tags),
        this.wanted_tags),
        (m) => econtains(file.etags, m))) as allFlag
WHERE file.path != this.file.path
WHERE aliases AND allFlag
`

const result = await dv.queryMarkdown(query, tp.config.target_file.path);
if ( result.successful ) {
 tR += result.value
} else {
  tR += "~~~~\n" + result.error + "\n~~~~"
}
%>

This require a frontmatter tag of either of the following (just one of them, though) :

---
wanted_tags: "#recipe"
wanted_tags: [ "#recipe" ]
wanted_tags: [ "#recipe", "#something" ]
---

You might want to verify the query in your environment as a normal query, just to verify that it’s correct. Then after that verification, you should be good to go.

Note that I’ve used econtains(file.etags, ... ) to match against the entire tag, so no partial tag matches will survice, like if you also had #recipelist, it wouldn’t match against that. This also requires the usage of "#tag" instead of just tag. So depending on your setup, and level of errors, the query/template could be modified.

But hopefully, this should get you going!

1 Like