1

Basically I need that the target="_blank" attribute would be added to all the external URLs inserted in the wagtail Draftail richtext editor:

external url

In [2]: wagtail.__version__
Out[2]: '2.0.1'

EDIT-1: This is not a duplicate. wagtail version 2 uses completely different richtext editor.

The proposed answer suggests $('a[href^="http://"]').attr('target', '_blank') which would add the appropriate attribute to the all links on the page which contain http://. It's very suboptimal solution, as there may be many more links on the page which do not require such a treatment. Obviously there must be more adequate fix, especially because wagtail already differentiate Internal and External links on the editor UI (see attached image).

EDIT-2:

It seems that in the wagtail.core.rich_text.feature_registry.FeatureRegistry class there is a method, which to my best understanding assign different handler to the different URL type, however I don't see what these these handlers are, how they get called or how to modify them?

def register_link_type(self, link_type, handler):
    self.link_types[link_type] = handler
NarūnasK
  • 4,564
  • 8
  • 50
  • 76
  • 3
    Possible duplicate of [Making external links open in a new window in wagtail](https://stackoverflow.com/questions/33300941/making-external-links-open-in-a-new-window-in-wagtail) – solarissmoke Apr 26 '18 at 11:53
  • 1
    This is not a duplicate, as wagtail version 2 uses completely different richtext editor. – NarūnasK Apr 26 '18 at 11:58
  • 1
    Have you actually looked at the answer there? The solution is completely independent of the back-end editor. – solarissmoke Apr 26 '18 at 11:59
  • This is a duplicate. The rich text editors have changed, but the link chooser hasn't, and server-side processing is also mostly the same. – Thibaud Colas Mar 17 '19 at 15:00
  • @ThibaudColas As I explained in the Edit-1, the proposed answer is nothing more but a `JavaScript` **hack**. You can mask dozens of framework problems by throwing at them tons of JavaScript, but it will not solve core problems. You've mentioned that the `server-side processing is also mostly the same` (hence you agree it's different, but kind of similar), so I assume you know a lot about `wagtail` internals? Why not then post a proper answer? This problem has to be solved in the guts of the framework (`wagtail`) and not be masked by the `JavaScript`. – NarūnasK Mar 18 '19 at 20:54
  • @NarūnasK It's one thing for you not to like the currently proposed solution, but it doesn't make your question less of a duplicate. Duplicates make it harder for people to find correct answers. And yes, I'm going to propose a better solution, (https://github.com/wagtail/wagtail/issues/3257#issuecomment-473674142), but I'll be responding on that other question only. – Thibaud Colas Mar 21 '19 at 23:18
  • @ThibaudColas Very well, post it wherever you like, as long as you will provide the correct solution. Just note, that your answer will probably hang around as a comment next to the accepted incorrect answer. – NarūnasK Mar 22 '19 at 09:47

1 Answers1

3

Create file wagtail_hooks.py and add the following lines:

class NewWindowExternalLinkHandler(LinkHandler):
    identifier = 'external'

    @classmethod
    def expand_db_attributes(cls, attrs):
        href = attrs["href"]
        return '<a href="%s" target="_blank" rel="noopener noreferrer">' % escape(href)

@hooks.register('register_rich_text_features')
def register_rich_text_handlers(features):
    features.register_link_type(NewWindowExternalLinkHandler)