Dynamic WikiText linking

Is there any way of having wikitext call a procedure to do linking? I am thinking of a user defined way to linking tiddlers via a procedure. For example,

[[abc| xyz]]

Where xyz is a procedure call.

Bobj

[[abc|xyz]] is already used to create a “pretty link”, where abc is the display text, and xyz is the tiddler title.

To use a procedure to output a link, I suggest just using the standard <<xyz "abc">> syntax, where the xyz procedure might be defined something like this:

\procedure xyz(tid)
<$link to=<<tid>>
... do something to compute and output the desired display text ...
</$link>
\end

-e

1 Like

You could also call the procedure from inside a transclusion {{abc||uvw}} in side uvw it will call xyz with the abc parameter or current tiddler. Better yet, {{abx||xyz}} where xyz is a tiddler that does what your procedure would have done to abc. An advantage here is the new transcluding operator.

In the case of [[abc|xyz]] xyz is a tiddler or as you propose a procedure. You could redefine the $link widget to handle calling procedure but how do you decide when its a tiddler and when its a procedure.

But @Bob_Jansen why?, If not @EricShulman way, as this is how you use a procedure?

1 Like

@TW_Tones , the reason i asked this question is because I am looking for a way to have dynamic linking where the target is determined at run time.

I think this would be useful in my gun TW because the gun type, the type of artillery piece a particular gun is, can and appears to have many different labels. For example, a 64 pounder RLF can be referred to as a 64 pounder, 64-pounder, 64 Pounder, 64-Pounder, 64 pndr, etc .

I have discussed with the domain experts about a standard labelling scheme but have been advised there is no agreed standard taxonomy.

So my thinking was to give each gun type tiddler a unique name, eg. MGA124, and then have a field containing all known labels for this type. The dynamic linker would then, when the user clicks a link in the specific gun text, look for a gun type with that source label in the dynamic label field and if a match is found, display that linked tiddler.

Now, the link should look identical to a normal wiki text link, so coloured blue. The user does not need to be aware of the underlying complexity.

This way, we can add new labels to gun types as we process content along the way.

Bobj

I’d recommend one (but not both!) of the two “alias” plugins I’m aware of: @mklauber’s Aliases or @pmario’s uni-link. In either case, you’d add labels to the aliases field, so aliases for “MGA124” might look like

aliases: [[64 pounder]] 64-pounder [[64 Pounder]] 64-Pounder [[64 pndr]]

Aliases are casesensitive in the “Aliases” plugin, but not in uni-link, so in that case you could omit “64-Pounder” and “64 Pounder” or their lowercase equivalents.

I use uni-link myself, but both plugins have their advantages.

  • Aliases overwrites the core link mechanism, which allows you to use the standard wikitext link format — so [[64 pounder]] would link 1) to a tiddler “64 pounder”, if one exists; 2) to a tiddler with “[[64 pounder]]” in its aliases field, if one exists, 3) to a disambiguation page if more than one tiddler shares the same alias. This might be the easiest solution, especially if some of the people using/maintaining your wiki are less familiar with TW. However, as it does overwrite some core widgets, it may not be optimal if you’ve made any other changes to link handling in your wiki.

  • uni-link maintains a wikitext distinction between [[standard links]] and [[alias links|?]], and it’s overall more feature-rich (and easier to customize, in my experience) at the cost of greater complexity. For instance, it also lets you choose to display the caption field, if present, as the link text of any [[standard link]] in your wiki. If you have a lot of short or opaque titles that you’d prefer to display with more semantic link text — e.g., you want a link to [[MGA124]] to render as “64 pounder RLF” by default — I recommend uni-link over Aliases.

2 Likes

If you definitely want to handle these with link syntax, the suggestion from @etardiff is the way to go.

But you could also easily add a custom procedure.

With tiddlers like this:

title: 64-Pounder
tags: [[Gun Type]]
aliases: [[64 Pounder]] [[64 pounder]] [[64-pndr]]

!! Info about 64-Pounder goes here

[img[hilthart-pedersen-neOFO5DPUO8-unsplash_600x400.jpg]]

we could write a procedure which lets us add links to the proper tiddler like this:

\procedure gun-type-link(name, display)
  <$list 
    filter="[<name>tag[Gun Type]] [tag[Gun Type]!is[draft]contains:aliases<name>] +[first[]]" 
    emptyMessage="""
      <span title=`Gun type "$(name)$" not found`>
        <$text text={{{[<display>!match[]else<name>]}}}/>^^<$link to="Gun Type">?</$link>^^
      </span>
    """>
    <$link><$text text={{{ [<display>!match[]else<name>] }}} /></$link>
  </$list>
\end gun-type-link

Then call it like this:

* `<<gun-type-link "64-Pounder" >>`: <<gun-type-link "64-Pounder" >>
* `<<gun-type-link "64 Pounder" "64 Pound Gun">>`: <<gun-type-link "64 Pounder" "64 Pound Gun">>
* `<<gun-type-link "64-pndr" "That's massive!">>`: <<gun-type-link "64-pndr" "That's massive!">>
* `<<gun-type-link "8-Pounder" >>`: <<gun-type-link "8-Pounder" >>
* `<<gun-type-link "8-Pounder" >>`: <<gun-type-link "8-pndr" >>
* `<<gun-type-link "8-pndr" "Ain't that cute?">>`:  <<gun-type-link "8-pndr" "Ain't that cute?">>
<br/>

* `<<gun-type-link "8-Pndr" >>`: <<gun-type-link "8-Pndr" >>
* `<<gun-type-link "63.999-Pounder" >>`: <<gun-type-link "63.999-Pounder" >>

To get output like this:

You can test this by downloading the following and dragging the resulting file onto any wiki:
GunTypeLink.json (162.1 KB)

The problem here is, that you do not have backlink handling. Also tiddler title changes have to be done by the relink-plugin.

Using uni-link aliases, has no need for relinking. If you need a new alias, just create one and leave the existing stuff untouched.

I certainly admit that I never use backlinks, so I don’t consider them much. If these are important to the OP, then one of the alias plugins would clearly be better.

If you haven’t included the current title in its own alias list (which I would suggest doing, and only skipped doing in my mock-up to demonstrate that it is not absolutely necessary), then when you change names, you might consider adding the old title as an alias. If you don’t, then the supplied text will render with a tooltip that says it’s missing, and a small link to the tag tiddler.

Unless I misunderstand what you’re saying, the same is true here. Simply add or remove aliases from the target tiddler and you can link to it with any aliases in the list. Earlier links whose alias was missing will start working.

But none of this has a chance of handling backlinks. If they are necessary, this won’t work at all.


I have yet to play with uni-link, although I watched the documentation video some time ago. It looks very powerful and useful in many circumstances. I have considered it simply for the case-insensitive linking, but I’ve still never actually installed it. It looks to be a very useful tool, and it might well be the best thing for the OP. My offering was more of a “Hey, here’s something simple that might work for you”


I do have one uni-link question, if you have a moment: Clearly I can use this link syntax to display the title, caption, or subtitle fields. And I can use the alias syntax to map “Doc pages”, “doc-pages”, and “docupage” to the tiddler Documentation Pages. But I often use ad hoc link text in TW, like this: ...which can be found in in the [[main content area|Docuementation Pages]] Can this be combined with uni-link aliases? Is there some syntax such as [[main content area|?|doc-pages]] that would do the equivalent? Or would I have to add “main content area” as an alias for Documentation Pages in order to use it?

1 Like

I’d like to thank everyone who has contributed to this discussion and especially @etardiff for his plugin suggestions. After some investigation, I’ll begin with uni-link and see how that goes.

Thanks again.

Bobj

1 Like

Yes. You have to define a new alias text.

I once did think about the possibility to call procedures with eg: [[alias|?<procedure]] … But that would have re-invented the wheel. We do have working procedure calls.

The only difference would have been back-linking. The uni-link syntax actually does insert a macro call to the aka = “also known as” macro. The back link code searches for this macro call. So it would be possible to call any procedure name. … But depending on the procedure code, it may cause performance problems. So I did not implement that.

1 Like