Home / GitHub Page

Backlinks

To use Joplin as a knowledge base it would be useful to have a “what links here”?

What do you think?

1 Like

I think this might be quite some effort and additionally there will be ongoing maintenance required. To do this you basically have to create a separate table and maintain a source - target list. Thus every time you touch a link, maintenance has to be done on that table.

I was thinking that this could be implemented like a plugin, so every time you want backlink info a script would run over all of your notes and find backlinks. This could be slow for a lot of notes, but will otherwise work. I don’t see this being a core feature in Joplin however.

1 Like

Using Python and Joplin API, I was able to add backlinks automatically. Following the script I wrote and used:

import requests, re
token="REPLACE YOUR TOKEN HERE"
resp = requests.get("http://127.0.0.1:41184/notes?token="+token+"&fields=id,title,body")
# Loop on all notes
for item in resp.json():
    # Find link to note '[anything](:/anything)' in note body, excludes links start with '!' or 'backlink:'
    links = re.findall("(?<=[^\!backlink:])\[.*\]\(:\/.*\)", item['body'])
    # If list 'links' is not empty
    if links:
        # Loop on links
        for link in links:
            # Get target node id
            target_node_id = re.search(r"\(:\/(.*)\)",link)
            # Get target node body
            output_dict = [x for x in resp.json() if x['id'] == target_node_id.group(1)]
            # Add backlink at the end of the target node body
            new_body={"body": output_dict[0]['body']+'\n* * *\n'+'backlink:[{}](:/{})'.format(item['title'], item['id'])}
            # Update target node
            update_resp = requests.put('http://127.0.0.1:41184/notes/'+target_node_id.group(1)+"?token="+token,json=new_body)

P.S.

  1. Keep the work backlink: before the backlink, that’s to avoid backlinks endless cycle. You can change it though in the code before first run.
  2. Take backup before running this script. I only run it on my Joplin notes and it works without errors but still need more tests. Your feedback will helpful to fix any error if any.

2 problems still:

  1. If the title of original note changed, the backlinks’ title will not be updated unless removed and rerun the script! May be another script should be written to update backlinks only.
  2. Running it again will duplicate backlinks, that need extra coding to check if backlink is already exist.

I just thought I will post this initial code, may someone would like to pick it up and resolve above 2 issues and improve it.

2 Likes

Are you saying links (and hence backlinks) are not primary citizens in Joplin? So I should organize my notes in a hierarchy, rather than link relevant pieces?

Okay, new idea here.

To implement “What links here” you just need one thing: the id of the note.

Proposed workflow:

  • right-click on the node
  • choose What links here (new menupoint) (could also be a “Show incoming links” menupoint in Edit below Search in all notes)
  • and you find yourself as if you have typed the note id after choosing “Search in all notes”