Meta
up:: ๐งช Code Diary
type:: #log/code
language:: JavaScript, Plugin - Templater
program:: Obsidian
topics:: Coding
Random Evergreen Notes
Language:: JavaScript, Plugin - Templater
Program:: Obsidian
Summary
This script will insert 5 random ๐ฒ evergreen notes upon creation of new note with the included template
Code
- Include this in the desired note template:
<% tp.file.include("[[Template, Random Evergreen]]") %>
- Make a "Template, Random Evergreen.md" file and add this:
<%*
const randomEvergreenNotes = []
app.metadataCache.getCachedFiles().forEach(filename => { // get all filenames in the vault and iterate through all of them, calling a function for each of them
let { tags } = app.metadataCache.getCache(filename) // get the tags in the file w/ the given name
tags = (tags || []).filter(t => t.tag && "#evergreen" === t.tag) // filter out all tags that are not "#evergreen"
if (tags.length > 0) {
randomEvergreenNotes.push(filename.slice(13, filename.length - 3)) // removes first 13 characters (03-evergreen/) then cuts off last three characters from filename (.md)
}
})
// loop through and display 5 random notes
var i = 0
do {
const randomIndex = Math.floor(Math.random() * randomEvergreenNotes.length)
tR += `- [[${randomEvergreenNotes[randomIndex]}]]` + "\r\n"
i++
} while (i<5)
%>
Explanation
Result
up:: ๐งช Code Diary