Can the notes filtered by dataview be set to automatically add a link to the current note?

Things I have tried

In the process of configuring Obsidian for the first time, I have divided many notes into modules such as themes, plugins, synchronization schemes, etc. Now I want to try to index these notes in a new note using MOC. dataview can easily filter out all relevant notes and present them in tables, lists, etc., but it does not establish a WikiLink between the current note and the target note. Is there a way for dataview to filter out the specified notes and add a WikiLlink to the current notes as well?

What I’m trying to do

Table, list and other formats can’t automatically create a link.

I’m wondering if dataviewjs supports this feature or if there is a relatively automated way to generate links (like a script?) .

Creating a table with links to the files is right there in the docs:

https://blacksmithgu.github.io/obsidian-dataview/queries/query-types/

Dataview is in its nature a dynamic tool, which creates on the fly links to other documents, like Alan described above.

However, it doesn’t out of the box create static links which can be used for backlinks and static connections between notes.

Which variant are you wondering about?

Thank you both for your enthusiastic replies, I may not have described clearly what I was hoping to achieve, here is an image showing:


I would like to get all the notes for #game tags (e.g. Outer Wilds, Minecraft, etc.) with the help of Dataview’s query function, but at the same time would like to create static links, as shown in the second image. Is there any way to create them automatically, instead of manually typing [[]] one by one according to the dataview search results?

Thanks for your answer on Daraview features, I now understand the difference between fly links and static links.

If dataview doesn’t create static links out of the box, are there any other tools or plugins that you would recommend to do this?

I would like to avoid adding static links manually, on the one hand it may miss some notes and the result is not complete, on the other hand if I add new notes later I need to update them manually, which is also a lot of work!

The combination of Templater and Dataview can be really powerful. Where you can in addition to just include the query into the new page, you can also opt for variants where you insert the result of the query into the page (using Execution commands), which makes it a static result.

There is also a Text Expander plugin I see referenced every now and then, which is also capable of expanding searches. This has the added benefit that it can be re-run, to update its contents on alter stage like you talk about here. It’s syntax and results are a little different, but it’s well worth the time to look into that plugin, as well.

And surely there are main other more specific to various cases, but those three are the top ones which I come to think of in this kind of scenario.

(Your response also triggered something I’m going to investigate a little, and that is how hard would it be to do queries which also checks whether the current page already has links to that page, and then only reports back additional new links? )

Just to elaborate a little on the difference between dynamic and static links, it’s also relevant when it comes to metadata in between the notes, such as backlinks, inlinks and outlinks. The query result won’t be a part of those metadata thingies, whilst static links would.

So in some case, like building indexes or MOCs or whatever you like to call them, it’s in some cases nice to build the group of links logically connected by some topic/area/function/… which we as humans see the pattern of, and can benefit from when reviewing later on. And in some cases, it’s just nice to see all the connections between some topics.

One case I’ve experienced is related to doing workouts. I can tag them with #workout, but that could both mean I had a workout, or I made changes or had thoughts about how to develop it further. The same tag, but two very different meanings. All of the first can easily be connected into my main Workout page, without any further connections, but the latter on related to how my workout has developed I feel is more natural to have statically linked from the same page.

And that’s a contextual difference I can make as a human, but it’s hard to incorporate into scripts/queries. So you’ll see in due time, that you’ll want both the hardcoded links which are helps you connect the dots, and also the query based links which summarise or links in a looser sense relevant stuff.

1 Like

Really Really thanks for the advice! I’m not familiar with Templater, and its documentation is a bit complicated, so I’m not sure which features to focus on.

If it’s convenient, would you mind sharing a simple demo of using Templater in conjunction with Dataview, where the final result is the generation of several static links with tag #game notes as mentioned above.

Simple? Well, that depends on eyes viewing, but here is something which works in my world, at least.

Instructions on how to setup the template, optional with a hotkey

There is some setup, which might seem a lot the first few times, but it’s not as bad as it seems.

  • Install the Templater plugin: Installation - Templater
  • Dedicate a folder for templates in your vault, like templates
  • Create a file in this folder, i.e. staticDataview (or whatever you feel like)
  • Copy the content from below into it
  • If you want to trigger it by hotkey, also do this:
    • Go to Settings > Templater > Template Hotkeys
    • If there isn’t a free field, hit “Add new hotkey for template”
    • In a free field, enter your template, like templates/staticDataview.md
    • Hit the + icon, which gives you a new dialog
    • Find your “Templater: Insert … staticDataview”, and hit the + icon here
    • Hit the hotkey you want to use to trigger this template. Hover with your mouse over it to check that it’s valid, and not in conflict with other hotkeys. If in conflict, hit the x icon, and try another…
<%*
const dv = this.DataviewAPI

const pages = await dv.query(`
LIST
FROM #game
`)

if (pages.successful) {
  const gameLinks = pages.value.values
  // console.log(gameLinks, typeof gameLinks)
  
  // Alternative 1: A simple markdownList from dv
  tR += `## Games in a list\n\n`
  tR += dv.markdownList(gameLinks)
  
  // Alternative 2: One game on each line
  tR += `\n## Games\n`
  const games = gameLinks
    .map(g => {
      // Extract the gamename _after_ the last "/" and without the ".md"
      const gameName = /\/([^\/]+)\.md$/.exec(g.path)[1]
      return `[[${ g.path } | ${ gameName }]]\n`
    })
    .join("")
    
  tR += games
  // console.log(games)
  
} else {
  tR += `\n~~~~\n${ pages.error }\n~~~~`
}  
%>

The template starts of with grabbing a shortcut for the dv api, always nice to have around. Then it executes the query. I’ve formatted this so that you should be able to easily change it to another query by just replacing the query. Be sure to keep the line in front, and the line afterwards (the one with only `))

If the query is successful, we’ll handle it, and if not it’ll show an error message which hopefully will help you resolve the error and get a better result later on.

Inside the handling I present two alternatives for handling this result. The actual list result from the query is found in pages.value.values, so let’s store that away.

In the first alternative a header is written to the page where we started the template, by doing tR += ... . This way of handling the output is due to the template being a dynamic execution template, indicated by starting the template with <%*. Then I use a version of generating the list from the Dataview API which generates markdown (and not html).

In the second alternative, we need to do some more steps to only output one game link on each line. Going from the array into the lines is called mapping, and is done using .map(). The first thing in the mapping is to extract the game name from the full path to the note. Kind of cryptic, but it works, at least if the game is in a folder. If not, come back to me, and we can fix that case.

With the game name, and the full path, we can return (to be written into our note) a full link with the g.path for the full path, and the gameName in the alias. Adding a newline, so it looks nice. And after the mapping, join the lines together, before adding to the output, using tR += games.

How to debug dynamic execution templates?

You do it painfully. Sorry to say, but it’s a little cumbersome to debug these script, but what I’m doing is that I’ve got a dummy template, which I’ve got a hotkey to run, so that I run it over and over again.

Then I open up a workspace setup with the following files/panes:

  • The dummy template
  • A log file in both editing and reading view, so that I can see the rendered result
  • The Console from Developers Tools to view all my console.log() messages

And the latter is the true secret to debugging. Use plenty of console.log(something) in the template for starters. I’ve left some of them in this template, which you could uncomment, and see the result. When we log an object, the console allows us to uncollapse and dig into the details of that object, and then we can see what’s available.

So for example, for the gameLinks output, one can see something like the following:
image

Revealing that the full link is in path, and that this link has no subpath (to a header/section/block), and no alias, in display. This output will of course vary depending on the object, but it allows you see what’s available for further coding.

Hopefully, I’ve not made to many glaring errors when writing about this, and that it makes some sense. Always a challenge to get to the right level of explanation. Feel free to ask if something was extremely unclear, and good luck in implementing your own variation of templates like this.

Two caveats: Firstly, the regex will most likely fail if your games are in the root folder, if so ask me. Secondly, the query as it stands could very well report false positives, and the template doesn’t handle that. If this is the case for you, then refine the query until it just returns the games you want to add using the templates.

PS! Lastly, in this reply, another version of how to interact between dataview and a template is shown. Could possibly be interesting.

3 Likes

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