Generating Backlinks

dev meta kitaab python

published 2023-06-04 21:25

updated 2023-06-16 20:37

I'm really out-growing vimwiki at this stage. But I'm not yet ready to commit to writing a vim plugin that does quite what I would like. In fact, my current setup with telescope, vimwiki, poonam, basant is still quite comfy. But poonam is growing features. Now it can do various cleaning facilities, as well as build indexes. Now I need back-links.

The reason I need this is because vim-zettel's back links only generates for the open buffer, and I want a constantly up to date reference that I can trigger on command for all notes. Especially since I haven't been keeping any of the back links generated.

= How? = I'm not sure if this is a inverted index or not, but it seems close enough.

Iterate through all the files with links, put all their matches into a big dict. This creates your inverted index. Be sure not to include index files when iterating through all files

Then iterate through each key in the inverted index, find where it says "### Backlinks " and overwrite that section of the file. Be sure not to include index files

{{{python link_regex = r'[\\. ]+\]\]' inverted_index = {} for file in files: with open(file, 'r') as fobj: content = fobj.readlines() for line in lines: match = re.match(link_regex, line) if match: v ='[[').lstrip('') v = v.split('|')[0] # We always want the first half, even if | exists if inverted_index[v]: invented_index[v] = set(file) else: invented_index[v].add(file) return inverted_index }}}

I'm scared of implementing this because it'll lead to constant changes in files. Deleting them, adding to them, generally mucking about with them through scripts. This means I need to test these functions rigorously.

I'm also beginning to wonder if it makes more sense to put these all in a database. Then again I'm not sure how I'd be able to access these files through vim and would lose the versioning that git provides. Yeah, I'm not doing that.