Skip to content

Instantly share code, notes, and snippets.

@jsloat
Last active December 27, 2022 23:27
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jsloat/142aef35fd8b6fd8e6d1fbb850653558 to your computer and use it in GitHub Desktop.
Save jsloat/142aef35fd8b6fd8e6d1fbb850653558 to your computer and use it in GitHub Desktop.
Generate backlinks for Bear notes in Scriptable on iOS
@davidcoyer
Copy link

This is really great. For some reason, the backlinks that it creates for me are all being treated as a task or todo. I see from your screenshot that it should be a bullet. Do you know why mine would be tasks?
CleanShot 2020-06-10 at 15 32 31

@jsloat
Copy link
Author

jsloat commented Jun 25, 2020

@davidcoyer

This is really great. For some reason, the backlinks that it creates for me are all being treated as a task or todo. I see from your screenshot that it should be a bullet. Do you know why mine would be tasks?
CleanShot 2020-06-10 at 15 32 31

Sorry, I thought I replied to this 🤔 But that is very strange, you can see on line 144 above that it should just print out a dash + space (bullet), then the backlink. You haven't modified the script at all?

@davidcoyer
Copy link

davidcoyer commented Jun 25, 2020 via email

@jsloat
Copy link
Author

jsloat commented Jun 26, 2020

Thanks for replying. No, I have not changed the script at all. I'll keep working on it and let you know if I find anything. Thanks.

Yeah sorry, I'm very curious what could lead to that result, so let us know if you figure it out :/

@davidcoyer
Copy link

davidcoyer commented Jun 26, 2020 via email

@ajgxyz
Copy link

ajgxyz commented Jun 30, 2020

I've been playing with this script and really like it. Really fills in a missing piece right in Bear right now and helps build out a more cohesive knowledge base. It runs really nicely on the iPad in side by side view, doesn't take long to process.

Just wanted to let you know really appreciate you creating this and publishing it.

A couple of quick questions:

  1. In your original post you said you had potentially related links that you were playing with, did that make it in or not?
  2. In a similar vein I was thinking it'd be useful to create an callback URL to quickly search the title of the note, that way you've mostly recreated the idea of 'Linked Notes' and then using a search query to show 'Unlinked Notes'

@jsloat
Copy link
Author

jsloat commented Jul 2, 2020

@davidcoyer awesome! Thanks for sharing the solution.

@ajgxyz good to hear :)

In your original post you said you had potentially related links that you were playing with, did that make it in or not?

Yes I am using it regularly, it works decently well but as I said the logic to determine "related" is really simple and probably stupid haha :) But I'll hopefully take some time to learn more about relevancy scoring in the future.

I will definitely share it -- the only blocker is that on my own devices, the code is distributed across probably 8+ files (different utils files, app API scripts), so it took me about an hour to condense that all into a single file as seen above. It's still on my backlog to do the same for the related notes file, just haven't found the time yet.

In a similar vein I was thinking it'd be useful to create an callback URL to quickly search the title of the note, that way you've mostly recreated the idea of 'Linked Notes' and then using a search query to show 'Unlinked Notes'

I'm not sure I understand, what is the end goal of what you're describing? To have a note that shows all notes that are linked from other notes? Or are you suggesting a different way to find note links?

@johnchandler
Copy link

@jsloat I see it's been a few months so I'm curious if this is still working? Trying it with Bear on iPadOS 14.2 in split view. It loads the most recent not, but then nothing happens and the script just keeps spinning in Scriptable. I can navigate through Bear with no lag...and don't see any changes.

@jsloat
Copy link
Author

jsloat commented Nov 11, 2020

@johnchandler 👋 Testing this out now, I also have this issue. I'll report back. Right now it seems to be something to do with having both apps open in split view, but I'll test a few permutations. Not sure if this is due to Scriptable or iOS changes.

Edit: yeah it seems to be only an issue with having both apps open in split view. It works fine for me when switching back and forth between the apps in full screen. Really disappointing since split view sped it up so much. I don’t think there’s anything I can do about this and as I said not sure if it’s caused by an app or OS change

@xlZeroAccesslx
Copy link

@jsloat This is awesome! Does it work with Wiki links with note headers?

E.g. [[Page Title/Header Title]]

https://blog.bear.app/2020/09/bear-1-7-15-is-out-with-new-features-for-wiki-links-html-export-and-more/

@xlZeroAccesslx
Copy link

If you change the regex from /\[\[(.+?)\]\]/g to /\[\[([\w\d\s]+)(?:\/.+|)\]\]/g on line 75 it will create backlinks for wiki links with headers references...assuming the intended behavior is to create backlinks for individual notes by ignoring the header reference.

@jsloat
Copy link
Author

jsloat commented Nov 23, 2020

@xlZeroAccesslx nice catch 👍 I have actually updated my own code to support this but didn't update here. I think your solution would probably work but I will just update the whole file with my latest version, will be up within a half hour

@jsloat
Copy link
Author

jsloat commented Nov 23, 2020

Updated version posted -- I refactored this for my devices a couple months ago so the code will look different, but the basic functionality is the same and it's backwards compatible if you've already been using it. But per usual, probably a good idea to backup your Bear database before trying :)

Only thing that was removed was the "blacklist" notes (array of note titles to exclude as link sources). I wasn't using this so I removed, but if anyone misses it I can add it back in.

The new version supports wiki links/note headers as @xlZeroAccesslx mentions above, plus some smaller tweaks that aren't really noticeable. I think this version uses less device memory so if anyone was experiencing slowness/crashes before, this may fix that.

@Torgithub
Copy link

Torgithub commented Dec 15, 2020

Great work man!

I would love if I could see some context in the backlinks as well, pulling in the paragraph were the link is mentioned.

Here is my backlinks from a page like the script is working now:

epigenetics

::METADATA::

Backlinks

  • [[S- 2020 - Who We Are - YouTube - Documentary]]

Here is how I ideally want it to look to get more context:

epigenetics

::METADATA::

Backlinks

  • [[S- 2020 - Who We Are - YouTube - Documentary]]
    • [[epigenetics]] - Control above the genes.

@jsloat
Copy link
Author

jsloat commented Jan 4, 2021

@Torgithub thanks! This is a nice idea, definitely possible. I may give this a go at some point, but probably not in the near-term.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment