Dataview table of all files in directory_a with column containing list of all files in directory_b that backlink to each given file/row in directory_a

Sorry, I couldn’t find a clear way to describe my problem in the subject line of this post.

But what I’m trying to do is:

  • Create a table of literature notes from directory_a
  • Make a column that holds a list of all files in directory_b with backlinks to each file/row from directory_a

The files in directory_b are syntheses of multiple literature notes that use backlinks to the source material. These syntheses typically have a many-to-many relationship with the directory_a files.

The table I have write now is very basic, whether created using DQL or dataviewJS. E.g.,

dv.pages('"path/to/directory_a"').map(p => [
	p.file.link,
	p.Title,
	p.Author,
	p.Year,
	p.Claims]))

Were I trying to put together something roughly equivalent to what I’m trying to accomplish using JSON, I’d might use nested for loops, with the outer iterating over directory_a and the inner over directory_b, evaluating for instances in which directory_b[j] contains a backlink to directory_a[i], and appending a backlink to directory_b[j] to an array of values corresponding to a key referenced_by in the JSON object for `directory_a[i]. There are alternative ways to accomplish this, of course, but I hope this communicates the general idea of the relationships between the files/directories and how I’m trying to treat/create cross-references between them - but instead of JSON objects, I’m dealing with Dataview table rows.

I feel like there must be a fairly straightforward way to do this using dataviewjs, but despite having a passing familiarity with Javascript, I’m still trying to wrap my head around a lot of the dataviewjs syntax. Someone I spoke with suggested I just needed to use the WHERE function in DQL to accomplish this, but that didn’t seem to work, and I’m pretty sure WHERE acts on what FROM returns, and the backlinks in directory_b files aren’t contained within the files in directory_a from which the FROM function pulls the table rows. But if there’s a way to do this with DQL, I’m definitely open to that, too.

I also tried creating an inline property within each given literature note called referenced_by that automatically populated itself using an inline dataview query. I got it to sort of work with =this.file.inlink, but couldn’t figure out how to use an inline query to restrict the returned files to those contained within directory_b. And even so, when I tried to include the unfiltered, query-populated referenced_by property as a table column, the cells in that column ended up rendering with the value of <empty string>.

I could obviously just manually add a backlink to each directory_b file to a frontmatter property in each corresponding directory_a file, but my goal here is to streamline and automate as much of my workflow as possible, and being able to skip that step would be very, very nice given the quantity of literature notes and syntheses I’m working with.

Any help is appreciated, thank you for your time!

In DQL given a file reference (which you’ve got in every row of your table), you’ve also got the file.inlinks giving you all inlinks to that particular file. Then the task becomes one of filtering out just the one from directory_b, and that is easily done using a combination of filter(file.inlinks, ... in combination with meta() to pull out the path part of the link.

I made a simple test setup, where in the base document I got link to all my “literature notes” (A-1, A-2, A3). In the notes I had links to prev/next (just for the fun of it). And then I made another directory B which had backlinks to the notes, if it’s named “Links23” it holds the links to 2 and 3. The query looks like this:

```dataview
TABLE fromB as "Dir B", file.inlinks as "All"
FROM "ForumStuff/f70/f70556/A"
FLATTEN list( filter(file.inlinks, (inlink) =>
   startswith(meta(inlink).path, "ForumStuff/f70/f70556/B")
 )) as fromB
```

And in my test setup it produces this output:

As can be seen the fromB contains only links from the B directory. You will of course need to adapt the query with folders matching your setup.

1 Like

That solved it, thank you so much for your help!

Alright, so I have a bit of a follow-up question if you don’t mind.

I want to do basically the same thing, but only if the current file reference is the value of a specific property (could be either inline or frontmatter) on another page. I’m been trying to adjust the syntax from your solution to my initial problem, but I’m stuck again

Please start a new post, even if it’s closely related. This since that would allow for updated description, example and references. It’s not clear which changes you want to get done.

Got it, thanks for the tip!

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