Linking to static files with Hugo

I got frustrated by this too and ended up creating a static shortcode that works like the ref and relref built-ins. Basically it takes the path and, if it doesn’t have a protocol in front, appends it to the BaseURL.

You can download a copy if you want, or the code is below:

{{- .Scratch.Set "path" (.Get 0) -}}
{{- if hasPrefix (.Scratch.Get "path") "/" -}}
  {{- .Scratch.Set "path" (slicestr (.Scratch.Get "path") 1) -}}
{{- end -}}
{{- .Scratch.Get "path" | absLangURL -}}

All but the last line is there to remove a leading "/", I think because it caused issues. You can replace absLangURL with a different function, if you prefer.

Usage: ![foo]({{< static "img/foo.png" >}})