How can I add random notes to my daily note while ignoring some folders?

Things I have tried

I’ve tried using this templater snippet (from here)

<%* 
const noOfNotes = 5
const files = app.vault.getFiles()

for (let i = 0; i < noOfNotes; i++) {
  const random = Math.floor(Math.random() * 
                            (files.length - 1))
  const randomNote = files[random]
    
  tR += `- [[ ${randomNote.path} | ${ randomNote.basename } ]]\n`
}
%>

What I’m trying to do

I’m trying to add a few random notes to my daily note so that I can review them as part of my daily workflow. However I want to ignore certain folders like _tools/templates and my Presentations/Export. Currently I end up with links to templates and also links to notes that don’t actually exist in my vault.

Do you mean something like the following?

<%*
const noOfNotes = 5
const dv = this.DataviewAPI
const files = await dv.tryQuery(`
  LIST
  FROM -"_tools/template" AND -"Presentations/Export"
`)

let randomList = []

for (let i = 0; i < noOfNotes; i++) {
  const random = Math.floor(Math.random() *
                            (files.values.length - 1))
  randomList.push(files.values[random])
}

tR += dv.markdownList(randomList)
%>

The query at the top could be written differently, but I made it this way so that if you want you can play around with the query, and modify to give you the list of notes you want to be included in the random notes list.

In other words, try out the following query on some page:

```dataview
  LIST
  FROM -"_tools/template" AND -"Presentations/Export"
```

The result of this query will be all the notes that your randomiser will pick from. If you don’t like it, modify the query, and get it to show the files you want, then insert that query into the template above.

3 Likes

Ah, perfect. Yeah that’s exactly the type of thing that I was looking for! Thanks!

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