hugo: use URL parser for image ref in render hook

Signed-off-by: David Karlsson <35727626+dvdksn@users.noreply.github.com>
This commit is contained in:
David Karlsson 2024-07-08 16:02:44 +02:00
parent 51e5271a23
commit 7f778f78ea
1 changed files with 13 additions and 18 deletions

View File

@ -1,13 +1,14 @@
{{ $imagePath := .Destination }} {{- $u := urls.Parse .Destination -}}
{{ if and (eq .Page.Kind "page") (not (hasPrefix .Destination "/")) }} {{- $src := $u.String -}}
{{ $imagePath = (printf "../%s" .Destination) }} {{ if not (or $u.IsAbs (strings.HasPrefix $u.String "/")) }}
{{ $base := urls.Parse .Page.Permalink }}
{{ $src = $base.JoinPath "../" $u.Path -}}
{{ end }} {{ end }}
{{ $params := (urls.Parse $imagePath).Query }} {{ $params := $u.Query }}
{{ $width := index $params "w" }} {{ $width := $params.Get "w" }}
{{ $height := index $params "h" }} {{ $height := $params.Get "h" }}
{{ $border := index $params "border" }} {{ $border := $params.Has "border" }}
<figure <figure
x-data="{ zoom: false }" x-data="{ zoom: false }"
@ -16,15 +17,10 @@
> >
<img <img
loading="lazy" loading="lazy"
src="{{ $imagePath }}" src="{{ $src }}"
alt="{{ .Text }}" alt="{{ .Text }}"
{{ with $width }} {{ with $width }} width="{{ . }}" {{ end }}
width="{{ index . 0 }}" {{ with $height }} height="{{ . }}" {{ end }}
{{ end }}
{{ with $height }}
height="{{ index . 0 }}"
{{ end }}
{{ with .Title }}title="{{ . }}"{{ end }}
class="rounded mx-auto{{ with $border }} border border-divider-light dark:border-divider-dark{{end}}" class="rounded mx-auto{{ with $border }} border border-divider-light dark:border-divider-dark{{end}}"
/> />
{{ with .Title }} {{ with .Title }}
@ -45,9 +41,8 @@
<img <img
loading="lazy" loading="lazy"
class="rounded max-w-full max-h-full" class="rounded max-w-full max-h-full"
src="{{ $imagePath }}" src="{{ $src }}"
alt="{{ .Text }}" alt="{{ .Text }}"
{{ with .Title }}title="{{ . }}"{{ end }}
/> />
</div> </div>
</template> </template>