lightbox: generate unique lightbox ids #451

This commit is contained in:
Sören Weber 2023-02-01 23:16:42 +01:00
parent f89c9944bd
commit 1383b7e6af
No known key found for this signature in database
GPG key ID: BEC6D55545451B6D
3 changed files with 11 additions and 5 deletions

View file

@ -3,5 +3,4 @@
"url" .Destination
"title" .Title
"alt" .Text
"ordinal" 0
) }}

View file

@ -0,0 +1,8 @@
{{- $random := "" }}
{{- $set := split "012345abcdefghijklmnopqrstuvwxyz" "" }}
{{- range (seq 16) }}
{{- with ($set | shuffle | first 1) }}
{{- $random = printf "%s%s" $random . }}
{{- end }}
{{- end }}
{{- md5 $random }}

View file

@ -2,7 +2,6 @@
{{- $url := .url }}
{{- $title := .title }}
{{- $alt := .alt }}
{{- $ordinal := .ordinal }}
{{- $classes := slice }}
{{- $lightbox := true }}
{{- $height := "auto" }}
@ -34,14 +33,14 @@
{{ $width = . }}
{{- end }}
{{- end }}
{{- $fragment := printf "%d-%s" $ordinal ((trim (replace (replace (replace (replace (replace $url "=" "-") "&" "-") "?" "-") "." "-") "/" "-") "-") | safeURL | anchorize) }}
{{- $id := partial "make-random-md5.hugo" }}
{{- if $lightbox }}
<a href="#{{ $fragment }}">
<a href="#{{ $id }}">
{{- end }}
<img src="{{ $url | safeURL }}" alt="{{ $alt }}"{{ with $title }} title="{{ . }}"{{ end }}{{ if len ($classes) }} class="{{ delimit $classes " " }}"{{ end }} style="height: {{ $height }}; width: {{ $width }};" loading="lazy">
{{- if $lightbox }}
</a>
<a href="javascript:history.back();" class="lightbox" id="{{ $fragment }}">
<a href="javascript:history.back();" class="lightbox" id="{{ $id }}">
<img src="{{ $url | safeURL }}" alt="{{ $alt }}"{{ with $title }} title="{{ . }}"{{ end }}loading="lazy">
</a>
{{- end }}