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)) -}}
|
{{- $image := partial "helper/image" (dict "Resources" .Page.Resources "Image" .Destination) -}}
|
||||||
{{- $Permalink := .Destination | relURL | safeURL -}}
|
|
||||||
{{- $alt := .PlainText | safeHTML -}}
|
{{- $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 */}}
|
{{/* 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 -}}
|
<img src="{{ $image.Permalink }}"
|
||||||
{{- $notSVG := ne (path.Ext .Destination) ".svg" -}}
|
{{ with $image.Width }}width="{{ . }}"{{ end }}
|
||||||
{{- $Permalink = $image.RelPermalink -}}
|
{{ with $image.Height }}height="{{ . }}"{{ end }}
|
||||||
|
|
||||||
{{- 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 }}
|
|
||||||
loading="lazy"
|
loading="lazy"
|
||||||
{{ with $alt }}
|
{{ with $alt }}
|
||||||
alt="{{ . }}"
|
alt="{{ . }}"
|
||||||
|
|||||||
@@ -9,45 +9,44 @@
|
|||||||
/// Height: Image height
|
/// Height: Image height
|
||||||
/// Width: Image width
|
/// Width: Image width
|
||||||
|
|
||||||
{{ $result := dict
|
{{ $result := "" }}
|
||||||
"Local" false
|
|
||||||
"Resource" nil
|
|
||||||
"Permalink" (.Image | absURL)
|
|
||||||
"Width" nil
|
|
||||||
"Height" nil
|
|
||||||
}}
|
|
||||||
|
|
||||||
{{ if not .Image }}
|
{{ if .Image }}
|
||||||
{{ $result = "" }}
|
|
||||||
{{ else }}
|
|
||||||
{{ $url := urls.Parse .Image }}
|
{{ $url := urls.Parse .Image }}
|
||||||
|
{{ $resource := "" }}
|
||||||
{{ if not (in (slice "https" "http") $url.Scheme) }}
|
{{ if not (in (slice "https" "http") $url.Scheme) }}
|
||||||
{{ $resource := .Resources.Get (printf "%s" (.Image | safeURL)) }}
|
{{ $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 }}
|
{{ if $resource }}
|
||||||
|
{{- $isSVG := eq $resource.MediaType.SubType "svg" -}}
|
||||||
|
{{- if $isSVG -}}
|
||||||
{{ $result = dict
|
{{ $result = dict
|
||||||
|
"Local" true
|
||||||
|
"Resource" $resource
|
||||||
|
"Permalink" $resource.RelPermalink
|
||||||
|
"Height" nil
|
||||||
|
"Width" nil
|
||||||
|
}}
|
||||||
|
{{- else -}}
|
||||||
|
{{- $result = dict
|
||||||
"Local" true
|
"Local" true
|
||||||
"Resource" $resource
|
"Resource" $resource
|
||||||
"Permalink" $resource.RelPermalink
|
"Permalink" $resource.RelPermalink
|
||||||
"Height" $resource.Height
|
"Height" $resource.Height
|
||||||
"Width" $resource.Width
|
"Width" $resource.Width
|
||||||
}}
|
}}
|
||||||
{{ end }}
|
{{- 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 }}
|
{{ end }}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user