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

<% tp.file.include("[[Template, Random Evergreen]]") %>
<%* 
    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