From a27f6eef5e2fbdc9570cb05a55befc80427dd346 Mon Sep 17 00:00:00 2001 From: Jan Zerfowski <54621022+jzerfowski@users.noreply.github.com> Date: Mon, 6 Jan 2025 06:00:00 +1100 Subject: [PATCH] refactor: use generic .Process for card processing and allow bundled images (#538) * Change processing method to more generic hugo .Process and allow resources from local .Page.Resources as well as global resources * Add comments * Fix double curly brackets error Co-authored-by: Xin * Maintain backward compatibility by using defaulting $process to $methd $options https://github.com/imfing/hextra/pull/538#discussion_r1903250264 * Use printf instead of delimit Co-authored-by: Xin --------- Co-authored-by: Xin --- layouts/shortcodes/card.html | 34 +++++++++++----------------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/layouts/shortcodes/card.html b/layouts/shortcodes/card.html index 4b18354..be4612f 100644 --- a/layouts/shortcodes/card.html +++ b/layouts/shortcodes/card.html @@ -13,30 +13,18 @@ {{/* Image processing options */}} {{- $method := .Get "method" | default "Resize" | humanize -}} {{- $options := .Get "options" | default "800x webp q80" -}} +{{- $process := .Get "process" | default (printf "%s %s" $method $options) -}} -{{- if and $image (not (urls.Parse $image).Scheme) -}} - {{/* Process images in assets */}} - {{- with resources.Get $image -}} - {{- $processed := "" -}} - {{- if eq $method "Resize" -}} - {{- $processed = (.Resize $options) -}} - {{- else if eq $method "Fit" -}} - {{- $processed = (.Fit $options) -}} - {{- else if eq $method "Fill" -}} - {{- $processed = (.Fill $options) -}} - {{- else if eq $method "Crop" -}} - {{- $processed = (.Crop $options) -}} - {{- else -}} - {{- errorf "Invalid image processing command: Must be one of Crop, Fit, Fill or Resize." -}} - {{- end -}} - {{- $width = $processed.Width -}} - {{- $height = $processed.Height -}} - {{- $image = $processed.RelPermalink -}} - {{- else -}} - {{/* Otherwise, use relative link of the image */}} - {{- if hasPrefix $image "/" -}} - {{- $image = relURL (strings.TrimPrefix "/" $image) -}} - {{- end -}} +{{- with or (.Page.Resources.Get $image) (resources.Get $image) -}} + {{/* Retrieve the $image resource from local or global resources */}} + {{- $processed := .Process $process -}} + {{- $width = $processed.Width -}} + {{- $height = $processed.Height -}} + {{- $image = $processed.RelPermalink -}} +{{ else }} + {{/* Otherwise, use relative link of the image */}} + {{- if hasPrefix $image "/" -}} + {{- $image = relURL (strings.TrimPrefix "/" $image) -}} {{- end -}} {{- end -}}