Skip to main content

Open New Tab in Hugo

·112 words·1 min
Posts hugo
Table of Contents

Hugo Config
#

When a link goes to an external website, I prefer to have them open in a new browser tab.

To accomplish this goal, I need to override the Hugo default behavior for rendering links, by creating a new file called <hugo>/layouts/_default/_markup/render-link.html.

And copy-paste the following code into the render-link.html file.


<a href="{{ .Destination | safeURL }}"{{ with .Title}} title="{{ . }}"{{ end }}{{ if or (strings.HasPrefix .Destination "http") (strings.HasPrefix .Destination "https") }} target="_blank"{{ end }} >{{ .Text | safeHTML }}</a>

With this, any link with http or https, will now open in new tab for external link while within the website, it will open at the same tab. :smile:

Related

Hugo
·508 words·3 mins
Posts config hugo
Install, setup, configure Hugo.
Python Rich Package
·121 words·1 min
Posts cli python
Enrich python cmdline apps with RICH.
Encrypted Shell Script
·329 words·2 mins
Posts bash crypto tools
We can encrypt our BASH shell script to protect some confidential configuration.