hugo-geekblog/layouts/shortcodes/img.html

66 lines
2.3 KiB
HTML

{{- $source := ($.Page.Resources.ByType "image").GetMatch (printf "%s" (.Get "name")) }}
{{- $customAlt := .Get "alt" }}
{{- $customSize := .Get "size" | lower }}
{{- $lazyLoad := default (default true $.Site.Params.geekblogImageLazyLoading) (.Get "lazy") }}
{{- $data := newScratch }}
{{- with $source }}
{{- $caption := default .Title $customAlt }}
{{- $isSVG := (eq .MediaType.SubType "svg") }}
{{- $origin := . }}
{{- if $isSVG }}
{{- $data.SetInMap "size" "tiny" "320" }}
{{- $data.SetInMap "size" "small" "600" }}
{{- $data.SetInMap "size" "medium" "1200" }}
{{- $data.SetInMap "size" "large" "1800" }}
{{- else }}
{{- $data.SetInMap "size" "tiny" "320x"}}
{{- $data.SetInMap "size" "small" "600x" }}
{{- $data.SetInMap "size" "medium" "1200x" }}
{{- $data.SetInMap "size" "large" "1800x" }}
{{- end -}}
<div class="flex justify-center">
<figure class="gblog-post__figure">
<a class="gblog-markdown__link--raw" href="{{ .Permalink }}">
<picture>
{{- $size := $data.Get "size" }}
{{- if not $isSVG }}
{{- if ne $customSize "origin" }}
<source
{{- if $customSize }}
srcset="{{ ($origin.Resize (index $size $customSize)).Permalink }}"
{{- else }}
srcset="{{ ($origin.Resize (index $size "small")).Permalink }} 600w, {{ ($origin.Resize (index $size "medium")).Permalink }} 1200w" sizes="100vw"
{{- end }}
/>
{{- end }}
{{- end }}
<img
{{- if $isSVG }}
src="{{ $origin.Permalink }}" width="{{ index $size (default "medium" $customSize) }}"
{{- else }}
{{- if $lazyLoad }}{{ print " loading=\"lazy\"" | safeHTMLAttr }}{{- end }}
{{- if eq $customSize "origin" }}
src="{{ $origin.Permalink }}"
{{- else }}
src="{{ ($origin.Resize (index $size "large")).Permalink }}"
{{- end }}
alt="{{ $caption }}"
{{- end }}
/>
</picture>
</a>
{{- with $caption }}
<figcaption>
{{ . }}
{{- with $source.Params.credits }}
{{ printf " (%s)" . | $.Page.RenderString }}
{{- end }}
</figcaption>
{{- end }}
</figure>
</div>
{{- end }}