Skip to content

Instantly share code, notes, and snippets.

@killbus
Last active November 30, 2021 14:39
Show Gist options
  • Save killbus/f47efbd13d96b437b9d218334508bffb to your computer and use it in GitHub Desktop.
Save killbus/f47efbd13d96b437b9d218334508bffb to your computer and use it in GitHub Desktop.

How to Open Link in New Tab with Hugo's new Goldmark Markdown Renderer

layouts
└── _default
    └── _markup
        └── render-link.html
{{ $baseurl := urls.Parse .Page.Site.BaseURL }}
{{ $desturl := urls.Parse .Destination }}
<a href="{{ .Destination | safeURL }}"{{ with .Title}} title="{{ . }}"{{ end }}{{ if and (strings.HasPrefix .Destination "http") (not (strings.HasPrefix $desturl.Host $baseurl.Host)) }} target="_blank" rel="nofollow noopener"{{ end }}>{{ .Text }}</a>

hugo official docs

@djibe
Copy link

djibe commented Nov 30, 2021

Hi, better trim whitespaces with

{{- $baseurl := urls.Parse .Page.Site.BaseURL -}}
{{- $desturl := urls.Parse .Destination -}}
<a href="{{ .Destination | safeURL }}"{{ with .Title}} title="{{ . }}"{{ end }}{{ if and (strings.HasPrefix .Destination "http") (not (strings.HasPrefix $desturl.Host $baseurl.Host)) }} target="_blank" rel="nofollow noopener"{{ end }}>{{ .Text }}</a>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment