Block links in DataviewJS

Hi everyone!

I’m pretty sure this isn’t possible but I just thought I’d try :smiley:

I am displaying a random block selected from certain notes using DataviewJS:

let bookList = dv.pagePaths("#books");
let book = bookList[Math.floor(Math.random() * bookList.length)];
let quotesList = [];
for (q of dv.page(book).file.lists.values)
	quotesList.push(q.text)
dv.paragraph(quotesList[Math.floor(Math.random() * quotesList.length)])

Is there a way to link to that specific block instead of the page it was taken from?

I don’t think so, because looking at the dv.pages object returned by DataviewJS, we have header and link and secion, but nothing that would give the block id or link.

Would be interested to hear if I’ve missed something or if there is a workaround.

Thanks!

This might not be the entire truth, but it might help you a little bit on your way to solve your issue. DataviewJS is able to do blockLinks, but I can’t find a good way to determine which blockId to use.

Upon further investigation I found that by adding ^q1 (or whatever ID you want) at the end of a list item, I could designate the blockId to that item, and furthermore I then discovered that link changed into this more specific target, whilst section remained to the larger section holding that item. This definition also added the blockId field to metadata of that list item, but how to get the default blockId I’ve not discovered yet.

So the question then becomes if either you could be bothered to add the blockId definition to all of your quotes, or whether there is a way to determine the blockId of the list item you’ve already found?

tl;dr: Until someone/something defines the blockId by adding the ^yourBlockId to the end of your block, the blockId is not defined.

I did some more research and found the following:

  1. Block reference ID generation question, #2 (which also references the 0.9.5 release notes) – This indicates the two methods of generating blockId’s: By searching/auto-complete, or by definitions

  2. Forum post Right click menu item to generate block … reference lead me to Copy Link to Block – Further indication that the blockId’s don’t exist by default

  3. Copy Block Link plugin – A plugin to actually generate the blockId’s, which turns out to actually add the definition to your actual file, not only add it to the metadata

  4. Available metadata in dataviewjs (see the end) – blockId only present if defined

So to conclude my research:

  • BlockId’s are not defined all the time, and either needs to be generated when doing a search or autocomplete like in the [[SomeNote^...]] case, or be defined by adding ^YourId at the end of a block
  • If the blockId is defined, then dataviewjs would present it, and if applicable the .link would include it (differentiating it from the .section link.

When testing this out in a Sandbox vault, I’ve also concluded that until you somehow define the blockId, it’s not present. And I also found that when you do the auto-completion block link, it actually insert the blockId definition for you.