Return link if exists otherwise external link

96 views
Skip to first unread message

Mat

unread,
Apr 20, 2021, 3:04:52 AM4/20/21
to TiddlyWiki
In the readme of a plugin I want to link to a tiddler that is not part of the plugin. 
If that tiddler exists in the wiki, then the link should be a regular tiddlylink.
If the tiddler is missing then it should be an external url to where that tiddler can be found.

The following works, calling it with
<<linkOrExt "https://mywiki#mytiddler">> 

\define link() {{{ [<__url__>split[#]rest[]decodeuri[]join[ ]] }}}
\define linkOrExt(url)
<$list filter="""$url$ +[split[#]rest[]decodeuri[]join[ ]is[missing]]""" emptyMessage=<<link>>> <a href="""$url$""" rel="noopener noreferrer" target="_blank">{{!!title}}</a>
</$list>
\end

...but I am hoping for something more elegant that doesn't need the extra macro. And it seems silly I have to do the splitting twice. Maybe something with then or else can be used but I can't fully wrap my head around these operators. (I wish the else op could somehow enclose a full expression directly in the filter: "...else<this separate filter>" )

Note: Trying to avoid the extra macro by doing:
emptyMessage={{{ [<__url__>split[#]rest[]decodeuri[]join[ ]] }}}
...returns a textified title, but it needs to be a link.

Ideas anyone?

<:-)

Saq Imtiaz

unread,
Apr 20, 2021, 3:16:24 AM4/20/21
to TiddlyWiki
Quick thought:

 (I wish the else op could somehow enclose a full expression directly in the filter: "...else<this separate filter>" )
 
Use a filter run prefix.

[... my first filter run..] :else[.. my other filter run...]

Mat

unread,
Apr 20, 2021, 4:24:31 AM4/20/21
to TiddlyWiki
Saq Imtiaz wrote:
Use a filter run prefix.

Appreciated. That'll be useful in other cases but it turns out it was not relevant for this issue.

----------------


But formulating the problem that emptyMessage was not linkified did spark some thoughts so the current status is decent enough:

\define linkOrExt(url)
<$vars
   link={{{ $url$ +[split[#]rest[]decodeuri[]join[ ]] }}}
   lbrack="[["  rbrack="]]">
<$list
   filter="""[<link>is[missing]]"""
   emptyMessage={{{ [<link>addprefix<lbrack>addsuffix<rbrack>] }}} >
  <a href="""$url$""" rel="noopener noreferrer" target="_blank">{{!!title}}</a>
</$list>
</$vars>
\end

Decent enough.
Thank you.

<:-)

Saq Imtiaz

unread,
Apr 20, 2021, 4:26:55 AM4/20/21
to TiddlyWiki
You could also try something like this (untested):

\define linkOrExt(url)
<$list filter="""$url$ +[split[#]rest[]decodeuri[]join[ ]!is[missing]]""" emptyMessage="""<a href=<<__url__>> rel="noopener noreferrer" target="_blank">{{!!title}}</a>"""> {{{ [<__url__>split[#]rest[]decodeuri[]join[ ]] }}} </$list>
\end

Mat

unread,
Apr 20, 2021, 4:57:03 AM4/20/21
to TiddlyWiki
@Saq  - thanks, I actually tried that, but the problem is that emptyMessage doesn't know what {{!!title}} is, so to say. (It would be desirable that it did!)

<:-)

Saq Imtiaz

unread,
Apr 20, 2021, 5:41:00 AM4/20/21
to TiddlyWiki
Hi Mat, are you sure about that? 

@Saq  - thanks, I actually tried that, but the problem is that emptyMessage doesn't know what {{!!title}} is, so to say. (It would be desirable that it did!)

 
{{!!title}} in emptyMessage will correspond to the title of the the currentTiddler variable outside the ListWidget. 

Perhaps explain what it is you expect when using {{!!title}} in emptyMessage.

 


Mat

unread,
Apr 20, 2021, 6:09:18 AM4/20/21
to TiddlyWiki
>{{!!title}} in emptyMessage will correspond to the title of the the currentTiddler variable outside the ListWidget. 
>Perhaps explain what it is you expect when using {{!!title}} in emptyMessage.

Thanks for following up! Exactly, {{!!title}} would get the title outside the listwidget. What I want is for e.g the url "https://mywiki#mytiddler" to output [[mytiddler]] if it exists as tiddler, otherwise <a href="...">mytiddler</a> . The macrocall will (typically) appear in plugin readme tiddlers.

Does this make sense or do I misunderstand something? Thank you.

<:-)

Saq Imtiaz

unread,
Apr 20, 2021, 6:45:15 AM4/20/21
to TiddlyWiki
That makes sense and yes it isn't how emptyMessage works, as the variable is only set by the ListWidget when the filter has a result.

 I had assumed from your code that you wanted {{!!title}} in the emptyMessage to mean you wanted the title of the currentTiddler as set outside the list widget.

I think constructing the title in a variable as you have done is the right way to approach this for readable code.

Mohammad Rahmani

unread,
Apr 20, 2021, 12:06:59 PM4/20/21
to tiddl...@googlegroups.com
Mat,
 How the URL shall be given as an encoded uri?


Best wishes
Mohammad


--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/tiddlywiki/e25c546b-4755-4106-a3b3-117f52262fc1n%40googlegroups.com.

Mat

unread,
Apr 20, 2021, 2:22:57 PM4/20/21
to TiddlyWiki
Mohammad wrote:
 How the URL shall be given as an encoded uri?


The macro is called like this


i.e the URL is just copy-pasted from some wiki. 

By the way, you who also create a lot of community stuff, might benefit from this. Its curren main "problem" is discussed here.

<:-)

Mohammad Rahmani

unread,
Apr 20, 2021, 3:55:35 PM4/20/21
to tiddl...@googlegroups.com
Thank you Mat! 
Nice addition!



Best wishes
Mohammad


--
You received this message because you are subscribed to the Google Groups "TiddlyWiki" group.
To unsubscribe from this group and stop receiving emails from it, send an email to tiddlywiki+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages