{{/* From Hugo, Apache v2 license
  https://github.com/gohugoio/hugo/blob/master/tpl/tplimpl/embedded/templates/_markup/render-link.html
*/}}
{{- $u := urls.Parse .Destination -}}
{{- $href := $u.String -}}
{{- if strings.HasPrefix $u.String "#" -}}
  {{- $href = printf "%s#%s" .PageInner.RelPermalink $u.Fragment -}}
{{- else if and $href (not $u.IsAbs) -}}
  {{- $path := strings.TrimPrefix "./" $u.Path -}}
  {{- with or
    ($.PageInner.GetPage $path)
    ($.PageInner.Resources.Get $path)
    (resources.Get $path)
  -}}
    {{- $href = .RelPermalink -}}
    {{- with $u.RawQuery -}}
      {{- $href = printf "%s?%s" $href . -}}
    {{- end -}}
    {{- with $u.Fragment -}}
      {{- $href = printf "%s#%s" $href . -}}
    {{- end -}}
  {{- end -}}
{{- end -}}

{{/* Open external links in a new tab if
  configured to do so by the page param externalLinkForceNewTab
  or site param article.externalLinkForceNewTab
  and the link starts with http: or https:
*/}}
{{- $forceNewTab := .Page.Params.externalLinkForceNewTab | default (.Page.Site.Params.article.externalLinkForceNewTab | default true) -}}
{{- $isExternal := or (strings.HasPrefix .Destination "http:") (strings.HasPrefix .Destination "https:") -}}
{{- $newTab := and $forceNewTab $isExternal -}}

<a href="{{ $href }}" {{ with .Title }}title="{{ . }}"{{ end }}
  {{- if $newTab }} target="_blank" rel="noreferrer"{{ end }}>
  {{- .Text | safeHTML -}}
</a>
{{- /* Trim EOF */ -}}
