How to remove space or line breaks from the output of get[text] filter?

Background

I have a tiddler that lists some filters.

[tag[EgTag]]
[tag[Eg2Tag]]

Link to the online demo tiddler

I have a macro that get the filters from the tiddlers. And exports the contet via JSON.

<$button style="min-width:300px"> Export <<__config__>>
<$set name="filter" filter="[<__config__>get[text]]">
<$action-sendmessage
   $message="tm-download-file"
   $param="$:/core/templates/exporters/JsonFile"
   exportFilter=<<filter>>
   filename="$config$.json" />
</$set>
</$button> 

Link to the online demo tiddler

Problem 1

This does not work if each filter is on a newline.

This does not work :x:

[tag[EgTag]]
[tag[Eg2Tag]]

This works :white_check_mark:

[tag[EgTag]][tag[Eg2Tag]]

What changes I need to make in "[<__config__>get[text]]" to make filter’s list with newline work?

Problem 2

Earlier, the "[<__config__>get[text]]" worked fine with the list of tiddlers, with each filter on a newline.

I am not sure what change was made in recent releases that this filter is not working any more.

I explored this problem some more. There is definitely an extra character when gets added when tiddler has newlines.

[tag[EgTag]]
[tag[Eg2Tag]]

If I use length operator on it, I get 26.

[tag[EgTag]][tag[Eg2Tag]]

Length operator on this text returns 25.

Problem is that this additional character of “space” cannot be removed using search-replace operator.

For example, if the tiddler is

Cat dog
Newline

Then, search-replace operator only removes the space in “Cat dog”.

[[New Tiddler 3]get[text]search-replace:gim[ ],[]]

The space added because of “Newline” cannot be removed

I haven’t tested, but you ought to be able to do this in regexp mode: search-replace:gim:regexp[\n],[]

You can also use \s to capture all whitespace, including spaces, tabs, and newlines.

1 Like

Please be aware that the solution provided by @etardiff will remove newlines, or any “blank” character when using \s. I would recommend replacing them with single space characters instead, because tiddler or tag titles can contain spaces and those should remain untouched, whilst space characters between filters are harmless for TW filter syntax.

Something like this (roughly tested): search-replace:gim:regexp[\s],[ ]

This should work for your initial json export problem.

Have fun!

Fred

1 Like

The problem is not with space or line breaks both are handled by TW filtre language.
The problem is with your code! your $set is not correct.

An old solution using text substitution works, change the

<$set name="filter" filter="[<__config__>get[text]]">

with

<$set name="filter" filter={{$config$}} >
1 Like

As text substitution is not recommended use instead:

<$set name="filter" value= {{{ [<__config__>get[text]] }}} >

What was the issue: you have nested filter/nested transclusion and this does not work with $set.

The exportFilter needs a filter expression.

2 Likes

Thank you @Mohammad. Both of the following solutions worked.


Thank you. I noticed that if I change my original code to

<$set name="filter" filter="[<__config__>get[text]search-replace:gim:regexp[\n],[]]">

from

<$set name="filter" filter="[<__config__>get[text]]">

It works too.

1 Like

Hi @talha131

Since exportFilter already takes an filter expressiion, you do not need the <$set> component. Simply:

<$button style="min-width:300px"> Export <<__config__>>
<$action-sendmessage
   $message="tm-download-file"
   $param="$:/core/templates/exporters/JsonFile"
   exportFilter={{$config$}}
   filename="$config$.json" />
</$button>

With that said, I’d like to explain why you had trouble with your macro. The key is to understand that the filter attribute of the $set widget will return a list of elements. When an item of the list contains whitespaces, the item is wrapped in double-brackets. In your case, your filter result has a list of one element—because the config variable contains only one input title. So you could also fix your original expression by adding the select attribute, thereby selecting the first element of the filtered list result:

<$set name="filter" filter="[<__config__>get[text]]" select="0">

Without select, your filter “variable” of ExampleConfiguration2 will have the value of:

[[[tag[EgTag]]
[tag[Eg2Tag]]]]

I think exportFilter will have trouble parsing the above expression; and even if it could, it should look for a tiddler titled [tag[EgTag]](newline)[tag[Eg2Tag]].

Hope this helps.

2 Likes