Files
blowfish/layouts/shortcodes/figure.html
ZhenShuo Leo 8bfa9926c7 fix(render-image): improper srcset usage
- correct srcset image w descriptor value
- use compressed image for src fallback
2025-10-11 15:07:20 +08:00

56 lines
2.3 KiB
HTML

{{ $disableImageOptimization := .Site.Params.disableImageOptimization | default false }}
{{ if .Get "default" }}
{{ partial "hugo-embedded/shortcodes/figure-default.html" . }}
{{ else }}
{{- $url := urls.Parse (.Get "src") }}
{{- $altText := .Get "alt" }}
{{- $caption := .Get "caption" }}
{{- $href := .Get "href" }}
{{- $class := .Get "class" }}
{{- $target := .Get "target" | default "_blank" }}
{{- $nozoom := .Get "nozoom" | default false -}}
<figure>
{{- with $href }}<a href="{{ . }}" {{ with $target }}target="{{ . }}"{{ end }} class="inline-block">{{ end -}}
{{- if findRE "^https?" $url.Scheme }}
<img class="my-0 rounded-md{{ with $nozoom }} nozoom{{ end }}{{ with $class }} {{ . }}{{ end }}" src="{{ $url.String }}" alt="{{ $altText }}" />
{{- else }}
{{- $resource := "" }}
{{- if $.Page.Resources.GetMatch ($url.String) }}
{{- $resource = $.Page.Resources.GetMatch ($url.String) }}
{{- else if resources.GetMatch ($url.String) }}
{{- $resource = resources.Get ($url.String) }}
{{- end }}
{{- with $resource }}
{{- if or $disableImageOptimization (eq .MediaType.SubType "svg")}}
<img
class="my-0 rounded-md{{ with $nozoom }} nozoom{{ end }}{{ with $class }} {{ . }}{{ end }}"
src="{{ .RelPermalink }}"
alt="{{ $altText }}"
/>
{{- else }}
<img
class="my-0 rounded-md{{ with $nozoom }} nozoom{{ end }}{{ with $class }} {{ . }}{{ end }}"
loading="lazy"
decoding="async"
fetchpriority="auto"
alt="{{ $altText }}"
{{ with .Width }}width="{{ . }}"{{ end }}
{{ with .Height }}height="{{ . }}"{{ end }}
src="{{ (.Resize "800x").RelPermalink }}"
srcset="
{{- (.Resize "800x").RelPermalink }} 800w,
{{- (.Resize "1280x").RelPermalink }} 1280w"
sizes="(min-width: 768px) 50vw, 65vw"
data-zoom-src="{{ .RelPermalink }}"
/>
{{- end }}
{{- else }}
<img class="my-0 rounded-md{{ with $nozoom }} nozoom{{ end }}{{ with $class }} {{ . }}{{ end }}" src="{{ $url.String }}" alt="{{ $altText }}" />
{{- end }}
{{- end }}
{{ if $href }}</a>{{ end }}
{{ with $caption }}<figcaption>{{ . | markdownify }}</figcaption>{{ end }}
</figure>
{{- end -}}