Simple text replacement

Hi,

I’m trying to find a way to do something I thought would be relatively simple but is turning out to rather scramble my brain. I’m just looking to produce a bunch of links to some of the tag tiddlers of the current tiddler. Easy enough, but then I want to shorten the display of those links to remove the prefix. What I have so far is:

<$list filter="[all[current]tags[]prefix[Session Logs]]">
<$link to={{!!title}}>{{!!title}}</$link>
</$list>

I ideally want the link to display without "Session Logs: " at the start. Is there a simple way to do a replace on a bit of text, or to remove the start of it?

Certainly. Your current code uses {{!!title}} for the link text, but you can replace this with any other content you’d like to display — including the output of another widget. Here, I’d recommend using the $text widget to display the result of a filtered transclusion as plain text:

<$list filter="[all[current]tags[]prefix[Session Logs]]">
<$link><$text text={{{ [{!!title}trim:prefix[Session Logs: ]] }}} /></$link>
</$list>

I used trim here as it’s simple and semantically clear, but for other forms of text replacement, you may also want to take a look at search-replace.

Incidentally, the $link widget defaults to using to={{!!title}} as its default target and {{!!title}} as its default display text, so if you do want a “normal” link that simply displays the name of its destination, you can simplify <$link to={{!!title}}>{{!!title}}</$link> to <$link/>.

3 Likes

Thank you - that was perfect for what I need :slight_smile:

And thank you for the information about the default display text as well, that will save me a lot of typing in the future :slight_smile:

1 Like

I tried to use search-replace more than once in the same filter, sometimes with regexp. And I couldn’t do it.

I am not sure if this is something wrong with my syntax.

But I found a way to accommodate this is to use [split[too many words]] and [join[two words]] multiple times in the same filter. This allowed me to brake up a line, and the insert new text in that place like multiple search-replace uses.

Is that good, or is there a better way to do that?

Well, in some ways, any solution that accomplishes your desired result is a good solution — particularly if it’s one that you fully understand. The split + join combo may not be the most efficient method, but it’s clever and creative, and it sounds like it worked for you. Good job!

I do think it’s likely that you were encountering some syntactical error in your search-replace attempts, though; it’s definitely possible to use it more than once in a filter run, with or without the :regexp flag. If you can recall what you tried that didn’t work, I’d be happy to try to troubleshoot it for you.

Welcome @Gaxx to talk.tiddlywiki

Of course there are other ways to do this including;

  • Using the caption field on the tiddler instead (you need to have set it)
  • Using a field session-log=yes and not including the prefix “Session Logs” on the titles in the first place
  • Including @etardiff’s answer above

Although since you want to remove the same prefix you use to list the tiddlers it is simple to do this;

<$link to={{!!title}}>{{{ [all[current]removeprefix[Session Logs ]] }}}</$link>
  • I included a space after logs (just guessing)

Thank you.

Just wanted to confirm either way. Don’t have it anymore.