Using links[] in current tiddler

Did you know the following will collect most links embeded within the tiddler text field and list them?

<$list filter="[all[current]links[]]">

</$list>

Adding this to the view template gives you summary of all links in a tiddler seperate from the text.

  • It will only return the effective title and ignore any prettl links like [[new name|long tiddlername]]

Question

Is there a simple, or mor complex way we can extract these links and also have access to the pretty name of each link?

Why do I ask?

Because we could use a naming standard in a pretty link such as f1 f2 etc… and use this to handle links selectivly based on this pretty link.

  • perhaps we could build an alternate operator that returned the original link eg [[f1|long tiddler name]] instead such as [prettylinks[]]
  • In this case it will return the full text string of the link not a reduced one.
  • Perhaps a clone of the existing link operator modified any JavaScriptors should see $:/core/modules/filters/links.js for a small project.
  • But could this also be valid for backlinks and other operators?

I wont spell it out here but I can imagin this opening a whole lot of interesting tricks if we had such a mechanisium.

1 Like

@TW_Tones i think I could use this. I’ll add to my todo list to review.

I recently created a tiddler in my project to list “referenced images”. Kind of the same idea, I think. My tiddler uses a regular expression to determine img tags within in user selected field. I don’t think a regular expression will work here.

Link to “Referenced Inages”

Ideally, I would want something that returns both the link (the tiddler referenced) and the display text (if found).

Thanks for your responce

Actually if you think about it there is already a regular expression doing exactly this, the core detects [[title]] and [[pretty|title]] already and in the final render replaces it with references to the $link widget, this includes camelCase if configured.

I raise this because it points to an existing limitation with filter operators dealing with links, in so far as they can only return the title and the pretty link if used is lost. If we think about it they currently return the [[title]] or a title formatted list, it seems to me modified or new filter operators could return [[pretty|title]] there by;

  • Retain the pretty name
  • Allow other wikiscript to interrogate the prefix eg [[pretty| and further filter based on that.
  • In a way we simply want to force the operator to return the [[ ]] and the pretty| of such links.

In one way I am simply asking that link related operators include a mode that returns titles in there as found form.

  • Of course we could expand this to include returning the fill text of other link forms as found [img[ etc…
  • As a result it may suggest one or more new operators with this specific objective in mind.

I don’t think you really need a JS operator for this; you can do it with a function. For example:

\function get.links(lb:"[[", rb:"]]") [get[text]split<lb>search<rb>] :map[split<rb>first[]]

{{{ [[HelloThere]get.links[]] }}}

Results on TW-com:

non-linear|Philosophy of Tiddlers
capturing|Creating and editing tiddlers
organising|Structuring TiddlyWiki
sharing|Sharing your tiddlers with others
to-do list|TaskManagementExample
essay or novel|"TiddlyWiki for Scholars" by Alberto Molina
still be able to use|Future Proof

You could then split on | for whatever further processing you wanted to do.

I hard-coded get[text] into the function to parallel links[], which only works with the text field (to my frequent frustration). But you could obviously replace it with a parameter, which would probably be my preference…

\function get.links(field:"text", lb:"[[", rb:"]]") [get<field>split<lb>search<rb>] :map[split<rb>first[]]

{{{ [[HelloThere]get.links[]] }}}
{{{ [[HelloThere]get.links[my-field]] }}}

Or just leave it out of the function altogether and handle that in the filter where you’re using it:

\function get.links(lb:"[[", rb:"]]") [split<lb>search<rb>] :map[split<rb>first[]]

{{{ [{HelloThere}get.links[]] }}}
{{{ [{!!text}get.links[]] }}}
{{{ [<storyTiddler>get[my-field]get.links[]] }}}

I will have to test but what happens if there is no |.

I did discover that technicaly most widgets accept various inputs and output something. Most of the time no further processing is done on a widgets output. This makes a custom widget that uses something similar to your code to list all links with pretty links when available a good idea. We could also add logic to aquire the caption as the prety link if no pretty link is given.

Thanks for the inspiration @etardiff

That depends entirely on your desired output. Since I didn’t know your intended use-case, I left that as an exercise for you. :wink: But for instance, if you wanted to retrieve a list of hard links (pretty and otherwise), you can test this on tw-com:

\function get.links(lb:"[[", rb:"]]") [split<lb>search<rb>] :map[split<rb>first[]]
\function link.target() [{!!title}split[|]butfirst[]else{!!title}]
\function link.display(field) [{!!title}split[|]!match<link.target>] ~[{!!title}get<field>] ~[{!!title}]

<ul>
<$list filter="[{HelloThere}get.links[]] [{Learning}get.links[]]">
<li><$link to=<<link.target>>><$list filter="[link.display[caption]]">{{!!title}}</$list></$link></li>
</$list>
</ul>

Or you could stick the whole thing in a procedure, if you prefer:

\function get.links(lb:"[[", rb:"]]") [split<lb>search<rb>] :map[split<rb>first[]]

\procedure link-list(filter, field, class)
	\function link.target() [{!!title}split[|]butfirst[]else{!!title}]
	\function link.display(field) [{!!title}split[|]!match<link.target>] ~[{!!title}get<field>] ~[{!!title}]
<ul class=<<class>>>
	<$list filter=<<filter>>>
		<li>
			<$link to=<<link.target>>>
				<$list filter="[link.display<field>]">{{!!title}}</$list>
			</$link>
		</li>
	</$list>
</ul>
\end

<<link-list "[{HelloThere}get.links[]] [{Learning}get.links[]]" caption>>

Output:

  • For this example, I changed the caption of HelloThere to caption: {{$:/core/icon}} Hello There to demonstrate the way it works with an alternate field (which may itself contain wikitext, like the icon).
  • I also used <$list filter="[link.display<field>]">{{!!title}}</$list> rather than <$transclude $variable=link.display field=<<field>> /> to ensure that any wikitext present in the field is parsed correctly (not simply displayed as raw text).
  • Revised in my code above but not in the screenshot:
    • Keep \function get.links outside the display procedure so it can be used in other filter contexts.

Of course, I’m not entirely sure why you would want to do this, as pretty-links are typically chosen to make sense in context, and lose that context if collected elsewhere. But it’s certainly possible!

This feels a little over-complicated to me, but as I said, I don’t know your use-case. Do you see a benefit to using a widget over a procedure? How do you envision using it?

To clarify is we now have a simple set of operators like links and backlinks to the recent backtranscludes that do retrieve links to related tiddlers. In such cases the pretty link is not available even when provided, or we need to seperatly retrieve the caption if this is in use.

  • Primarily I would like to see this gap filled
  • You have detailed a solution for the links operator
  • You have proposed returning a compound value using | as a delimiter.

What practical use do I have?

  • First in my view addressing a systemic gap in in TiddlyWiki that we could fill.
  • Second be able to list all links found in a tiddler (links[]) in a summary or footer on a tiddler;
    • but maintaining the pretty link if provided
  • Doing similar for other related link operators like backlinks.

Keep it simple Sweetheart

Using the method suggested by @etardiff Emily and simply wrapping the pretty|title or title in [[ ]] will allow listing of links[] with their original pretty title.

  • Similarly sometime we may want to find caption|title
  • or even pretty|title else caption|title else title

The general pattern here is retriving the display|title then subsequently returning [[display|title]] or [[title]], this is an example of (sometimes) returning two values from filters, not only a single value.

  • If we adopt some functions or widgets that return display|title or title and then a general solution for handling these eg wrap and display with the pretty link whern available.

Widgets tend to be where data goes to be displayed, so if we want a generic solution to accept display with space|title or display|title or title (and lists there of) and return [[display|title]] or [[title]] a widget makes sense.

  • So if we build an ecosystem of tools to take account of pretty links and captions as links to titles we will generate one or more display|title or title items, an display them at the end point with a widget and / or function.

Without complicating the above notes, as pointed out by @etardiff perhaps we need to also consider the form icon|display|title or a variation there of.

The author of the content may use a pretty link or want the caption to be used in various contexts otherwise they may not have used a pretty link or caption.

Final note:

We have also found value in leveraging the missing tiddler mechanisium for virtual titles, and of course these can also optionaly have pretty links which it would be helpfull if they too were available to the designer.

I also not the observation that table rows using WikiText are | delimited including a leading and trailing | so any tools designed to handle the aformentioned | handling, may be designed to assist in extracting a row from text as well.