Filter to count the total number of backlinks

Hi, I am hoping someone can help with a filter to use in order to count the total number of backlinks? I am interested in a crude measure of the connectivity of my knowledge base. I know how to exclude system tiddlers and so on.

I have been trying but everything I try seems (I think) to count the number of tiddlers that have at least one backlink whereas I want to total number of backlinks for all non-system tiddlers, so if a tiddler has five backlinks it would contribute 5 to the total.

Final use is in two lines of text information example

There are 1021 non-system tiddlers in this TW.
There are 3503 backlinks

This would indicate a connectivity ratio of just over 3

Thanks !

Based on a little preliminary testing on TW-com…

"""
`[[HelloThere]backlinks[]]`
{{{ [[HelloThere]backlinks[]] }}}

`[[HelloThere]backlinks[]count[]]`: {{{ [[HelloThere]backlinks[]count[]] }}}

`[backlinks[]count[]]`: {{{ [backlinks[]count[]] }}}
`[links[]count[]]`: {{{ [links[]count[]] }}}

`[all[tiddlers]count[]]`: {{{ [all[tiddlers]count[]] }}}
`[all[tiddlers]] :filter[backlinks[]] +[count[]]`: {{{ [all[tiddlers]] :filter[backlinks[]] +[count[]] }}}
"""

That last filter is counting only the number of tiddlers that have at least one backlink, and you can see it doesn’t match the results of counting the output of [backlinks[]] (which takes [all[tiddlers]] as its input by default). But the backlinks[] count does seem low… probably because the backlinks operator dominantly appends its results.

Some filter operators like enlist and enlist-input can take the suffix :raw, which prevents the default deduplication. But links and backlinks don’t offer the option, so we’ll have to build some title lists that enlist-input:raw can work with.

\function backlinks.list() [backlinks[]] +[format:titlelist[]join[ ]]

"""
`[[HelloThere]backlinks.list[]] +[enlist-input:raw[]count[]]`: {{{ [[HelloThere]backlinks.list[]] +[enlist-input:raw[]count[]] }}}

`[all[tiddlers]] :map[backlinks.list[]] +[enlist-input:raw[]count[]]`: {{{ [enlist<tiddlers>] :map[backlinks.list[]] +[enlist-input:raw[]count[]]  }}}
"""

Here, I’m using a custom function, backlinks.list(), which will generate a title list of all the backlinks of each of its input titles. When the set of input tiddlers includes more than one title, we also need to use this function in a separate :map filter run in order to make a separate list of backlinks for each input title—otherwise, we run into the dedup problem again. So at this stage, we have a list of lists…

Now we can use enlist-input:raw to return the full list of backlinks without duplicates removed, and finally count them.

And the results:

A few more notes:

  • For brevity, I used the default [all[tiddlers]] in all these examples, but you can of course start from [all[tiddlers]!is[system]] or any other set of tiddlers you’d like to examine.
  • The triple quotes """ are there purely to preserve whitespace in the screenshots; you don’t need them in your actual code!
  • As per the usual restrictions of links and backlinks, this filter will only count hard links in the text field.
  • If you have a large and heavily linked wiki, this may be rather slow! My tests on TW-com were pretty snappy, but in my largest wiki (20k tiddlers and a bit over 43k backlinks, per this filter) there was some brief but noticeable lag.
3 Likes

Just curious: Why are you conceiving this in terms of “backlinks” — if you’re looking for the total connectivity of the wiki, wouldn’t “links” work just as well.

(I think of “backlinks” as useful only when we’re taking a particular tiddler as the home for a view template or info panel that helps us track which other tiddlers point toward it.)

Hi Springer, I guess that’s just the way it evolved. I adopted a plugin to show the backlinks as a list at the tail of every tiddler, I used CSS to make them faint so not too much visual clutter. So when I am following a knowledge base “thread” the backlinks list at the end of each tiddler is often a good starting point.

Over time I added functionality to show the most “linked to” tiddlers first and also the “least linked to” tiddlers so that I could check out neglected items or items that were not very important and might be culled.

So I haven’t really evaluated the other way of working - I have tended to think of the number of incoming links to a tiddler as a measure of its importance as a kind of “topic hub” - a bit of knowledge that a lot of other tiddlers depend on or refer to.

I have close to 3000 knowledge tiddlers, some whole book chapters so its take many different ways of working with custom plugins to somehow be able to dive in quickly, relate ideas and find tiddlers that really should be linked together but are not - perhaps because ideas that relate evolved at different times etc. :grinning:

1 Like

Thanks so much, it seems to be working correctly for me - I am not surprised I was having difficulty. Much appreciated :grinning:

1 Like