theme: open external links in separate tab feature #419

This commit is contained in:
Sören Weber 2022-12-16 18:41:22 +01:00
parent 95e07665a7
commit 39d685f566
No known key found for this signature in database
GPG key ID: BEC6D55545451B6D
3 changed files with 18 additions and 5 deletions

View file

@ -16,6 +16,8 @@ This document shows you what's new in the latest release. For a detailed list of
## 5.9.0 (not yet released)
- {{% badge style="note" title=" " %}}Change{{% /badge %}} Absolute links prefixed with `http://` or `https://` are now opened in a separate browser tab.
- {{% badge style="info" icon="plus-circle" title=" " %}}New{{% /badge %}} The theme now supports [Hugo's module system](https://gohugo.io/hugo-modules/).
---

View file

@ -1,5 +1,6 @@
<!-- This code opens an external link in a new tab. -->
<a href="{{ .Destination | safeURL }}"{{ with .Title}} title="{{ . }}"{{ end }}{{ if strings.HasPrefix .Destination "http" }} target="_blank" rel="noopener"{{ end }}>
{{ .Text | safeHTML }}
</a>
{{- partial "shortcodes/link.html" (dict
"context" .Page
"url" .Destination
"title" .Title
"content" .Text
) }}

View file

@ -0,0 +1,10 @@
{{- $context := .context }}
{{- $url := .url }}
{{- $title := .title | default "" }}
{{- $title = trim $title " " }}
{{- $content := .content }}
{{- $target := .target | default "" }}
{{- if and (eq (len $target) 0) (or (strings.HasPrefix $url "http://") (strings.HasPrefix $url "https://") ) }}
{{- $target = "_blank" }}
{{- end -}}
<a href="{{ $url | safeURL }}"{{ if $title }} title="{{ $title }}"{{ end }}{{ if gt (len $target) 0 }} target="{{ $target }}" rel="noopener"{{ end }}>{{ $content | safeHTML }}</a>