Example Macro containing code to append a tag and/or field to the current tiddler, based on its name with whitespace

Hello All,

I’ve been vibe-coding some tiddlywiki stuff using Gemini (I know, sorry) and sometimes it’s convenient to create a uniqueID based upon a
the tiddler name where a macro was ran, but one thing Gemini frequently gets wrong is handling whitespace in those added tags or fields.

Like if I want a macro that just adds a tag that’s the original Tiddlers name + foo

IE
“This Tiddler” > macro adds a tag with “This Tiddler foo”

Gemini won’t handle this correctly and I’ll end up with three separate tags

“This” “Tiddler” “Foo”

I think it’s probably missing some kind of built-in macro or something that needs the wrap the <<thisTiddler>> for this to work correctly.

Can some one give me a quick example of the proper “TiddlyWiki” way to handle this so I can correct it? I tried searching around but I bet I’m not using the right terms to find the solution.

Thanks for the help.

-Xyvir

Most tiddler fields are intended to hold a single value that can include spaces. For these fields, you can just use $action-setfield, like this:

<$action-setfield text="this has spaces in it"/>

or

<$action-setfield somefield="one two three"/>

However, the tags field is different. It is expected to contain a LIST of items, where the items are separated by spaces, so

<$action-setfield tags="one two three"/>

adds three separate tags: “one”, “two”, and “three”. If you want to use tag values that contain literal spaces, they have to be enclosed within doubled square brackets, so

<$action-setfield tags="[[one two]] three"/>

adds two tags: “one two”, and “three”

To make it easier to add/remove individual items from the tags field (or any other field that you are using to hold a list of items), there is a different widget, $action-listops, that knows how to do this for you. To add a tag, you can write:

<$action-listops $tags="[[this has spaces]]"/>`

and to remove a tag, you can write:

<$action-listops $tags="-[[this has spaces]]"/>

Note that the $tags=... param is actually using “filter syntax”, and the leading “-” tells $action-listops to REMOVE the specified value from the tags field. So… for your desired use case (adding a tag that contains spaces), you can write:

<$action-listops $tags="[<currentTiddler>addsuffix[ foo]]"/>

Also note that the $tags= param is a special “shorthand” use of the $action-listops widget. The full $action-listops syntax for adding a value to the tags field is:

<$action-listops $field="tags" $subfilter="[<currentTiddler>addsuffix[ foo]]"/>`

which also can be used to add/remove items from any list field you want:

<$action-listops $field="somefield" $subfilter="[<currentTiddler>addsuffix[ foo]]"/>`

Final note: all of the above examples default to setting fields in the current tiddler. To act on a different tiddler you need to add $tiddler="sometiddler" syntax to the widgets:

<$action-setfield $tiddler="sometiddler" tags="one two three"/>
<$action-setfield $tiddler="sometiddler" tags="[[one two]] three"/>
<$action-listops $tiddler="sometiddler" $tags="[[this has spaces]]"/>`
<$action-listops $tiddler="sometiddler" $tags="-[[this has spaces]]"/>
etc...

enjoy,
-e

2 Likes

THANK YOU this is extremely helpful

Your very thorough response answered my question, but I’m curious does $action-listops always add the item to the end of the space delimited list? Or can you specify a specific insertion point somehow?

There’s a whole bunch of filter operators you can use for manipulating list fields.

see https://tiddlywiki.com/#Extended%20Listops%20Filters for documentation.

-e

1 Like