Allow Editor to specify whether Event links should open in in blank tab
Currently, Event website links open in a blank tab. However, the Editor may want to selectively open some links in a new tab, such as external links, while others would open in the same tab (e.g., internal links).
:information_source: see a possible solution in a comment below: https://github.com/WesternFriend/WF-website/issues/368#issuecomment-1365647325
The Wagtail RichText editor doesn't allow users to specify a target when adding URLs but may support that feature at a later point: https://github.com/wagtail/wagtail/issues/8150
https://github.com/wagtail/wagtail/issues/1167
Task
- [ ] determine the priority of supporting the target attribute for Event-related links
- [ ] decide on a possible solution, such as adding a repeating field group for Event website(s) and a target attribute
We might use a bit of JavaScript to scan all links on a page when it renders and set the target link to blank for external links.
// Work-in-progress based on
// https://github.com/wagtail/wagtail/issues/1167#issuecomment-1072345927
$(document).ready(function(){
$('a[href^="http://westernfriend.org"]').attr('target', '_blank');
$('a[href^="http://"]').attr('rel', 'nofollow noopener');
$('a[href^="https://"]').attr('target', '_blank');
$('a[href^="https://"]').attr('rel', 'nofollow noopener');
});
There is a solution to this issue that was suggested in the Wagtail GitHub issue: https://github.com/wagtail/wagtail/issues/1167#issuecomment-479638881
from django.utils.html import escape
from wagtail.core import hooks
def external_link_handler(attrs):
href = attrs["href"]
return '<a href="%s" target="_blank" rel="noopener nofollower">' % escape(href)
@hooks.register('register_rich_text_features')
def register_external_link(features):
features.register_link_type('external', external_link_handler)
I think this flexibility is something I would like to have site-wide, not just for "events."