Hyperlinks of the Rich Text Editor control

I’m going to create a dictionary project using Vaadin’s Rich Text Editor control. One of the most important points should be hyperlinks from one dictionary article to another. I understand how hyperlinks can lead to external pages, but can they lead to internal resources? Is there any type of HyperlinkClickHandler that would allow me to react in a custom way to a hyperlink click?

Hello and welcome to the community!
I’ve tried it myself, and I’m afraid that there is no “Click Handler” on the Rich Text Editor that you could use.

As you said, you can insert hyperlinks to the external pages like so:

richTextEditor.setValue("<p>This is the link to external page: <a href="https://www.wikipeda.org">Go to Wikipedia</a></p>");

For implementing the dictionary feature going from one article to another on the same server, the best you could do is to use relative URLs:

richTextEditor.setValue("<p>This is the link to internal resource: <a href="/article2">Go to Article 2</a></p>");

However, there is currently a very strict sanitizer, to keep the HTML code of the editor content safe. This sanitizer removes local links. There is already a feature request opened for this issue: RichTextEditor removes relative urls · Issue #6834 · vaadin/flow-components · GitHub

So, until the feature is implemented: if you want to reference an internal resource in the rich text editor, you have to use the full URL to the resource, including the protocol and domain:

richTextEditor.setValue("<p>This is the link to internal resource: <a href="https://mydictionary.com/article2">Go to Article 2</a></p>");

Note that you can get protocol and domain from the current URL of the page:

UI.getCurrent().getPage().fetchCurrentURL(url ->
                    System.out.println("Current URL: " + url));

Thanks, Břetislav, I already thought that no one would be interested in this matter and that I would be left alone with this problem :-)
It seems that routing and fully specified URLs are the way to go. At least for now. It’s sad that the RichTextEditor component doesn’t simply copy the hyperlink functionality from the Spreadsheet component:

Hyperlinks
Hyperlinks in cells can point to other worksheets in the current workbook — or to external URLs. Links must be added through the POI API.
Vaadin Spreadsheet provides default handling for hyperlink clicks. These can be overridden with a custom HyperlinkCellClickHandler, which you assign with setHyperlinkCellClickHandler().