How to render external links with Hugo
Link render hook#
By default Hugo renders links without target="_blank" and consequently external links do not open in a new browser tab. That’s not what I want. Fortunately there’s an easy way to fix this: create a link render hook.
layouts/_default/_markup/render-link.html
1{{- $u := urls.Parse .Destination -}}
2<a href="{{ .Destination | safeURL }}"
3 {{- with .Title }} title="{{ . }}"{{ end -}}
4 {{- if $u.IsAbs }} target="_blank"{{ end -}}
5>
6 {{- with .Text }}{{ . }}{{ end -}}
7</a>
8{{- /* chomp trailing newline */ -}}From now on external links should open in a new browser tab.
Further reading#
- Hugo link render hooks
- An introduction to Hugo’s templating syntax
Read other posts