I have a macro that I am having problems using the encodeuricomponent function.
The macro below:
\define SearchDDG(topic)
[[Search DuckDuckGo: $topic$|https://duckduckgo.com/?q=$topic$]]
\end
would be called by <<SearchDDG "Topic of Interest">> but is not uri encoded.
The TW documentation suggests adding [[Title with Space]] +[encodeuricomponent[]] but this only works on filters.
How can I uri encode with macros?
Although you are using a macro you are defining a filter and you are not using the encodeuricomponent[] operator in a filter anywhere.
- Please share the rest of the code that does this
Correct. I don’t want to use a filter. The entire code is listed above in the macro.
Description/summary: encodeuricomponent only works on filters and will not work on urls that are included in a macro
Steps to reproduce:
- Define a macro using the below code and tag with $:/tags/Macro. Save the tiddler.
\define SearchDDG(topic)
[[Search DuckDuckGo: $topic$|https://duckduckgo.com/?q=$topic$]]
\end
- Call macro with
<<SearchDDG "topic">> and render tiddler.
- Clicking on link will open browser to
https://duckduckgo.com/?q=topic
- Call macro with
<<SearchDDG "topic with space">> and render tiddler.
- Clicking on link will open browser to
https://duckduckgo.com/?q=topic with space rather then the expected https://duckduckgo.com/?q=topic%20with%20space
Try this:
\define SearchDDG(topic)
<$let url={{{ [[https://duckduckgo.com/?q=]] [[$topic$]encodeuricomponent[]] +[join[]] }}}>
<a href=<<url>> target=_blank><$text text="Search DuckDuckGo: $topic$"/></a>
</$let>
\end
Notes:
- The
$let widget uses “filtered transclusion” syntax to construct the desired target URL
- The first filter run defines the initial part of the URL.
- The next filter run takes the
$topic$ param value and applies the encodeuricomponent[] filter operator.
- The last filter run concatenates the results of the previous two filter runs to assemble the full URL.
- The full URL is then output as a standard HTML
<a> link that with target=_blank to open in a separate tab.
- The link text is displayed using a
$text widget to prevent creating any tiddler links (e.g., “DuckDuckGo”)
enjoy,
-e
Thank you! That works just as expected. I didn’t know about the $let widget, “filtered transclusion” or the join command.
Another way to achieve the correct URI encoding is to use the [ext[...]] “external link” syntax, like this:
\define SearchDDG(topic)
[ext[Search DuckDuckGo: $topic$|https://duckduckgo.com/?q=$topic$]]
\end
Using the [ext[...]] syntax didn’t work for me. I would have prefered this since it looks simplier.
I tested the [ext[...]] syntax on https://TiddlyWiki.com, and it works as expected, even when the topic text contains spaces. What browser/platform are you using?
quine on ios 16.4, TiddlyWiki version 5.2.7.
Maybe possibly the iOS or quine.
For now, I’ll stick the the first solution. Thanks!