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