Toward Bidirectionality: More Precise Backlinks


Status: Partially implemented for line labels.

1 Objective

Decorate certain links with more precise backlinks, so that you can tell (easily) which other things refer back to this link. This gives us bidirectionality for these links, which is a useful property.

2 Background

There are several use cases of bidirectional links:

  1. line labels (for going from the block body to the line label link in prose)

  2. backlinks (for making each backlink go back to the exact link used in the prose, instead of "Cell 12" or "Table 1")

  3. footnotes (for going from the footnote definition back to the footnote link superscript in prose)

Originally we encountered a problem where sometimes, it's not clear which body of text uses the line label in a code block. This can happen if the code block is very large, or if the explanation that uses the line label sits a bit further away from the code block itself (the explanation could even be in another file). So when readers look at a lonely looking line label, they have to manually search for the line label link in the text.

The same thing is true for backlinks --- most backlinks currently point to paragraph cells, but that's the finest granularity we have. You still have to look inside the paragraph (after clicking the backlink) to find the location of the backlink.

A similar issue exists for footnotes (where we want to see, from the footnote definition at the bottom of the page) exactly where the footnote definition is referenced in the prose (via a superscript). While it's easy to click on a footnote superscript to go to its definition at the bottom of the page, it's hard going the other way (if you start from the footnote definitions at the bottom, it's hard to see where these superscripts are used in the overall page).

3 Problem

Line labels, backlinks, and footnotes are all unidirectional, when in practice they should be bidirectional (so that you can start from either end of the link). The lack of bidirectionality currently makes some links a bit mysterious for the reader. Mystery is the opposite of what we want from Literate Programming!

4 Constraints

4.1 Keep IDs unique

In our woven HTML, we want to avoid duplicating any unique IDs (each link, if it has an ID associated with it, must be unique in the doc).

5 Solution Design

What we want is a kind of generalized backlink. Currently we store backlinks using this data structure in the Archive:

backlinks :: Map GPath (Map GPath [(GCI, GCI)])

Backlinks are a cell-to-cell adjacency list. The keys are object paths, and each GCI pair in the values is simply an edge from the source cell to the target cell.

Map GPath (Map GPath [(GCI, GCI)])
      |          |       |
      |          |       `---> List of edges (source GCI, target GCI)
      |          `---> Source object path
      `----> Target object path

This is constructed in the Archive by looking at document-level pseudo edges, which is

(LCI, LinkOid)

going from a source cell to some named target. The idea is that a LinkOid resolves to a named destination cell, giving us cell-to-cell edges.

Unfortunately this is too coarse because the starting vertex (LCI) is only as accurate as the current cell, and the LinkOid is limited because it is not the only thing used for link targets:

data LinkTarget
  = LinkTargetOid LinkOid
  | LinkTargetLineLabel LinkOid
  | LinkTargetFootnote Text
  | LinkTargetURI N.URI
  | LinkTargetFilePath FilePath

Instead of using cell indices as vertices, we want to use more granular things. For the starting vertex, we can use a link index, which is just an integer which we increment every time we see a link. This gives each link a unique "address" within a single document.

type LLI = Int

LLI stands for local link index. Similar to the mechanism as we already have for cell indices, namely LCI and GCI (which is just LCI with offsets), we can have a LLI and GLI for local and global link indices.

That takes care of the starting vertex. The destination vertex can be a LinkTarget, so our edge could be

(GLI, LinkTarget)

Using these more granular edges, we can now know for any given link target, what the backlinks are for it.

There is some special consideration for footnotes: unlike LinkOid which embeds the OID (UUID) and are unique across files, LinkTargetFootnote Text does not have this. This is because Orgmode does not support linking to footnotes across files. Because of this, whenever we see an edge that points to a footnote, we can use the GLI to know which document's footnote we are talking about (that is, the GLI and the LinkTargetFootnote are always assumed to be from the same document).

Similar to backlinks, we can use a nested map structure like

Map GPath (Map GPath [(GLI, LinkTarget)])
      |          |       |
      |          |       `---> List of edges (source GLI, target LinkTarget)
      |          `---> Source object path
      `----> Target object path

to capture all (granular) backlinks.

All links in the document can be given a LLI index number to form a unique ID for that link, so that we can precisely target these links. This satisfies the constraint at Keep IDs unique.

6 Alternatives considered

7 Future work

7.2 Add performance benchmarks

It would be nice to have performance benchmarks so that we can more easily tell the effect of adding new features (like the one proposed in this design).

7.3 Use local for the current Object being woven

While orthogonal to this design doc, this idea was discovered in the process of writing this design.

Currently when weaving we have lots of areas where we do something like

  | Just originObj <-
     lookup
       weaveConf.originObjectPath
       weaveConf.archive.objects,

in a guard, to extract the currently-being-woven Object. However this can also be cleaned up with the use of local, so that it just becomes

  | Just originObj <- weaveConf.currentObj

Maybe we should also change the originObj naming to be currentObj or curObj because the latter is simpler. Same for originObjectPath.

Page metrics