Home → Bikeshed → Components
Render Link
Published:
This render hook builds off of Hugo’s built-in one and provides robust linking to pages and site assets. To this I made two changes:
- Trimming
/content/
from the.Destination
so I can reference files in the structure it appears in VS Code. - Add a warning in case a referenced page or asset doesn’t exist, pointing to the offending file.
Source Code
{{- $u := urls.Parse .Destination -}}
{{- $href := $u.String -}}
{{- if strings.HasPrefix $u.String "#" }}
{{- $href = printf "%s#%s" .PageInner.RelPermalink $u.Fragment }}
{{- else if not $u.IsAbs -}}
{{- $path := strings.TrimPrefix "./" $u.Path }}
{{- $path := strings.TrimPrefix "/content" $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 -}}
{{- else -}}
{{- with $.PageInner.File -}}
{{- warnf "Link %s in file %s not found" $.Destination .Filename -}}
{{- else -}}
{{- warnf "Link %s not found" $.Destination -}}
{{- end -}}
{{- end -}}
{{- end -}}
{{- $attributes := dict "href" $href "title" (.Title | transform.HTMLEscape) -}}
<a
{{- range $k, $v := $attributes -}}
{{- if $v -}}
{{- printf " %s=%q" $k $v | safeHTMLAttr -}}
{{- end -}}
{{- end -}}
>{{ .Text | safeHTML }}</a>
{{- /**/ -}}