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:
@@ -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="{{ . }}"
|
||||
|
||||
@@ -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 }}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user