HomeBikeshedComponents

render-link.html

Published: Updated:

This render hook builds off of Hugo’s built-in one and provides robust linking to pages and site assets. To this I made three changes:

  1. Trimming /content/ from the .Destination so I can reference files in the structure it appears in VS Code.
  2. Add a warning in case a referenced page or asset doesn’t exist, pointing to the offending file.
  3. Added support for attributes specified as a JSON object in the markdown title.

The next thing I’m trying to do is link to non-HTML items, like the site’s RSS feed.

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" $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 -}}
  {{- else -}}
    {{- if $.PageInner.File -}}
      {{- warnf "Link %s referenced in %s not found." $.Destination $.PageInner.File.Filename -}}
    {{- else -}}
      {{- warnf "Link %s not found." $.Destination -}}
    {{- end -}}
  {{- end -}}
{{- end -}}
{{- $attributes := dict "href" $href -}}
{{- with .Title -}}
    {{- if hasPrefix . `{` -}}
        {{- $attrObj := transform.Unmarshal . -}}
        {{- range $k, $v := $attrObj -}}
            {{- $attributes = merge $attributes (dict $k $v) -}}
        {{- end -}}
    {{- else -}}
        {{- $attributes = merge $attributes (dict "title" . ) -}}
    {{- end -}}
{{- end -}}
<a
  {{- range $k, $v := $attributes -}}
    {{- if $v -}}
      {{- printf " %s=%q" $k $v | safeHTMLAttr -}}
    {{- end -}}
  {{- end -}}
  >{{ .Text | safeHTML }}</a>
{{- /**/ -}}