Script: generate backlinks in Bear

This is a script that I run about once a week that adds backlinks to Bear notes. It uses Scriptable and Bear’s really extensive X-callback API and adds backlinks at the end of notes like this:

Post image

The basic workflow is:

  • Find all notes containing [[ (the start of a note link in Bear Markdown syntax)
  • Parse the body of those notes to get the titles of all linked notes
  • Update notes that are linked to with backlinks

The main caveat is that it can take a really long time to run – there’s no way to get the body of all notes at once, each note must be opened individually to fetch it. So for every note that’s checked for backlinks or appended to, that’s 1 roundtrip between Scriptable & Bear. Though if you run this on an iPad with both Scriptable & Bear open, it’s exponentially faster.

I’m also experimenting in the same script with guessing at “related notes”, as inspired by Roam:

Post image

It’s a pretty dumb implementation since I’m not experienced with natural language parsing, but it’s been sort of fun to see the connections that get made.

I just added the backlinking code on GitHub here if anyone’s interested in trying it out. The related notes code is still pretty rough so I left it out for now.

There are further instructions and caveats in the code comment, but the important one is that I’d recommend backing up your Bear notes before running it, just in case :slight_smile:

2 Likes

This is super cool! Thanks for sharing :pray:

1 Like