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