Source Your Own Daily-Quotes in Obsidian
Tags: obsidian, pkm, productivity, zettelkasten
Published: 2021-06-07 19:31:29

**Hey! This post has gotten a bit old and I'm no longer using the solution described here myself. I will keep the post around, but now recommend taking a look at either [this plugin](https://github.com/decatetsu/local-quotes) or [this more elegant solution](https://forum.obsidian.md/t/displaying-a-random-quote-in-my-daily-note/27225/3). Though I can't vouch for either myself, I no longer feel confident in my own solution decribed below as well.**

---

In this post you will learn how to collect quotes in Obsidian and then use them inside your daily notes for inspiration. Either as a replacement or in addition to the quotes from https://quotes.rest/ many people already use.

## Intro

If you use Obsidian there's a good chance you heard about the concept of daily notes. They are a great tool to add to your workflow and Obsidian makes it very easy to use them via its native Daily Note Plug-In. Also, if you have looked for inspiration in how other people set-up and use their daily notes, it's safe to assume you've seen at least a few that make use of [Templater's](https://silentvoid13.github.io/Templater/) `tp.web.daily_quote()` function to query https://quotes.rest/ for an inspirational quote to display for the day.

This is great! But in keeping with the spirit of letting your notes represent _your_ interests and helping you remember what you learn, I thought it would be cool to collect my own quotes from the things I read, watch and listen to and then display a random one of those in each of my daily notes.

## What You'll Need

If you want to replicate the whole workflow described below, you will need Obsidian itself and then:

* Enable the "Daily notes" plugin from the Core plugins
* Install the [Templater](https://silentvoid13.github.io/Templater/) Community plugin

## How I Collect My Quotes

When I process some kind of media to incorporate it into my vault, I collect quotes I find interesting, beautiful or which simply describe the contents of the piece of media eloquently or succinctly.

For each of these, I create a new note with a title of the form `(QUOTE) "This is the beautiful, eloquent quote I captured."` (So to be clear: the quote itself goes inside the title.)

I then you'll need to put at least the tag `#quote` inside the body. You can additionally put whatever you want in there. But for this to work, you will need at least that tag - or another tag of your choosing you will put in all and only your quote notes - inside the note.

**So, to recap**: quote goes in the title, `#quote` (or a tag of your choosing) in the body.

## Create A Script to Link to a Random Quote

Now, create a new note wherever you create your templates and put the following script in there:

```javascript
<%* 
	const quoteFiles = []
	
	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 && "#quote" === t.tag) // filter out all tags that are not "#quote"
		if (tags.length > 0 && filename.startsWith("(QUOTE)")) { // list will contain at least one tag for the relevant notes, also filter out all notes that dont start w/ "QUOTE"
			quoteFiles.push(filename.slice(0, filename.length - 3)) // cut off last three characters from filename, otherwise the links would contain `.md` at the end
		}
	})
	
	const randomIndex = Math.floor(Math.random() * quoteFiles.length)
	
	tR += `[[${quoteFiles[randomIndex]}]]`
%>
```

And then put this one inside your daily note template:

```javascript
<% tp.file.include("[[Random Quote]]") %>
```

That's it!

Here's an example from my daily note from today:

![an example from my vault](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nynlt9efxi46xgd8p5pv.png)

(The quote is from Jeroen Fieren's great post [The Opposite Collector’s Fallacy](https://technosoof.wordpress.com/2020/05/23/the-opposite-collectors-fallacy/))
 
## Thanks

In creating this, I had help from users torantine, SkepticMystic and koala from the Obsidian Discord Server. Also, the function above is _heavily_ based on the `findTargets()` function from the [tag-wrangler](https://github.com/pjeby/tag-wrangler) plugin.

Also, thanks to @thedyslexicpoet for catching a bug in my original script.

## Edits

2021/07/24:

Fixed a potential bug in the script by removing the `'` in `don't` in this line:

```javascript
if (tags.length > 0 && filename.startsWith("(QUOTE)")) { // list will contain at least one tag for the relevant notes, also filter out all notes that don't start w/ "QUOTE"
```

Turning it into this:

```javascript
if (tags.length > 0 && filename.startsWith("(QUOTE)")) { // list will contain at least one tag for the relevant notes, also filter out all notes that dont start w/ "QUOTE"
```