image: adjust to Hugo's build-in code #857

- resolve render hook destination with leading ./
- adjust variable naming
This commit is contained in:
Sören Weber 2024-05-28 21:48:37 +02:00
parent cfd65dad08
commit 833ccac2d2
No known key found for this signature in database
GPG key ID: BEC6D55545451B6D

View file

@ -4,7 +4,6 @@
{{- $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 "lazy" true "lightbox" true "shadow" false }}
@ -17,23 +16,20 @@
{{- $height := "auto" }}
{{- $width := "auto" }}
{{- $attributes := .attributes | default dict }}
{{- $dest_url := urls.Parse $url }}
{{- $dest_path := $dest_url.Path }}
{{- $image := "" }}
{{- if not $dest_url.IsAbs }}
{{- $u := urls.Parse .url }}
{{- $src := $u.String }}
{{- if not $u.IsAbs }}
{{- $path := strings.TrimPrefix "./" $u.Path }}
{{- with or
($page.Resources.Get $dest_url.Path)
(resources.Get $dest_url.Path)
($page.Resources.Get $path)
(resources.Get $path)
}}
{{- $image = . }}
{{- end }}
{{- with $image }}
{{- $url = $image.RelPermalink }}
{{- with $dest_url.RawQuery }}
{{- $url = printf "%s?%s" $url . }}
{{- $src = .RelPermalink }}
{{- with $u.RawQuery }}
{{- $src = printf "%s?%s" $src . }}
{{- end }}
{{- with $dest_url.Fragment }}
{{- $url = printf "%s#%s" $url . }}
{{- with $u.Fragment }}
{{- $src = printf "%s#%s" $src . }}
{{- end }}
{{- else }}
{{- if eq $page.Site.Params.image.errorlevel "warning" }}
@ -43,9 +39,9 @@
{{- end }}
{{- end }}
{{- end }}
{{- if $dest_url.RawQuery }}
{{- if $dest_url.Query.Has "classes" }}
{{- $classes := slice | append (split ($dest_url.Query.Get "classes") ",") }}
{{- if $u.RawQuery }}
{{- if $u.Query.Has "classes" }}
{{- $classes := slice | append (split ($u.Query.Get "classes") ",") }}
{{- range $classes }}
{{- $k := . }}
{{- $v := true }}
@ -56,19 +52,19 @@
{{- $effects = merge $effects (dict $k $v) }}
{{- end }}
{{- end }}
{{- if $dest_url.Query.Has "featherlight" }}
{{- if $u.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")) }}
{{- $effects = merge $effects (dict "lightbox" (ne ($u.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")) }}
{{- if $u.Query.Has $k }}
{{- $effects = merge $effects (dict $k (ne ($u.Query.Get $k) "false")) }}
{{- end }}
{{- end }}
{{- with $dest_url.Query.Get "height" }}
{{- with $u.Query.Get "height" }}
{{- $height = . }}
{{- end }}
{{- with $dest_url.Query.Get "width" }}
{{- with $u.Query.Get "width" }}
{{- $width = . }}
{{- end }}
{{- end }}
@ -78,7 +74,7 @@
{{- $classes = $classes | append $c }}
{{- end }}
{{- $id := cond (or (eq .id nil) (eq .id "")) (partial "make-random-md5.hugo" $page) .id }}
{{- $attributes = merge $attributes (dict "alt" $alt "src" $url "title" ($title | transform.HTMLEscape)) }}
{{- $attributes = merge $attributes (dict "alt" $alt "src" $src "title" ($title | transform.HTMLEscape)) }}
{{- if $effects.lazy }}
{{- $attributes = merge $attributes (dict "loading" "lazy") }}
{{- end }}