Skip to content

Commit

Permalink
feat: update figure + picture partials for lazysizes support
Browse files Browse the repository at this point in the history
  • Loading branch information
h-enk committed Jan 29, 2024
1 parent fa55453 commit f1a2302
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
24 changes: 13 additions & 11 deletions layouts/partials/figure.html
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
{{- $fetchPriority := or .fetchpriority site.Params.hyas_images.defaults.fetchpriority }}
{{- $loading := or .loading site.Params.hyas_images.defaults.loading }}
{{- $process := or .process site.Params.hyas_images.defaults.process }}
{{- $lqip := or .lqip site.Params.hyas_images.defaults.lqip }}
{{- $src := or .src "" }}
{{- $title := or .title "" }}
{{- $caption := or .caption "" }}
Expand Down Expand Up @@ -164,17 +165,17 @@
{{- $r = . }}
{{- end }}

{{/* Convert to webp. */}}
{{- $f := site.Params.hyas_images.defaults.format }}
{{- if eq $f "webp" }}
{{- $r = $r.Resize (printf "%dx%d webp" $r.Width $r.Height) }}
{{- end }}

{{- /* Process image. */}}
{{- with $process }}
{{- $r = $r.Process $process }}
{{- end }}

{{- /* Process LQIP. */}}
{{- $l := "" }}
{{- with $lqip }}
{{- $l = $r.Resize . }}
{{- end }}

{{- /* Determine widths for srcset generation. */}}
{{- $widths := slice }}
{{- if $width }}
Expand Down Expand Up @@ -216,29 +217,30 @@
{{- /* Render. */}}
<figure>
<img
srcset="data:{{ $l.MediaType }};base64,{{ $l.Content | base64Encode }}"
{{- range $formats }}
{{- with index $im . }}
{{- $sizes := $stdSizes }}
{{- with $width }}
{{- $sizes = printf "%dpx" . }}
{{- end }}
srcset="
data-srcset="
{{- range $k, $_ := . }}
{{- if $k }},{{- end }}
{{- printf `%s %dw` .RelPermalink .Width }}
{{- end }}"
sizes="{{ $stdSizes }}"
data-sizes="{{ $stdSizes }}"
{{- end }}
{{- end }}
src="{{ $fi.RelPermalink }}"
width="{{ string $r.Width }}"
height="{{ string $r.Height }}"
decoding="{{ $decoding }}"
fetchpriority="{{ site.Params.hyas_images.defaults.fetchpriority }}"
fetchpriority="{{ $fetchPriority }}"
loading="{{ $loading }}"
alt="{{ $alt }}"
{{- with .title -}}title="{{ $title }}"{{- end -}}
{{- with .class -}}class="{{ $class }}"{{- end -}}
{{- with .title }}title="{{ $title }}"{{- end }}
class="lazyload blur-up{{- with .class }} {{ . }}{{- end }}"
/>
{{- with $caption }}<figcaption>{{ . }}</figcaption>{{- end }}
</figure>
Expand Down
26 changes: 14 additions & 12 deletions layouts/partials/picture.html
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
{{- $fetchPriority := or .fetchpriority site.Params.hyas_images.defaults.fetchpriority }}
{{- $loading := or .loading site.Params.hyas_images.defaults.loading }}
{{- $process := or .process site.Params.hyas_images.defaults.process }}
{{- $lqip := or .lqip site.Params.hyas_images.defaults.lqip }}
{{- $src := or .src "" }}
{{- $title := or .title "" }}
{{- $width := or (int .width) 0 }}
Expand Down Expand Up @@ -161,17 +162,17 @@
{{- $r = . }}
{{- end }}

{{/* Convert to webp. */}}
{{- $f := site.Params.hyas_images.defaults.format }}
{{- if eq $f "webp" }}
{{- $r = $r.Resize (printf "%dx%d webp" $r.Width $r.Height) }}
{{- end }}

{{- /* Process image. */}}
{{- with $process }}
{{- $r = $r.Process $process }}
{{- end }}

{{- /* Process LQIP. */}}
{{- $l := "" }}
{{- with $lqip }}
{{- $l = $r.Resize . }}
{{- end }}

{{- /* Determine widths for srcset generation. */}}
{{- $widths := slice }}
{{- if $width }}
Expand Down Expand Up @@ -218,24 +219,25 @@
{{- with $width }}
{{- $sizes = printf "%dpx" . }}
{{- end }}
<source type="{{ (index . 0).MediaType.Type }}" srcset="
<source type="{{ (index . 0).MediaType.Type }}" data-srcset="
{{- range $k, $_ := . }}
{{- if $k }},{{- end }}
{{- printf `%s %dw` .RelPermalink .Width }}
{{- end }}"
sizes="{{ $stdSizes }}">
data-sizes="{{ $stdSizes }}">
{{- end }}
{{- end }}
<img
src="{{ $fi.RelPermalink }}"
src="data:{{ $l.MediaType }};base64,{{ $l.Content | base64Encode }}"
data-src="{{ $fi.RelPermalink }}"
width="{{ string $r.Width }}"
height="{{ string $r.Height }}"
decoding="{{ $decoding }}"
fetchpriority="{{ site.Params.hyas_images.defaults.fetchpriority }}"
fetchpriority="{{ $fetchPriority }}"
loading="{{ $loading }}"
alt="{{ $alt }}"
{{- with .title -}}title="{{ $title }}"{{- end -}}
{{- with .class -}}class="{{ $class }}"{{- end -}}
{{- with .title }}title="{{ $title }}"{{- end }}
class="lazyload blur-up{{- with .class }} {{ . }}{{- end }}"
/>
</picture>

Expand Down

0 comments on commit f1a2302

Please sign in to comment.