Some time ago, I asked how to get a list of section that have a certain tag, see List heading of tagged section, but I got no replies.
I tried to implement it myself, and it works with the dataviewjs
code below:
const tag = "#MyTag"
const regex = new RegExp('#+ (.*)\n.*' + tag + '.*', 'g')
let table = []
for (let page of dv.pages(tag)) {
const contents = await dv.io.load(page.file.path)
const matches = contents.matchAll(regex)
let at_least_one_match = false
for (const match of matches) {
at_least_one_match = true
table.push([
page.file.mday,
dv.sectionLink(page.file.name, match[1])])
}
if (!at_least_one_match) {
// this is to get pages where the tag is a property or a the very top.
table.push([
page.file.mday,
dv.fileLink(page.file.name)])
}
}
dv.table(["Modified", ""], table.sort(a => a[0]))
This results in a table like
listing sections or pages (if a section for the tag is not found).
One Question: The sort statement does not seem to work. Any hints on that?
I am also open to any feedback and improvements of the code!
Thanks!