How can I autogenerate block reference?


I have a problem with upgrading my custom quote system.

What I already have

I have a note called “Wisdom” that contains simple quotes like this:

First wisdom nugget

Second wisdom nugget

Third wisdom nugget…

I also have a Templater template set up so that it fetches “Wisdom” splits into each quote and then randomly selects one of them to display as a quote like this:

Wisdom nugget:

Second wisdom nugget
— Wisdom nugget

It works fine.

What I want to upgrade

Sometimes when I see the quote I want to change it in some way so I want to extend this system so that instead of getting a raw copy of the quote’s text, I get a link to that quote inside the “Wisdom”.

What I know

I know about block links and the ![[...]] embedded links. I also know how to generate a link to an existing ^blockID with Templater.

The problem

I don’t want to manually micromanage if every single line in “Wisdom nuggets. md” has a ^blockID. The quotes in “Wisdom” can be added to the file in various ways. I write them there manually, I use the Note Refactor plugin, I copy-paste them, and I expect more ways to come in the future. I do not find the idea of manually adding ^blockID every time something gets added to the file acceptable.

What solutions I am considering

Templater checks

Whenever I run the Templater template, if it finds a quote without a ^blockID, it adds it generates one, and adds it to the file “Wisdom”

This requires that the check and the generation of new ^blockID happen from anywhere I would want to fetch a quote. This violates the DRY principle and I would like to avoid that.

Check when “Wisdom" is modified

Whenever the “Wisdom” file is modified, something finds all quotes without a ^blockID and then generates and adds them. This sounds like a much better idea but I do not know what that something is. Is there something, that can react to the file being modified?

Obsidian autocomplete function

I noticed that when I write [[Wisdom nuggets#^]] Obsidian gives me suggestions on everything, that is or could be a block link and auto generates ^blockID if there is none. Is there a way I could use this function with Templater? How would I do it?

It would be nice to sidestep the need to parse the file, check if there is a ^blockID on each line, and then generate some unique ^blockID.

So in the end the question is, can you help me find a better solution than checking every time any template wants to get a quote and replicating the code for every template that does that?

Meh… this all sounds very complicated to me. I’ve a similar setup but use instead dice roller by Jeremy Valentine. Instructions on how to set up random quotes is described in his extensive documentation.

By the way, if you click on this kind of quotes, dice roller will pick yet another random quote, which is nice

I’ve seen this solution around but that is not what I am looking for.

Intuitively this feels like the better solution, as it generates the ids when needed. So my question is more if you could streamline the process of getting quotes from your various sources.

In essence, would it work for you to either make a user script to get the quote, or by other means make a command which gets a quote. In either case, the id is generated
by that script if not already present.

Building a system around it either linked to changes detected in the file, or by timed checks of the file will most likely always be slightly heavier in computational cost, instead of changing it when needed (aka when getting the quote).

1 Like

I really like this idea. I am new to Obsidian and didn’t think of isolating the feature as a user script. It makes a lot of sense. Thanks!

1 Like

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