hugo-theme-relearn/layouts/partials/shortcodes/image.html

80 lines
No EOL
3.4 KiB
HTML

{{- $page := .page }}
{{- if and (not $page) .context }}
{{- $page = .context }}
{{- warnf "%q: DEPRECATED parameter 'context' for shortcode 'image' found, use 'page' instead; see https://mcshelby.github.io/hugo-theme-relearn/basics/migration#5180" $page.File.Filename }}
{{- end }}
{{- $url := .url }}
{{- $title := .title }}
{{- $alt := .alt }}
{{- $effects := dict "border" false "lightbox" true "shadow" false }}
{{- if $page.Site.Params.imageeffects }}
{{- $effects = merge $effects $page.Site.Params.imageeffects }}
{{- end }}
{{- if $page.Params.imageEffects }}
{{- $effects = merge $effects $page.Params.imageEffects }}
{{- end }}
{{- $height := "auto" }}
{{- $width := "auto" }}
{{- $dest_url := urls.Parse $url }}
{{- $dest_path := $dest_url.Path }}
{{- $image := $page.Resources.GetMatch (path.Clean $dest_url.Path) }}
{{- if not $image }}
{{- $image = .Resources.GetMatch $dest_path }}
{{- end }}
{{- if not $image }}
{{- $image = .Resources.GetRemote $url }}
{{- end }}
{{- if $image }}
{{- $url = $image.RelPermalink }}
{{- if $dest_url.RawQuery }}
{{- $url = printf "%s?%s" $url $dest_url.RawQuery }}
{{- end }}
{{- end }}
{{- if not $image }}
{{- if and ($page.Site.Params.render_hooks.errorlevel "warning") (not (or (strings.HasPrefix .url "http://") (strings.HasPrefix .url "https://") )) }}
{{- warnf "%q: Image %s is not a resource but linked anyways." $page.File.Filename .url }}
{{- end }}
{{- end }}
{{- if $dest_url.RawQuery }}
{{- if $dest_url.Query.Has "classes" }}
{{- $classes := slice | append (split ($dest_url.Query.Get "classes") ",") }}
{{- range $classes }}
{{- $k := . }}
{{- $v := true }}
{{- if strings.HasPrefix $k "no" }}
{{- $k := strings.TrimPrefix "no" $k }}
{{- $v := false }}
{{- end }}
{{- $effects = merge $effects (dict $k $v) }}
{{- end }}
{{- end }}
{{- if $dest_url.Query.Has "featherlight" }}
{{- warnf "%q: DEPRECATED usage of 'featherlight' image CSS class found, use 'lightbox' instead; see https://mcshelby.github.io/hugo-theme-relearn/basics/migration#5110" $page.File.Filename }}
{{- $effects = merge $effects (dict "lightbox" (ne ($dest_url.Query.Get "featherlight") "false")) }}
{{- end }}
{{- range $k, $v := $effects }}
{{- if $dest_url.Query.Has $k }}
{{- $effects = merge $effects (dict $k (ne ($dest_url.Query.Get $k) "false")) }}
{{- end }}
{{- end }}
{{- with $dest_url.Query.Get "height" }}
{{- $height = . }}
{{- end }}
{{- with $dest_url.Query.Get "width" }}
{{- $width = . }}
{{- end }}
{{- end }}
{{- $classes := slice }}
{{- range $k, $v := $effects }}
{{- $c := printf "%s%s" (cond $v "" "no") $k }}
{{- $classes = $classes | append $c }}
{{- end }}
{{- $id := cond (or (eq .id nil) (eq .id "")) (partial "make-random-md5.hugo" $page) .id }}
{{- if $effects.lightbox -}}
<a href="#R-image-{{ $id }}" class="lightbox-link">
{{- end -}}
<img src="{{ $url | safeURL }}" alt="{{ $alt }}"{{ with $title }} title="{{ . }}"{{ end }} class="figure-image {{ delimit $classes " " }}" style="height: {{ $height }}; width: {{ $width }};" loading="lazy">
{{- if $effects.lightbox -}}
</a>
<a href="javascript:history.back();" class="lightbox-back" id="R-image-{{ $id }}"><img src="{{ $url | safeURL }}" alt="{{ $alt }}"{{ with $title }} title="{{ . }}"{{ end }} class="lightbox-image {{ delimit $classes " " }}" loading="lazy"></a>
{{- end }}