I tried to use the example here. It works great for links with anchor to another page, but if I use an anchor to this page, such as a link [like](#this)
, it does not work correctly.
{{ $link := .Destination }}
{{ $isRemote := strings.HasPrefix $link "http" }}
{{- if not $isRemote -}}
{{ $url := urls.Parse .Destination }}
{{ $fragment := "" }}
{{- with $url.Fragment }}{{ $fragment = printf "#%s" . }}{{ end -}}
{{- if $url.Path -}}{{/* ensure it referring to another page */}}
{{- with .Page.GetPage $url.Path }}{{ $url.Path }}{{ $link = printf "%s%s" .RelPermalink $fragment }}{{.Permalink}}{{ end }}
{{- else -}}
{{ $link = $fragment }}
{{ end }}
{{ end -}}
<a href="{{ $link | safeURL }}"{{ with .Title}} title="{{ . }}"{{ end }}{{ if $isRemote }} target="_blank"{{ end }}>{{ .Text | safeHTML }}</a>