"Add a link" button selects the first occurence of `url` in the selection

Summary

In the markdown editor, clicking on the Add a link button automatically wraps the text selection with the markdown tag required to render a link, and then selects the first occurence of url in the output. The intent is make it easy to paste in an URL once the link tag has been generated.

For example, with the following text in the text area:

Some text

Selecting the whole text and clicking on the Add a link button properly wraps Some text with the markdown tag, and selects url:

[Some text](url)
             ^
      This is selected

However if the text initially selected contains the term url, that will be selected, instead of the one that was generated. For example, with the following text:

foo_url_bar

The results would be:

[foo_url_bar](url)
      ^
This is selected

This makes the experience confusing, as pasting some text now would result in a broken link pointing to url.

Steps to reproduce

  1. In a markdown editor, type foo_url_bar and select the whole word.
  2. Click on the Add a link button in the toolbar.

Example Project

Any project with discussions

What is the current bug behavior?

Selects the first occurence of url in the selected text.

What is the expected correct behavior?

Select url in the generated markdown tag.

Relevant logs and/or screenshots

url_button

Possible fixes

This is most likely due to how the moveCursor function works. It looks for a pattern passed as the select parameter and selects the first occurence it finds in textArea. A possible solution would be to tweak this function to search for the pattern from the end of textArea rather than the beginning.

Edited by Paul Gascou-Vaillancourt