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 <fuxin1997@gmail.com>

* 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 <fuxin1997@gmail.com>

---------

Co-authored-by: Xin <fuxin1997@gmail.com>
This commit is contained in:
Jan Zerfowski 2025-01-06 06:00:00 +11:00 committed by GitHub
parent 323f4c4b44
commit a27f6eef5e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -13,30 +13,18 @@
{{/* Image processing options */}} {{/* Image processing options */}}
{{- $method := .Get "method" | default "Resize" | humanize -}} {{- $method := .Get "method" | default "Resize" | humanize -}}
{{- $options := .Get "options" | default "800x webp q80" -}} {{- $options := .Get "options" | default "800x webp q80" -}}
{{- $process := .Get "process" | default (printf "%s %s" $method $options) -}}
{{- if and $image (not (urls.Parse $image).Scheme) -}} {{- with or (.Page.Resources.Get $image) (resources.Get $image) -}}
{{/* Process images in assets */}} {{/* Retrieve the $image resource from local or global resources */}}
{{- with resources.Get $image -}} {{- $processed := .Process $process -}}
{{- $processed := "" -}} {{- $width = $processed.Width -}}
{{- if eq $method "Resize" -}} {{- $height = $processed.Height -}}
{{- $processed = (.Resize $options) -}} {{- $image = $processed.RelPermalink -}}
{{- else if eq $method "Fit" -}} {{ else }}
{{- $processed = (.Fit $options) -}} {{/* Otherwise, use relative link of the image */}}
{{- else if eq $method "Fill" -}} {{- if hasPrefix $image "/" -}}
{{- $processed = (.Fill $options) -}} {{- $image = relURL (strings.TrimPrefix "/" $image) -}}
{{- 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 -}}
{{- end -}} {{- end -}}
{{- end -}} {{- end -}}