From 39d685f56681254ccc21fbe9a8923d662f4e52f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Weber?= Date: Fri, 16 Dec 2022 18:41:22 +0100 Subject: [PATCH] theme: open external links in separate tab feature #419 --- exampleSite/content/basics/migration/_index.en.md | 2 ++ layouts/_default/_markup/render-link.html | 11 ++++++----- layouts/partials/shortcodes/link.html | 10 ++++++++++ 3 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 layouts/partials/shortcodes/link.html diff --git a/exampleSite/content/basics/migration/_index.en.md b/exampleSite/content/basics/migration/_index.en.md index 6b74c0a07a..5239b661b3 100644 --- a/exampleSite/content/basics/migration/_index.en.md +++ b/exampleSite/content/basics/migration/_index.en.md @@ -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/). --- diff --git a/layouts/_default/_markup/render-link.html b/layouts/_default/_markup/render-link.html index b6753c853c..01ab9f9306 100644 --- a/layouts/_default/_markup/render-link.html +++ b/layouts/_default/_markup/render-link.html @@ -1,5 +1,6 @@ - - - - {{ .Text | safeHTML }} - \ No newline at end of file +{{- partial "shortcodes/link.html" (dict + "context" .Page + "url" .Destination + "title" .Title + "content" .Text +) }} \ No newline at end of file diff --git a/layouts/partials/shortcodes/link.html b/layouts/partials/shortcodes/link.html new file mode 100644 index 0000000000..faf18543a6 --- /dev/null +++ b/layouts/partials/shortcodes/link.html @@ -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 -}} +{{ $content | safeHTML }} \ No newline at end of file