Reputation: 665
Since I use wagtail headlessly the internal links get messed up due to them using the site-url listed in settings. Instead, I want to be able to override that same URL and point them to my frontend.
This post talks a little bit about it in 2018, but I'm hoping this has changed? Wagtail: Is it possible to disable prepending internal link's with the site domain?
For external links you'd do something like this to override it:
class NewWindowExternalLinkHandler(LinkHandler):
# This specifies to do this override for external links only.
identifier = 'external'
@classmethod
def expand_db_attributes(cls, attrs):
href = attrs["href"]
print(attrs)
# Let's add the target attr, and also rel="noopener" + noreferrer fallback.
# See https://github.com/whatwg/html/issues/4078.
return '<a href="%s" target="_blank" rel="noopener noreferrer">' % escape(href)
Is it possible to do the same for internal links?
E.g now since I use a multi-site setup my link looks something like: https://localhost.my-backend-api-domain.com/page/pagename I want it to look like this: https://my-frontend-domain.com/page/pagename
Upvotes: 1
Views: 470
Reputation: 2941
Also I was struggling to change the behaviour of the internal page links.
Intuitivly one would think to follow the external-link example and seethe identifier to external
.
That's however not the case. After digging further into the source I was able to change the behaviour of the external link like this:
from django.utils.html import escape
from wagtail import hooks
from wagtail.rich_text.pages import PageLinkHandler
from wagtail.models import Page
class TargetNewInternalLinkHandler(PageLinkHandler):
@classmethod
def expand_db_attributes(cls, attrs):
try:
page = cls.get_instance(attrs)
return '<a href="%s" class="uk-link uk-link-text">' % escape(page.localized.specific.url)
except Page.DoesNotExist:
return "<a>"
@hooks.register('register_rich_text_features')
def register_external_link(features):
features.register_link_type(TargetNewInternalLinkHandler)
I know this doesn't exactly answer your question. But does it help you figure out a solution?
Upvotes: 1