Need help to wrap a selected text to an url

Hello,
I’d like to generate out of a marked text a link to a website:

Text eg: Test 1,2.3

final link: <a class="vers" href="https://www.MyWebsite/inhalt/Test1%2C2.3">Test 1,2.3</a>

Before I have to remove the “blanc” from Test 1,2.3 - and here I’m failing with:

<$action-sendmessage
	$message="tm-edit-text-operation"
	$param="replace-selection"
	[[<currentTiddler>]search-replace[ ],[]]
/>

Than I have to build the final link - but how?
The idea is to have an “Edit Toolbar”-Button to trigger it.

Thanks for help,
Stefan

1 Like

In haste: have you checked out

https://tiddlywiki.com/#Substituted%20Attribute%20Values

and related stuff?

The examples include getting a link that concatenates base url and suffix…

Of course you can use the [*] button in the editor to transform the URL into the recomended one automaticaly. eg [[https://www.MyWebsite/inhalt/Test1%2C2.3]] then edit to [[name|https://www.MyWebsite/inhalt/Test1%2C2.3]]

Then on tiddlywiki.com the internals plugin allows you to see the resulting html as follows;

<p><a class="tc-tiddlylink-external" href="https://www.MyWebsite/inhalt/Test1%2C2.3" rel="noopener noreferrer" target="_blank">name</a></p>
  • target="_blank" is helpful to stop moving away from the wiki.

Thus if you must modify the css perhaps you can change tc-tiddlylink-external.

  • Or look at how these buttons are implemented.

Another trick I like to use is to type “name sentence” into the editor, select it and hit the editors link button and paste the url, then enter and it inserts the above form [[name sentence|https://www.MyWebsite/inhalt/Test1%2C2.3]]

Thanks @Springer, @TW_Tones for feedback.

To use the [*] button in the editor would be a good solution, but it will not take care of the last part in the url “Test1%2C2.3”

The prefix is always: https://www.MyWebsite/inhalt/
the rest of the code is variable (eg.Test 1,2.3 or Test 25,23…),
and I don’t want to edit it each time – just select and click an “Edit Toolbar”-Button → finished.

For my understanding these steps are necessary:

  1. Change variable part:
  • search/replace blanc isn’t needed to remove - correct?
  • replace “,” with “%2C” → maybe with [^,\%2C\d]
    Result: Test 1,2.3 → Test 1%2C2.3 or Test1%2C2.3
  1. build the complete url: href=https://www.MyWebsite/inhalt/Test1%2C2.3

  2. build the final one:
    <a class="vers" href="https://www.MyWebsite/inhalt/Test1%2C2.3">Test 1,2.3</a>
    or
    <p><a class="tc-tiddlylink-external" href="https://www.MyWebsite/inhalt/Test1%2C2.3" rel="noopener noreferrer" target="_blank">name</a></p>

Thanks, Stefan

I wanted to see what I could come up with for this. I’m not much of a user of editor buttons, generally writing wikitext directly without them. But it was something to learn. And I made reasonable progress; it may be enough for you.

But it is not plain wikitext. It uses a JS module. There are many here who would find that a real problem. I’m hoping one of them could answer how to do this without descending to JS.

You can test it by downloading the following, dragging it onto a wiki, and reloading – JS modules require a reload before working: CustomEditorButtonAndAction.json (2.6 KB)

Building a button and action

This involves three tiddlers:

  • One is simply a link icon found on https://morosanuae.github.io/tw-icons/.

  • Then there is a button created following the How to create dynamic editor toolbar buttons tutorial.

    title: $:/_/my/core/ui/EditorToolbar/inhalt-link
    tags: $:/tags/EditorToolbar
    caption: Inhalt Link
    condition: [<targetTiddler>!has[type]] [<targetTiddler>get[type]prefix[text/vnd.tiddlywiki]]
    description: Add Inhalt Link
    icon: $:/images/google-material-design/content/sharp/24px/link
    
    <$action-sendmessage
      $message="tm-edit-text-operation"
      $param="inhalt-link"
    />
    

    The condition here is probably naive. It’s what was in the tutorial. But this should probably also work for Markdown tiddlers, and perhaps others. That’s left as an exercise for the reader.

  • That custom-link text operation does not exist, though. We have to build it. Looking through the options in WidgetMessage: tm-edit-text-operation, none of them quite fit. But wrap-selection and make-link are close, so I cloned the simpler one, make-link, and modified it to this:

    title: $:/_/my/core/modules/editor/operations/text/inhalt-link.js
    type: application/javascript
    module-type: texteditoroperation    
    
    /*\
    title: $:/core/modules/editor/operations/text/inhalt-link.js
    type: application/javascript
    module-type: texteditoroperation    
    
    Text editor operation to make a specific custom link    
    
    \*/
    (function(){    
    
    /*jslint node: true, browser: true */
    /*global $tw: false */
    "use strict";    
    
    exports["custom-link"] = function(event,operation) {    
    
    	operation.replacement = '<a class="vers" href="https://www.MyWebsite/inhalt/' + encodeURIComponent(operation.selection) + '">' +  operation.selection+ '</a>';
    	operation.cutStart = operation.selStart;
    	operation.cutEnd = operation.selEnd;    
    
    	operation.newSelStart = operation.selStart + operation.replacement.length;
    	operation.newSelEnd = operation.newSelStart;
    };    
    
    })();    
    

    The only non-boilerplate line here is this:

    	operation.replacement = '<a class="vers" href="https://www.MyWebsite/inhalt/' + 
    		encodeURIComponent(operation.selection) + '">' +  operation.selection+ '</a>';
    

    which should be fairly clear, once it’s understood that encodeURIComponent is a built-in JS function to turn, say, Test 1,2.3 into the URL-safe Test%201%2C2.3

When these are imported and the wiki reloaded, you can test it simply by editing a tiddler, selecting the text you want to wrap and clicking on the new button. (It will be at the end of the row of buttons.)

Adding Keyboard shortcut

I followed the instructions for creating keyboard shortcuts, and that added three more simple tiddlers:

title: $:/config/ShortcutInfo/inhalt-link

Insert Inhalt link
title: $:/config/shortcuts-mac/inhalt-link

meta-shift-Z
title: $:/config/shortcuts-not-mac/inhalt-link

ctrl-shift-Z

And then the appropriate keystroke for your system will run the same action, wrapping your text as the styled link.

Not done

  • The button examples use an extra level of indirection for their text. This is used for overrides, and—I believe—for internationalization. I didn’t bother with that here. If this is for something to be distributed, it would make sense to do so.

  • I didn’t do anything about placing this button in the appropriate position in the toolbar. I imagine the mechanism is relatively simple, but there look to be groups of buttons, so it may not be as trivial as placing this in the right spot in a list or using list-before or list-after. It’s bedtime, and I’m too tired to investigate.

  • The biggest issue is that this requires a JS tiddler. It seems like there should be a simple way to do what you want without one. Perhaps one of the more experienced community members can offer something.