How to embed a clickable link to a random note in another note via a Templater template?

What I’m trying to do

In my dashboard, I would like a link to a random note from a specific folder to show up every time I open the dashboard.

Things I have tried

Currently I have this Templater template:

<%_*
const files = app.vault.getFiles().filter(f => f.path.includes("Inbox")); 
const random = Math.floor(Math.random() * (files.length - 1));
const randomNote = files[random];
_%>  
[[<% randomNote.basename %>]]

It does the right thing if I simply run Insert Template.

Next, I added the following line to my dashboard:

`<%+ tp.file.include("[[Random Inbox Note]]") %>`

Problem: the text that is produced is correct, but formatted as an inline codeblock, and it isn’t clickable, as in the following image.

So the same template works correctly when I run it manually, but produces an inline codeblock when run automatically.

Can someone help me figure out how to get Templater to insert text that is processed as markdown?

i’d try tR += "[[Some note name]]" or maybe even without the quotes…

The only syntax that produced anywhere near the right results is:

tR += "[[" + randomNote.basename + "]]"

But the same results: an unclickable codeblock rather than interpreted markdown.

then write what you need to write outside the scope of the templater script, after %> swhere and if you need dynamic input you only add the wrapper around the dynamic parts…?

I’m sorry, I don’t understand what you’re suggesting.

never mind, i’m not in obsidian now…sorry…

i have to ask also: are you in live preview or source mode after insertion??

In read-only, I only see the codeblock.
In live preview & edit, I see the raw tp.... call.
That much is as I’d’ve expected.

i think the problem is that the dashboard file is not a template so the templater script needs to be triggered
so what you want to accomplish should be done through dvjs, which will trigger on its own

<%+ – This syntax is most likely going to be unsupported in a soon to come version of Templater, due to its inconsistency to produce results. It should, in theory, expand the template whenever you switch to Reading mode, and not do anything will in an edit mode.

You’re most likely better of using some dataviewjs script to generate this link, or potentially a pure Dataview script (using the hash() function to keep it from randomising the note all the time).

The point being that since dataview query are meant to produce dynamic results all the time, when combined with producing something random you need to turn down the randomness a little related to often you want new random notes. The aforementioned hash() function can be used to produce a new random value every day/hour/… depending on the seed you give it, so that it’ll return a consistent value even when the query runs multiple times. Various methods to use this can be found in this forum.

1 Like

Fair enough. I’ll take this as a solid answer, and look for a different approach.