Skip to content

Commit

Permalink
fix(core): move external link icon to the end of the link text
Browse files Browse the repository at this point in the history
  • Loading branch information
dropforge committed Jul 29, 2024
1 parent 5df5db4 commit 5f13c7d
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions myhpi/core/templatetags/core_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,18 @@ def insert_footer(page):
def tag_external_links(content):
"""Takes the content of a website and inserts external link icons after every external link."""
external_links = re.finditer(
'<a[^>]*href="(?!#|' + settings.SITE_URL + ")(?!\/)[^>]*>[^<]*", content
# match the entire <a> tag if the href does not start with SITE_URL
# also include all tags inside it (as small times as possible .*?)
'<a[^>]*href="(?!#|' + settings.SITE_URL + ")(?!/)[^>]*>(.*?)</a>",
content,
)
for link in reversed(list(external_links)):
content = (
content[: link.start() + 2]
content[: link.start() + len("<a")]
+ " target='_blank'"
+ content[link.start() + 2 : link.end()]
+ content[link.start() + len("<a") : link.end() - len("</a>")]
+ " {% bs_icon 'box-arrow-up-right' extra_classes='external-link-icon' %}"
+ content[link.end() :]
+ content[link.end() - len("</a>") :]
)
template = Template("{% load bootstrap_icons %}" + content)
return template.render(Context())
Expand Down

0 comments on commit 5f13c7d

Please sign in to comment.