refactor: image logic in page (#1255)

* fix: add SVG check in helper/image before accessing .Width and .Height

* refactor: drop image resizing logic in render-image, and use helper/image to process image

* fix: render-image $permalink not correct
This commit is contained in:
Jimmy
2026-01-30 15:07:14 +01:00
committed by GitHub
parent ff2e308445
commit 31022581d8
2 changed files with 33 additions and 56 deletions
+5 -27
View File
@@ -1,34 +1,12 @@
{{- $image := .Page.Resources.GetMatch (printf "%s" (.Destination | safeURL)) -}}
{{- $Permalink := .Destination | relURL | safeURL -}}
{{- $image := partial "helper/image" (dict "Resources" .Page.Resources "Image" .Destination) -}}
{{- $alt := .PlainText | safeHTML -}}
{{- $Width := 0 -}}
{{- $Height := 0 -}}
{{- $Srcset := "" -}}
{{/* SVG and external images won't work with gallery layout, because their width and height attributes are unknown */}}
{{- $galleryImage := false -}}
{{- $galleryImage := and $image.Height $image.Width -}}
{{- if $image -}}
{{- $notSVG := ne (path.Ext .Destination) ".svg" -}}
{{- $Permalink = $image.RelPermalink -}}
{{- if $notSVG -}}
{{- $Width = $image.Width -}}
{{- $Height = $image.Height -}}
{{- $galleryImage = true -}}
{{- if .Page.Site.Params.imageProcessing.content.enabled -}}
{{- $small := $image.Resize `480x` -}}
{{- $big := $image.Resize `1024x` -}}
{{- $Srcset = printf `%s 480w, %s 1024w` $small.RelPermalink $big.RelPermalink -}}
{{- end -}}
{{- end -}}
{{- end -}}
<img src="{{ $Permalink }}"
{{ with $Width }}width="{{ . }}"{{ end }}
{{ with $Height }}height="{{ . }}"{{ end }}
{{ with $Srcset }}srcset="{{ . }}"{{ end }}
<img src="{{ $image.Permalink }}"
{{ with $image.Width }}width="{{ . }}"{{ end }}
{{ with $image.Height }}height="{{ . }}"{{ end }}
loading="lazy"
{{ with $alt }}
alt="{{ . }}"
+28 -29
View File
@@ -9,45 +9,44 @@
/// Height: Image height
/// Width: Image width
{{ $result := dict
"Local" false
"Resource" nil
"Permalink" (.Image | absURL)
"Width" nil
"Height" nil
}}
{{ $result := "" }}
{{ if not .Image }}
{{ $result = "" }}
{{ else }}
{{ if .Image }}
{{ $url := urls.Parse .Image }}
{{ $resource := "" }}
{{ if not (in (slice "https" "http") $url.Scheme) }}
{{ $resource := .Resources.Get (printf "%s" (.Image | safeURL)) }}
{{ if $resource }}
{{ $resource = .Resources.Get (printf "%s" (.Image | safeURL)) }}
{{ else }}
{{ with try (resources.GetRemote $url) }}
{{ with .Err }}
{{ errorf "%s" . }}
{{ else with .Value }}
{{ $resource = . }}
{{ else }}
{{ errorf "Unable to get remote resource %q" $url }}
{{ end }}
{{ end }}
{{ end }}
{{ if $resource }}
{{- $isSVG := eq $resource.MediaType.SubType "svg" -}}
{{- if $isSVG -}}
{{ $result = dict
"Local" true
"Resource" $resource
"Permalink" $resource.RelPermalink
"Height" nil
"Width" nil
}}
{{- else -}}
{{- $result = dict
"Local" true
"Resource" $resource
"Permalink" $resource.RelPermalink
"Height" $resource.Height
"Width" $resource.Width
}}
{{ end }}
{{ else }}
{{ with try (resources.GetRemote $url) }}
{{ with .Err }}
{{ errorf "%s" . }}
{{ else with .Value }}
{{ $result = dict
"Local" false
"Resource" .
"Permalink" .RelPermalink
"Height" .Height
"Width" .Width
}}
{{ else }}
{{ errorf "Unable to get remote resource %q" $url }}
{{ end }}
{{ end }}
{{- end -}}
{{ end }}
{{ end }}