Hi everyone,
I’m using Hugo for building my websites. I’m trying image processing of hugo, currently, we are using 1000+ images and it will grow with time. I’m using the shortcode for rendering images from markdown. I wanted to try the image processing of hugo, I wanted to make it responsive to reduce layouts shifts and improve CLS. Is there any way to build responsive images either with hugo processing or some other alternatives?
I’m using mounts to change the directory of images from static to assets.
[[module.mounts]]
source = 'assets'
target = 'assets'
[[module.mounts]]
source = 'static'
target = 'assets'
Here is the shortcode I’m using for my image.
{{ if .Get "caption" }}
{{ $.Scratch.Set "caption" (.Get "caption") }}
{{ end }}
<!-- resolve absolute image path -->
{{ $permalink := $.Page.Permalink }}
{{ $image := .Get "src" }}
{{- $url := (relURL $image) -}}
{{/* {{- if not (fileExists (print "static" $url) ) -}}
{{- errorf "%s references an image does not exist — %q" $.Page.Path $url -}}
{{- end -}} */}}
<figure class="image--main {{ with .Get "class" }} {{.}} {{ end }}" {{ with .Get "width" }}style="width: {{ . }};" {{
end }} {{ with .Get "height" }}style="height: {{ . }};" {{ end }}>
<a {{ if .Get "lightbox" }} data-lightbox="{{ .Get "lightbox" | markdownify | plainify }}" {{ else }}
data-lightbox="image-{{ $image }}" {{ end }} href="{{ $url }}"
{{ with .Get "target" }} target="{{ . }}" {{ end }}
{{ with .Get "rel" }} rel="{{ . }}" {{ end }}>
<img src="{{ $url }}"
{{ if .Get "alt" }} alt="{{ .Get "alt" | markdownify | plainify }}" {{ end }}
{{ with .Get "align" }}align="{{ . }}" {{ end }}
{{ with .Get "title" }}title="{{ . }}" {{ end }}
decoding="async" loading="lazy" class="lazyload img-shortcode"/>
</a>
{{ if ($.Scratch.Get "caption") }}
<figcaption>
<span class="img--caption">
Figure {{ $.Page.Scratch.Get "fig" }}. {{ $.Scratch.Get "caption" | markdownify | plainify }}
{{ if .Get "attr" }}
[{{- with .Get "attrlink"}}<a href="{{ . | relURL }}">{{ end }}{{ .Get "attr" | markdownify }}{{ if .Get
"attrlink"}}</a>{{ end -}}]
{{ end }}
</span>
</figcaption>
{{ end }}
</figure>
{{ .Page.Scratch.Add "fig" 1 }}
{{ $.Scratch.Delete "caption"}}
Thanks