{{$trimmed := strings.TrimSuffix ".md" (.Destination | safeURL)}}
{{$trimmed = replace $trimmed "%20" "-" }}
{{$external := strings.HasPrefix $trimmed "http" }}
{{- if $external -}}
{{ .Text | safeHTML }}
{{- else -}}
{{$fixedUrl := (cond (hasPrefix $trimmed "/") $trimmed (print "/" $trimmed)) | urlize}}
{{$rooted := strings.TrimRight "/" (.Page.GetPage $fixedUrl).RelPermalink }}
{{ .Text | safeHTML }}
{{- end -}}