refactor(image): centralize page image params and warn on bad shape

Route all .Params.image access through a single normalizer partial that
returns the params as a map and emits a warnf with the expected shape
when frontmatter is set but not an object.
This commit is contained in:
George Cushen committed 2026-05-27 03:53:21 +01:00
1 parent a93892fc61
commit 64cdb4a81a
5 files changed
+18 -18

No files matched your search

@@ -13,10 +13,8 @@
{{ $resource := (.Resources.ByType "image").GetMatch "*featured*" }}
{{ if not (reflect.IsResource $resource) }}
{{/* Otherwise fall back the image file specified in front matter */}}
{{ $filename := "" }}
{{ if and .Params.image (reflect.IsMap .Params.image) }}
{{ $filename = .Params.image.filename }}
{{ end }}
{{ $image_params := partial "functions/get_page_image_params.html" . }}
{{ $filename := $image_params.filename | default "" }}
{{/* Search in page folder */}}
{{ $resource = (.Resources.ByType "image").GetMatch $filename }}
{{/* Otherwise in `assets/media/` folder */}}
@@ -0,0 +1,12 @@
{{/* Return the page's `image` frontmatter as a map, or an empty map if unset.
Warns if `image` is set but not an object. */}}
{{ $img := .Params.image }}
{{ $result := dict }}
{{ if $img }}
{{ if reflect.IsMap $img }}
{{ $result = $img }}
{{ else }}
{{ warnf "page %q: `image` frontmatter must be an object, e.g.\n image:\n filename: cover.jpg\nGot: %v" .Path $img }}
{{ end }}
{{ end }}
{{ return $result }}
@@ -8,10 +8,8 @@
{{ end }}
{{ $resource := partial "functions/get_featured_image.html" $item }}
{{ $anchor := "Center" }}
{{ if and $item.Params.image (reflect.IsMap $item.Params.image) }}
{{ $anchor = $item.Params.image.focal_point | default "Center" }}
{{ end }}
{{ $image_params := partial "functions/get_page_image_params.html" $item }}
{{ $anchor := $image_params.focal_point | default "Center" }}
{{ $fill_image := .config.fill_image | default true }}
{{ $showDate := .config.show_date | default true }}
{{ $showReadTime := .config.show_read_time | default true }}
+1 -5
View File
@@ -141,11 +141,7 @@
{{ else }}
{{/* Fallback to featured image if no slides */}}
{{ $featured := partial "functions/get_featured_image.html" . }}
{{/* Safely extract image parameters - handle case where image is not a map */}}
{{ $image_params := dict }}
{{ if and .Params.image (reflect.IsMap .Params.image) }}
{{ $image_params = .Params.image }}
{{ end }}
{{ $image_params := partial "functions/get_page_image_params.html" . }}
{{ if and $featured (not $image_params.preview_only) }}
{{/* Fit image within max size. */}}
{{ $image := $featured }}
+1 -5
View File
@@ -147,11 +147,7 @@
{{ $featured := partial "functions/get_featured_image.html" . }}
{{/* Safely extract image parameters - handle case where image is not a map */}}
{{ $image_params := dict }}
{{ if and .Params.image (reflect.IsMap .Params.image) }}
{{ $image_params = .Params.image }}
{{ end }}
{{ $image_params := partial "functions/get_page_image_params.html" . }}
{{/* Featured image layout */}}
{{ if and $featured (not $image_params.preview_only) }}
{{/* Fit image within max size. */}}