layouts: map: Make unique ID unique on page, not only shortcut parent

The SHORTCODE.Ordinal method "returns the zero-based ordinal of the shortcode in
relation to its parent." (https://gohugo.io/methods/shortcode/ordinal/)

This is a problem if the same shortcode is used on the different page through
different paths, e.g. once on the page itself and once by a partial (e.g. a
header or footer).

Instead of just using the .Ordinal by itself, use a hash of the parent file
_and_ the ordinal, which gives us a properly unique identifier.
This commit is contained in:
Martin Rubli
2023-12-18 23:11:59 +01:00
parent 0384314398
commit f3746e0fc4
6 changed files with 6 additions and 6 deletions

View File

@@ -2,7 +2,7 @@
{{ errorf "%s: cannot be used outside leaflet-map: %s" .Name .Position }}
{{ end }}
{{ $uniqueMapId := .Parent.Ordinal | safeJS }}
{{ $uniqueMapId := (printf "%s:%d" .Page.File.UniqueID .Parent.Ordinal) | md5 | safeJS }}
<script>
map_options_{{ $uniqueMapId }}.heightgraph = {

View File

@@ -6,7 +6,7 @@
{{ errorf "%s: missing 'id' parameter: %s" .Name .Position }}
{{ end }}
{{ $uniqueMapId := .Parent.Ordinal | safeJS }}
{{ $uniqueMapId := (printf "%s:%d" .Page.File.UniqueID .Parent.Ordinal) | md5 | safeJS }}
{{ $id := .Get "id" }}
<script>

View File

@@ -119,7 +119,7 @@ Elevation profile options:
*/}}
{{ $uniqueId := .Ordinal | safeJS }}
{{ $uniqueId := (printf "%s:%d" .Page.File.UniqueID .Ordinal) | md5 | safeJS }}
<div id="map_container_{{ $uniqueId }}" class="map-container" style="width: {{ .Get "width" | default "auto" }}; height: {{ .Get "height" | default "50vh" }};">
<div id="map_{{ $uniqueId }}" class="map"></div>

View File

@@ -9,7 +9,7 @@
{{ errorf "%s: missing 'lon' parameter: %s" .Name .Position }}
{{ end }}
{{ $uniqueMapId := .Parent.Ordinal | safeJS }}
{{ $uniqueMapId := (printf "%s:%d" .Page.File.UniqueID .Parent.Ordinal) | md5 | safeJS }}
<script>
map_options_{{ $uniqueMapId }}.markers.push({ lat: {{ .Get "lat" }}, lon: {{ .Get "lon" }} })

View File

@@ -2,7 +2,7 @@
{{ errorf "%s: cannot be used outside leaflet-map: %s" .Name .Position }}
{{ end }}
{{ $uniqueMapId := .Parent.Ordinal | safeJS }}
{{ $uniqueMapId := (printf "%s:%d" .Page.File.UniqueID .Parent.Ordinal) | md5 | safeJS }}
<script>
map_options_{{ $uniqueMapId }}.scale = {

View File

@@ -6,7 +6,7 @@
{{ errorf "%s: missing 'path' parameter: %s" .Name .Position }}
{{ end }}
{{ $uniqueMapId := .Parent.Ordinal | safeJS }}
{{ $uniqueMapId := (printf "%s:%d" .Page.File.UniqueID .Parent.Ordinal) | md5 | safeJS }}
<script>
map_options_{{ $uniqueMapId }}.tracks.push({{ .Get "path" }})