Creating a TiddlyWiki Macro to Copy Plain Text from Formatted Content

I am still new to TiddlyWiki macros.

Browser : Chrome
OS: Windows 11
TiddlyWiki version: 5.3.5

I tried to make macro that can copy the text with WikiText formatting or any HTML code into clipboard.
But the copied output should be plain text.

<!------------------ MACROS DEFINITION ----------------->
\define text-to-copy(sentence)

<!-- I want to `$sentence$` variable to be plain text when copy even the text has style formatting-->
<<copy-to-clipboard """$sentence$""" style="position;relative; float:right;">>

<div style="background-color: #f5f5f5; border: 1px solid #8D8D8D;">
$sentence$
</div>
\end
<!------------------------------------------------------------>
<!------------------ EXAMPLE ----------------->
<<text-to-copy """This is @@color:red;red@@ and ''bold''.""">>
<!------------------------------------------------------------>

The appearance is like below image.

My desired output when copied to clipboard is only plain text This is red and bold NOT includes the WikiText like This is @@color:red;red@@ and ''bold''.

Can anyone help me on this or any reference that I can try? Thanks!

To remove all formatting, you can use the $wikify widget, like this:

<$wikify name="clip" text="""$sentence$""">
<$macrocall $name="copy-to-clipboard" src=<<clip>> style="position;relative; float:right;"/>

Note: because we now need to pass the clip variable to the copy-to-clipboard macro, we have to use the <$macrocall $name="copy-to-clipboard" .../> widget syntax with named parameters instead of the macro shortcut syntax (e.g. <<copy-to-clipboard ...>>)

Also note that \define and <$macrocall $name=.../> syntax have been “deprecated” (but can still be used if you want). The newer equivalent syntax is to use \procedure and <$transclude $variable=.../>, like this:

\procedure text-to-copy(sentence)
<$wikify name="clip" text=<<sentence>>>
<$transclude $variable="copy-to-clipboard" src=<<clip>> style="position;relative; float:right;"/>
<div style="background-color: #f5f5f5; border: 1px solid #8D8D8D;">
<<sentence>>
</div>
\end

Note that \procedure automatically creates internal variables for each parameter name, so we use <<sentence>> instead of """$sentence$""".

enjoy,
-e

1 Like

Thanks, @EricShulman
Cool! Seems like what I wanted.

<!-- Procedure definition-->
\procedure text-to-copy(sentence)
<$wikify name="clip" output="text" text="""<<sentence>>""">
<$transclude $variable="copy-to-clipboard" src=<<clip>> style="position;relative; float:right;"/>
<div style="background-color: #f5f5f5; border: 1px solid #8D8D8D;">
<<sentence>>
</div>
\end

<!-- Procedure call-->
<<text-to-copy """
First Line : Hello world. ''Bold'' and @@color:red;red@@ <br>*<br>
Second line : Hello world. ''Bold'' and @@color:red;red@@ <br>*<br>*<br>
Third line:  Hello world. ''Bold'' and @@color:red;red@@
"""
>>

However, it did not let me to copy text with multi-line because when I copied, even it has 2 or 3 break line, the copied content assumes it just one break line.

Output like below:
image

Any other TiddlyWiki widget should I use or check?

Somehow I found workaround for my problem! I used <pre> tag in HTML to preserve the line breaks when copying the multi-line formatted text. I put the tag when entering the text, not inside procedure.

<!-- Procedure definition-->
\procedure text-to-copy(sentence)
<$wikify name="clip" output="text" text="""<<sentence>>""">
<$transclude $variable="copy-to-clipboard" src=<<clip>> style="position;relative; float:right;"/>
<div style="background-color: #f5f5f5; border: 1px solid #8D8D8D;">
<<sentence>>
</div>
\end

<!-- Procedure call-->
<<text-to-copy """<pre>First Line : Hello world. ''Bold'' and @@color:red;red@@ 
*
Second line : Hello world. //Italic// and @@color:red;red@@ 
*
*
Third line:  @@Hello world@@. __Underline__ and @@color:red;red@@
*
*
*
Last Line</pre>"""
>>

Here’s the output after I copied and pasted in other text file.
image

If anyone got better solutions, feel free to share :slight_smile:

I suppose there might be a use-case where you don’t want to preserve newlines, but it seems to me that you could put the <pre>...</pre> tags inside the procedure, like this:

<$wikify name="clip" output="text" text="""<pre><<sentence>></pre>""">

-e

1 Like