refactor(render-image): see detail

- trim white spaces which can't be eliminated by minify
- unify dict input usages
This commit is contained in:
ZhenShuo Leo
2025-10-11 14:56:51 +08:00
parent 8bfa9926c7
commit 792ec9a601
+49 -28
View File
@@ -1,6 +1,19 @@
{{ define "RenderImageSimple" -}} {{- define "RenderImageSimple" -}}
<img class="my-0 rounded-md" loading="lazy" alt="{{ .alt }}" src="{{ .src }}"> {{- $imgObj := .imgObj -}}
{{- end }} {{- $src := .src -}}
{{- $alt := .alt -}}
<img
class="my-0 rounded-md"
loading="lazy"
decoding="async"
fetchpriority="low"
alt="{{ $alt }}"
src="{{ $src }}"
{{ with $imgObj -}}
{{ with $imgObj.Width }}width="{{ . }}"{{ end }}
{{ with $imgObj.Height }}height="{{ . }}"{{ end }}
{{- end }}>
{{- end -}}
{{- define "RenderImageResponsive" -}} {{- define "RenderImageResponsive" -}}
{{/* Responsive Image {{/* Responsive Image
@@ -50,39 +63,47 @@
data-zoom-src="{{ $imgObj.RelPermalink }}"> data-zoom-src="{{ $imgObj.RelPermalink }}">
{{- end -}} {{- end -}}
{{ define "RenderImageCaption" -}} {{- define "RenderImageCaption" -}}
{{- with .caption }} {{- with .caption -}}
<figcaption>{{ . | markdownify }}</figcaption> <figcaption>{{ . | markdownify }}</figcaption>
{{- end }} {{- end -}}
{{- end }} {{- end -}}
{{- $disableImageOptimizationMD := .Page.Site.Params.disableImageOptimizationMD | default false }} {{- $disableImageOptimizationMD := .Page.Site.Params.disableImageOptimizationMD | default false -}}
{{- $urlStr := .Destination | safeURL -}} {{- $urlStr := .Destination | safeURL -}}
{{- $url := urls.Parse $urlStr -}} {{- $url := urls.Parse $urlStr -}}
{{- $altText := .Text }} {{- $altText := .Text -}}
{{- $caption := .Title }} {{- $caption := .Title -}}
{{- $isRemote := findRE "^(https?|data)" $url.Scheme }} {{- $isRemote := findRE "^(https?|data)" $url.Scheme -}}
{{- $resource := "" }} {{- $resource := "" -}}
{{- if not $isRemote }} {{- if not $isRemote -}}
{{- $resource = or ($.Page.Resources.GetMatch $urlStr) (resources.Get $urlStr) }} {{- $resource = or ($.Page.Resources.GetMatch $urlStr) (resources.Get $urlStr) -}}
{{- end }} {{- end -}}
<figure> <figure>
{{- if $isRemote }} {{- if $isRemote -}}
{{ template "RenderImageSimple" (dict "src" $urlStr "alt" $altText) }} {{- template "RenderImageSimple" (dict "imgObj" "" "src" $urlStr "alt" $altText) -}}
{{- else if $resource }} {{- else if $resource -}}
{{- $isSVG := eq $resource.MediaType.SubType "svg" }} {{- $isSVG := eq $resource.MediaType.SubType "svg" -}}
{{- $shouldOptimize := and (not $disableImageOptimizationMD) (not $isSVG) }} {{- $shouldOptimize := and (not $disableImageOptimizationMD) (not $isSVG) -}}
{{- if $shouldOptimize }} {{- if $shouldOptimize -}}
{{- template "RenderImageResponsive" (dict "imgObj" $resource "alt" $altText) -}} {{- template "RenderImageResponsive" (dict "imgObj" $resource "alt" $altText) -}}
{{- else }} {{- else -}}
{{ template "RenderImageSimple" (dict "src" $resource.RelPermalink "alt" $altText) }} {{/* Not optimize image
{{- end }} If it is an SVG file, pass the permalink
{{- else }} Otherwise, pass the resource to allow width and height attributes
{{ template "RenderImageSimple" (dict "src" $urlStr "alt" $altText) }} */}}
{{- end }} {{- if $isSVG -}}
{{- template "RenderImageSimple" (dict "imgObj" "" "src" $resource.RelPermalink "alt" $altText) -}}
{{- else -}}
{{- template "RenderImageSimple" (dict "imgObj" $resource "src" $resource.RelPermalink "alt" $altText) -}}
{{- end -}}
{{- end -}}
{{- else -}}
{{- template "RenderImageSimple" (dict "imgObj" "" "src" $urlStr "alt" $altText) -}}
{{- end -}}
{{ template "RenderImageCaption" (dict "caption" $caption) }} {{- template "RenderImageCaption" (dict "caption" $caption) -}}
</figure> </figure>