How do I use a URL in a macro?

34 views
Skip to first unread message

thor...@gmail.com

unread,
Sep 20, 2021, 3:35:18 AM9/20/21
to TiddlyWiki
Hello all,

I'm having trouble understanding why a simple macro I wrote doesn't work properly:

```
\define todoLink(url, name:"Link")
<a href="$url$" class="tc-tiddlylink-external" target="_blank" rel="noopener noreferrer">$name$</a>
\end
```

If I use `<<todoLink http://foo.org bar>>` to call my shiny new macro, the first parameter seems to be completely ignored:

`<a href="bar" class="tc-tiddlylink-external" target="_blank" rel="noopener noreferrer">Link</a>`

On the other hand, if I use the `<$macrocall>` syntax, like `<$macrocall $name="todoLink" url="http://foo.org" name="bar"/>`, I get the expected behavior.

<a href="http://foo.org" class="tc-tiddlylink-external" target="_blank" rel="noopener noreferrer">bar</a>

Ultimately, I'll be using this macro in tiddlers with a `url` field, so I _think I'll have to use the `<$macrocall>` syntax. But I feel like this behavior will bite me in the future. Why does first method of calling the macro produce incorrect output, and why does the second work?

I can make the first method work by putting the URL in quotes, but I would like to avoid that in general because it's more typing :): `<<todoLink "http://foo.org" bar>>`. Why are the quotes required? I thought macro expansion substituted text without treating the macro inputs as WikiText?

My browser is Opera 78.0.4093.184 on Windows 10, and my TiddlyWiki version is 5.1.21. Thanks for any help!

Eric Shulman

unread,
Sep 20, 2021, 8:35:52 AM9/20/21
to TiddlyWiki
On Monday, September 20, 2021 at 12:35:18 AM UTC-7 thor...@gmail.com wrote:
I can make the first method work by putting the URL in quotes, but I would like to avoid that in general because it's more typing :): `<<todoLink "http://foo.org" bar>>`. Why are the quotes required? I thought macro expansion substituted text without treating the macro inputs as WikiText?

The `<<macroname param param param>>` syntax, in addition to handling *unnamed* parameters, also recognizes *named* parameters, using `name:value`.
Thus, a value like `http://foo.org` is parsed as a parameter named "http", with a value of "//foo.org".  By enclosing it in quotes, it avoids the named parameter parsing and passes it into the macro as a single parameter value

So... you can use the quotes to bypass the parameter name parsing: `<<todoLink "http://foo.org" bar>>`
or, you can add a parameter *name*, like this:  `<<todoLink url:http://foo.org bar>>`.

Either way, it's going to be "more typing" (but only a little).  Still, its less typing than the full `<$macrocall ...>` widget syntax.

-e

thor...@gmail.com

unread,
Sep 20, 2021, 10:42:13 AM9/20/21
to TiddlyWiki
Thank you, your explanation it makes it very clear what's going on. Whoops :D!

> Either way, it's going to be "more typing" (but only a little).  Still, its less typing than the full `<$macrocall ...>` widget syntax.

In this case, I will be using the `<$macrocall ...>` syntax because I want to create a template tiddler using `tm-new-tiddler`. This template contains wikitext which field-transcludes a URL field. However, if I want to create a generic macro for links where I might want to specify the URL scheme, I know to watch out for my colons this time :D!


Reply all
Reply to author
Forward
0 new messages