diff --git a/layouts/partials/author-card.html b/layouts/partials/author-card.html index 330ad37..839874b 100644 --- a/layouts/partials/author-card.html +++ b/layouts/partials/author-card.html @@ -3,18 +3,16 @@ */ -}}
{{- $avatarRes := .Resources.GetMatch "avatar.*" -}} - {{- $avatarSrc := "" -}} - {{- with $avatarRes }}{{ $avatarSrc = .RelPermalink }}{{ else }}{{ with $.Params.avatar }}{{ $avatarSrc = . }}{{ end }}{{ end -}} - {{- with $avatarSrc -}} - - {{ $.Params.name | default $.Title }} - - {{- end -}} + {{- if $avatarRes -}} + {{- $av := $avatarRes.Resize "96x webp" -}} + + {{ $.Params.name | default $.Title }} + + {{- else -}}{{- with .Params.avatar -}} + + {{ $.Params.name | default $.Title }} + + {{- end -}}{{- end -}}
{{ .Params.name | default .Title }} diff --git a/layouts/partials/img.html b/layouts/partials/img.html new file mode 100644 index 0000000..be6fe09 --- /dev/null +++ b/layouts/partials/img.html @@ -0,0 +1,38 @@ +{{- /* + Renders a Hugo image resource as an optimised WebP with responsive srcset. + + Parameters: + res — Hugo image resource (required) + widths — slice of pixel widths to generate, e.g. (slice 600 1200) + default: (slice 800 1600) + sizes — value for the attribute + default: "100vw" + class — CSS classes applied to + alt — alt text (default "") + loading — "lazy" or "eager" (default "lazy") +*/ -}} +{{- $res := .res -}} +{{- $widths := .widths | default (slice 800 1600) -}} +{{- $sizes := .sizes | default "100vw" -}} +{{- $class := .class | default "" -}} +{{- $alt := .alt | default "" -}} +{{- $loading := .loading | default "lazy" -}} + +{{- $entries := slice -}} +{{- $primary := $res -}} +{{- range $i, $w := $widths -}} + {{- $img := $res.Resize (printf "%dx webp" $w) -}} + {{- $entries = $entries | append (printf "%s %dw" $img.RelPermalink $w) -}} + {{- if eq $i 0 -}}{{- $primary = $img -}}{{- end -}} +{{- end -}} +{{ $alt }} diff --git a/layouts/partials/post-card-large.html b/layouts/partials/post-card-large.html index 290dcc0..1f6e8f8 100644 --- a/layouts/partials/post-card-large.html +++ b/layouts/partials/post-card-large.html @@ -10,7 +10,7 @@ {{- if not $thumb -}}{{- $thumb = $post.Resources.GetMatch "01.png" -}}{{- end -}}
{{- if $thumb -}} - {{ $post.Title }} + {{- partial "img.html" (dict "res" $thumb "widths" (slice 800 1200) "sizes" "(max-width: 768px) 100vw, 50vw" "class" "w-full h-full object-cover" "alt" $post.Title) -}} {{- else -}}{{- with $post.Params.banner -}} {{- partial "media.html" (dict "media" . "class" "w-full h-full object-cover") -}} {{- else -}} diff --git a/layouts/partials/post-card.html b/layouts/partials/post-card.html index 35ef1ba..c1fc03f 100644 --- a/layouts/partials/post-card.html +++ b/layouts/partials/post-card.html @@ -10,7 +10,7 @@ {{- if not $thumb -}}{{- $thumb = $post.Resources.GetMatch "01.png" -}}{{- end -}}
{{- if $thumb -}} - {{ $post.Title }} + {{- partial "img.html" (dict "res" $thumb "widths" (slice 600 1000) "sizes" "(max-width: 640px) 100vw, (max-width: 1024px) 50vw, 25vw" "class" "w-full h-full object-cover" "alt" $post.Title) -}} {{- else -}}{{- with $post.Params.banner -}} {{- partial "media.html" (dict "media" . "class" "w-full h-full object-cover") -}} {{- else -}} @@ -40,16 +40,12 @@ {{- $authorPage := site.GetPage (printf "authors/%s" .) -}} {{- with $authorPage -}} {{- $avRes := .Resources.GetMatch "avatar.*" -}} - {{- $avSrc := "" -}} - {{- with $avRes }}{{ $avSrc = .RelPermalink }}{{ else }}{{ with $.Params.avatar }}{{ $avSrc = . }}{{ end }}{{ end -}} - {{- with $avSrc -}} - {{ $authorPage.Params.name | default $authorPage.Title }} - {{- end -}} + {{- if $avRes -}} + {{- $av := $avRes.Resize "64x webp" -}} + {{ $authorPage.Params.name | default $authorPage.Title }} + {{- else -}}{{- with .Params.avatar -}} + {{ $authorPage.Params.name | default $authorPage.Title }} + {{- end -}}{{- end -}} {{ .Params.name | default .Title }} {{- end -}} diff --git a/layouts/posts/single.html b/layouts/posts/single.html index 5ca942f..99a0942 100644 --- a/layouts/posts/single.html +++ b/layouts/posts/single.html @@ -73,7 +73,7 @@ {{- if not $bannerImg -}}{{- $bannerImg = .Resources.GetMatch "01.png" -}}{{- end -}} {{- if $bannerImg -}}
- {{ .Title }} + {{- partial "img.html" (dict "res" $bannerImg "widths" (slice 1200 1800) "sizes" "100vw" "class" "w-full object-cover" "alt" .Title "loading" "eager") -}}
{{- else -}}{{- with .Params.banner -}}
@@ -116,7 +116,7 @@ {{- if $video -}} {{- else -}} - + {{- partial "img.html" (dict "res" . "widths" (slice 800 1200) "sizes" "(max-width: 640px) 100vw, (max-width: 1024px) 50vw, 33vw" "class" "w-full") -}} {{- end -}}