Bases: file.backlinks and file.links improvements

Use case or problem

Obsidian Formula running

  • file.backlinks displays a full path backlink (e.g. Folder A/File A.md)
  • file.links displays just outgoing links (e.g. File B)

Proposed solution

Request 1

I would like to separate those links with “,”
For example, “[[A]], [[B]], [[C]]” instead of “[[A]] [[B]] [[C]]”. It would make it easier to see how many links there are

Even better if I can make a list and I can scroll in Base, for example

  • [[A]]
  • [[B]]
  • [[C]]

Request 2

I would like to switch from [[Folder A/File A.md]] to [[File A]] in case of file.backlinks

It would make the view cleaner and less clutter, especially files in a deep nested folder (e.g. Folder 1/Folder1.1/Folder1.1.1/File A.md)

Current workaround (optional)

I try function replace() but it doesn’t work

I think, to get the desired display of the backlinks, you can use something like:

file.backlinks.map(value.asFile()) 

or

file.backlinks.map(file(value)) 

And if you want to have a comma separated list, this should work :blush: :

file.backlinks.map(value.asFile()).join(", ") 

Using .toString() instead of join() could also potentially be an option but I’m currently not sure anymore :see_no_evil_monkey:

We will make file.backlinks look like file.links (short / no path). You can always get the path from the link.

We won’t do the other changes.

Thanks for your suggestion.

The first 2 formulas work like a charm. However the last one whether .join() or .to String() will convert links into string, therefore unable to interact.

Yes :sweat_smile: … I can only guess that something changed since my previous post :innocent:
(I’m pretty sure I tested either .join() or .toString() and it was working fine “out of the box” at that moment :blush: )

A workaround, for comma separated list, which was shared on discord and keeps the links clickable could be:

file.backlinks.map([value.asFile(),", "]).flat().slice(0,-1)

This will only work as expected within the base though :blush:
I mean, if you “copy the base to clipboard” (pasting it as a markdown table somewhere) the clickable comma separated list of “links” will become a list of paths with extra commas in between the values…

A more complete workaround would be:

file.backlinks.map([link(value.asFile(),value.asFile().name),", "]).flat().slice(0,-1)

… but you’ll still get extra commas in between each link when copying the base to clipboard :sweat_smile:

2 Likes

That’s what I’m taking about :ok_hand:

Thank you for your clever code. :folded_hands: