How to add links to random notes to daily note

Hi all,
I tried, but failed, to find a good way to add a given number of links to random notes in a given folder (my 2nd brain notes) to my daily note. I’d like to add these links to my daily notes as a way to force myself to review a certain number of notes every day.

Is there a way to implement this? I’m certain it should be possible with JS in templater, but I can’t seem to get the paths right.

Thanks for any help.


1 Like
const files = app.vault.getFiles()
const random = Math.floor(Math.random() * (files.length - 1))
const randomNote = files[random]
dv.paragraph('[[' + randomNote.basename + ']]')

@dhniceday this is great. Is there a way to not have it generate a new random link every time I accidentally enter the code in edit mode? I.e. generate the randomNote variable only once.

The best way to make any dataview not refresh itself, is to include it in a template.

So if you add that code into a dynamic template section of your daily note template and send the result to tR, it’ll be static for that day.

Unfortunately, that is not the case for me. I added the code to a template that is loaded when a new daily note is generated. However, this will still change the link every time I reopen the daily note or switch between read and edit view.

I guess you didn’t see the note I made on tR, or understood it, for the following code only stores the result of the code into your new note:

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`

I’ve also included the option to specify how many random notes you want by changing the first line to whatever number of random notes you want.

1 Like

This is great, thanks a lot.
I figured your comment was connected to the first.

What exactly do you mean with tR?

Templater can run in different modes, and can do various stuff in those modes, one of them, the Javascript Execution Commands, using <%* to start the template block, allows for javascript execution, and it has a special way of doing output.

In this mode, you can append stuff to the new note by appending text to the tR variable, like you see in the last line of my template. More info on this, and other modes, can be seen here: Execution Commands - Templater

1 Like

Thanks, I’ve been looking for something almost exactly like this, but with a couple additional additional improvements. Is it possible to also:

  • Exclude certain directories? i.e. the attachments folder
  • Exclude notes that don’t exist yet? (but have links to them)

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