Bases: Filter by backlinks folder

What I’m trying to do

I want to filter base to show only
files with backlinks; backlinks should be in folder _KB (or subfolders).

Working solutions with backlinks:

Function filter

file.backlinks.filter(value.asFile().folder == "_KB").length > 0
file.backlinks.filter(value.asFile().inFolder("_KB")).length
file.backlinks.filter(value.contains("_KB/")).length > 0

Function map

file.backlinks.map(value.contains("_KB")).contains(true)
file.backlinks.map(value.contains("_KB"))==1

Questions

  1. Can code be simpler/shorter?
  2. How to use another function like hasLink() instead of backlinks()?
    because backlinks function can be “expensive” to run and doesn’t update automatically

Related

  1. Do your formulas actually need the .length part? I did not test it for your examples, but for my filters it is not necessary.

  2. You can’t always replace the backlinks() functions. It is only possible if the respective notes also contain a link in the “opposite” direction. Then you could try to “reverse” your filter with something like .hasLink or file.links. But at least in all cases where I am using .backlinks, there has been no alternative.

  1. Yes code with filter works also in form:
file.backlinks.filter(value.asFile().folder == "_KB")
file.backlinks.filter(value.asFile().inFolder("_KB"))
file.backlinks.filter(value.contains("_KB"))
  1. Brainstorm
    It’s hard to use file as start point.
    I wonder if it could be done with “body” to operate in the form of
  • list handwritten [...]
  • path _KB/...

or invert everything with negation ! (like De Morgan’s laws)

I am not sure what you mean with your second point.

Something like []+list(file) to collect all files from the _KB folder and check if they have a link to file. However, I’ve found that an list [] (and elements of that list) doesn’t save to the next step of the Base filtering process.

For bases filter you could simply do:

filters:
  and:
    - file.folder == "_KB"
    - file.hasLink("file")

This return all notes in the folder _KB which link to a specific file.