Random note with specific tag

What I’m trying to do

Show the content of a random note with a specified tag in my daily notes.

I have 40-50 notes that I want to appear, randomly, in my daily notes (just one of these notes should appear each day). They have a specific tag, let’s say “#daily_random_note”.

The ideal solution would be a Templater/js code in my daily note template.

Things I have tried

The “open tagged random note” command from Smart Random Note plugin is very close to what I’m looking for, but it doesn’t fit well with a daily note template (even if I add a button) because I have to click it and change the file. I’m trying to find a solution that allows me to read these notes (even if I have to place the cursor over a link) from my daily note file.

The post below should show you a way forward, where you just modify the query to suit your needs of having a specific tag, and the number of links you want.

If you want the full content of the file, you need to add some magic related to using await dv.io.load() or similar. Examples of that can be found recently in this forum.

1 Like

this is not templater based, but i personally use dataviewjs to randomly show my permanent note on my home dashboard. the downside (or upside, depending on how you see it) is that it will also randomise if u close and reopen the note. copied it from from pseudometa (there’s a youtube interview with danny hatcher)

Random #PermanentNote

const numberToShow = 3
const notes = dv.pages('#PermanentNote')
	.sort(() => 0.5 - Math.random())
	.slice(0, numberToShow)
	.map(note => note.file.link);
dv.list(notes)

Hi holroy, thanks a lot! I’m having some trouble with dv.io.load, but the post you shared resolved the main problem.

thanks efemkay, very useful

Here is a fully working example, where you end up with your random content in the randomContent variable, and can insert it wherever you want using <% randomContent %>:

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

const candidates = await dv.tryQuery(`
  LIST WITHOUT ID file.path
  FROM #daily_random_note
`)
const randomNo = Math.floor(Math.random() *
  (candidates.values.length - 1))
const filename = candidates.values[ randomNo ]
const randomContent = await dv.io.load(filename)  
_%>

... 

<% randomContent %>

If you’ve already got a Templater execution block in your daily note template, then just insert that code block together with it somewhere in there. :slight_smile:

1 Like

Thank you very much holroy - perfect solution

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