Dataviewjs to list distinct tags in all daily notes from this past week

What I’m trying to do:

Create a dataviewjs snippet returning a string that will contain all distinct tags included in my daily notes (named YYYY-MM-DD) from the current week.

example: if today’s date is 2023-12-08 and:

note 2023-11-30: (that is not in the current week)
#tagN
#tagZ
#tag6

note 2023-12-04:
#tag1
#tag2
#tag3

note 2023-12-05:
#tag1
#tag4
#tag3

note 2023-12-06:
#tag2
#tag5
#tag7

The snippet embeded in a week recap note should display the string:

#tag1 #tag2 #tag3 #tag4 #tag5 #tag7

Things I have tried

To start with, I tried this snippet that is supposed to return distinct tags from a folder

let tags = []
for (let tag of dv.pages('ObsiLog/journals').file.tags) {
 if (tags.indexOf(tag) == -1) {
 tags.push(tag)
 }
}
dv.list(tags)

but I get an error on the path to my journals folder where my daily notes are whether I use:
‘ObsiLog/journals’
‘/ObsiLog/journals’
‘/journals’
‘journals’’

Once working, it will needs to be tweaked to only push tags from the files’ week: if looking at a note from week 12, I only want to show the tags from that week in particular

Thank you for your help!

I solved my path issue, my dataviewjs code is now

let tags = []
for (let tag of dv.pages('"journals"').file.tags) {
	 if (tags.indexOf(tag) == -1 ) {
	 tags.push(tag)
	 }
}

dv.el("p",tags.join(" "))

My tags now appear as a string like I wanted instead of a list.

I now need help on filtering the results to show only tags from the current week of daily files

Here is some code to get all file links having a file.day within current week:

```dataviewjs

const currentWeek = moment().format("yyyy-W")

dv.span(dv.pages()
  .where(p => p.file.day)
  .where(p => moment(p.file.day.toISODate()).format("yyyy-W") == currentWeek )
  .file.link
  .join())
```

Note that file.day gets it value either of properly formatted date in the file name, as in your case, or from a date property within the file (allowing for filenames to either use localised dates or no date at all).

So redoing your unique tags code, combined with this week code, we could arrive at something like the following:

```dataviewjs
// Define some helper variables
const currentWeek = moment().format("yyyy-W")
const uniqueTags = {} // Using a set, which never adds a key twice...

// Loop through this weeks list of tags
dv.pages('"journals"')
  .where(p => p.file.day)
  .where(p => moment(p.file.day.toISODate()).format("yyyy-W") == currentWeek )
  .file.etags
  .forEach(t => uniqueTags[t] = true) // The value doesn't really matter

// Display the unique list of tags
dv.paragraph(Object.keys(uniqueTags).join(" "))
```
1 Like

@holroy

We’re almost there!

I just need the week number from the file name, not the moment
I want to be able to look back at a note from 3 months ago and see the tags from that week

So I would need some help on changing:

const currentWeek = moment().format(“yyyy-W”)

to something like
const fileWeek = dv.current().file.name.toISODate().format(“yyyy-W”)

which doesn’t work…

thank you for your help!

I changed
const currentWeek = moment().format(“yyyy-W”)
to
const fileWeek = moment(dv.current().file.day.toISODate()).format(“yyyy-W”)

in order to get the results from the same week as the file it’s called from

thank you for your help!

Here is my final code that works as expected:

``` dataviewjs
let tags = []
let toRemove = ['#Tasks','#Notes','#Dev','#Intake','#phone','#email']
const file_week = moment(dv.current().file.day.toISODate()).format("yyyy-W")

for (let tag of dv.pages('"journals"')
.where(p => moment(p.file.day.toISODate()).format("yyyy-W") == file_week)
.file.tags ) {

   if (tags.indexOf(tag) == -1) {
   tags.push(tag)
   }
}
    tags = tags.filter( function(e) {
      return !toRemove.includes(e);
    })
   tags.sort()
dv.el("p",tags.join(" ")
)
1 Like

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