HomeBikeshedComponents

Render Image

Published:

This render hook builds off of Hugo’s built in one which allows for robust linking to page and site resources with a couple of changes:

  1. Added width and height attributes to the images (this is overridden by any dimensions specified in .Attributes)
  2. Added warnings when a referenced image isn’t found in the site’s assets folder.

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>
{{- /**/ -}}