How to Open Link in New Tab in Hugo

Hongtao Hao / 2020-06-20


The default of Hugo is that when you open a link in a page rendered by markdown, you’ll stay in the current tab. How can we enable openning a new tab then?

The answer was provided by PrasdPen .

Go to layouts/_default, create a new folder and name it as _markup. Then add a render-link.html with the following code:

<a href="{{ .Destination | safeURL }}"{{ with .Title}} title="{{ . }}"{{ end }}{{ if strings.HasPrefix .Destination "http" }} target="_blank" rel="noreferrer noopener"{{ end }}>{{ .Text | safeHTML }}</a>

This code was initiall provided by Agrim Prasad and then improved by h-enk . The main difference is the addition of rel="noreferrer noopener".

In fact, the solution was also earlier provided by bep here .

The main difference between the official solution provided by Hugo and the solution by h-enk is that the former uses rel="noopener" whereas the latter uses rel="noreferrer noopener".

I used the latter one (because it looks longer and safer…), but I guess both will work.

Wait, but how can I make sure that readers will open an internal link rather than opening in a new tab?

As Agrim Prasad pointed out, you can use relative links: [my amazing post](/posts/my-amazing-post/)

Last modified on 2020-10-07