hugo-theme-relearn/layouts/partials/shortcodes/image.html
2022-11-18 21:49:02 +01:00

54 lines
No EOL
1.8 KiB
HTML

{{- $context := .context }}
{{- $url := .url }}
{{- $title := .title }}
{{- $alt := .alt }}
{{- $classes := "" }}
{{- $featherlight := true }}
{{- $height := "auto" }}
{{- $width := "auto" }}
{{- $dest_url := urls.Parse $url }}
{{- $dest_path := path.Clean $dest_url.Path }}
{{- $image := "" }}
{{- if ($dest_url.IsAbs) }}
{{/* this image does not belong to the site, get it from external */}}
{{- $image = resources.GetRemote $url }}
{{- else }}
{{- /* Get local page resource image. */}}
{{- $image = $context.Resources.Get $dest_path }}
{{- if not $image }}
{{- /* Get local image from assets directory. */}}
{{- $dest_path := path.Clean $dest_url.Path }}
{{- $image = resources.Get $dest_path }}
{{- end }}
{{- end }}
{{- if $image }}
{{- $url = $image.RelPermalink }}
{{- if $dest_url.RawQuery }}
{{- $url = printf "%s?%s" $url $dest_url.RawQuery }}
{{- end }}
{{- if ne $image.MediaType.SubType "svg" }}
{{- $height = printf "%dpx" $image.Height }}
{{- $width = printf "%dpx" $image.Width }}
{{- end }}
{{- end }}
{{- if $dest_url.RawQuery }}
{{- $classes = delimit (split ($dest_url.Query.Get "classes") ",") " " }}
{{- $featherlight = (ne ($dest_url.Query.Get "featherlight") "false") }}
{{- if or ($dest_url.Query.Get "height") ($dest_url.Query.Get "width") }}
{{- $height = "auto" }}
{{- $width = "auto" }}
{{- end }}
{{- with $dest_url.Query.Get "height" }}
{{ $height = . }}
{{- end }}
{{- with $dest_url.Query.Get "width" }}
{{ $width = . }}
{{- end }}
{{- end }}
{{- if $featherlight }}
<a href="{{ $url | safeURL }}" data-featherlight="image">
{{- end }}
<img src="{{ $url | safeURL }}" alt="{{ $alt }}"{{ with $title }} title="{{ . }}"{{ end }}{{ with $classes }} class="{{ . }}"{{ end }} style="height: {{ $height }}; width: {{ $width }};" loading="lazy">
{{- if $featherlight }}
</a>
{{- end }}