Dark Raw

Open Link in New Tab

Hugo does not open links natively in a new tab. This code will help you to achive that - with one line of code!

  1. Create layouts/_default/_markup/render-link.html
  2. Add this line for pre Hugo 0.96:
<a href="{{ .Destination | safeURL }}"{{ with .Title}} title="{{ . }}"{{ end }}{{ if strings.HasPrefix .Destination "http" }} target="_blank"{{ end }}>{{ .Text | markdownify }}</a>

or, in case you use Hugo 0.96 or newer:

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

That’s all! If you get an error that points to markdownify - check your Hugo version!

External links will now open in a new tab > Source + Credit - and local links will open in the same tab > brötchen

© 2022 - Bake the Gloom away!