From 899cab1cf6d888356c3cad8ba641c1ce026adcfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Weber?= Date: Wed, 17 May 2023 22:55:34 +0200 Subject: [PATCH] theme: make IDs stable for testing --- exampleSite/test.bat | 1 + layouts/partials/make-random-md5.hugo | 19 ++++++++++++------- layouts/partials/shortcodes/expand.html | 2 +- layouts/partials/shortcodes/image.html | 2 +- layouts/partials/shortcodes/openapi.html | 2 +- 5 files changed, 16 insertions(+), 10 deletions(-) diff --git a/exampleSite/test.bat b/exampleSite/test.bat index 08473ffd28..ce4200d998 100644 --- a/exampleSite/test.bat +++ b/exampleSite/test.bat @@ -3,6 +3,7 @@ set /p version=<..\layouts\partials\version.txt echo %version%>metrics.%version%.txt echo disableAssetsBusting=true>>config.toml echo disableGeneratorVersion=true>>config.toml +echo disableRandomIds=true>>config.toml hugo --templateMetrics --templateMetricsHints --cleanDestinationDir --destination public.%version% >> metrics.%version%.txt move /Y metrics.%version%.txt public.%version%\metrics.txt 2>&1 >NUL git restore config.toml diff --git a/layouts/partials/make-random-md5.hugo b/layouts/partials/make-random-md5.hugo index d355dde35d..e6dba739df 100644 --- a/layouts/partials/make-random-md5.hugo +++ b/layouts/partials/make-random-md5.hugo @@ -1,8 +1,13 @@ -{{- $random := "" }} -{{- $set := split "012345abcdefghijklmnopqrstuvwxyz" "" }} -{{- range (seq 16) }} - {{- with ($set | shuffle | first 1) }} - {{- $random = printf "%s%s" $random . }} +{{- $context := . }} +{{- if (ne $context.Site.Params.disableRandomIds true) }} + {{- $random := "" }} + {{- $set := split "012345abcdefghijklmnopqrstuvwxyz" "" }} + {{- range (seq 16) }} + {{- with ($set | shuffle | first 1) }} + {{- $random = printf "%s%s" $random . }} + {{- end }} {{- end }} -{{- end }} -{{- md5 $random }} \ No newline at end of file + {{- md5 $random }} +{{- else }} + {{- printf "00000000000000000000000000000000" }} +{{- end }} \ No newline at end of file diff --git a/layouts/partials/shortcodes/expand.html b/layouts/partials/shortcodes/expand.html index 3998920d56..e201afec15 100644 --- a/layouts/partials/shortcodes/expand.html +++ b/layouts/partials/shortcodes/expand.html @@ -6,7 +6,7 @@ {{- if eq (printf "%T" $expanded) "string" }} {{- $expanded = (eq $expanded "true") }} {{- end }} -{{- $id := cond (or (eq .id nil) (eq .id "")) (partial "make-random-md5.hugo") .id }} +{{- $id := cond (or (eq .id nil) (eq .id "")) (partial "make-random-md5.hugo" $context.Page) .id }} {{- with $context }}
diff --git a/layouts/partials/shortcodes/image.html b/layouts/partials/shortcodes/image.html index 5d28e57b35..7d8c486d63 100644 --- a/layouts/partials/shortcodes/image.html +++ b/layouts/partials/shortcodes/image.html @@ -33,7 +33,7 @@ {{ $width = . }} {{- end }} {{- end }} -{{- $id := cond (or (eq .id nil) (eq .id "")) (partial "make-random-md5.hugo") .id }} +{{- $id := cond (or (eq .id nil) (eq .id "")) (partial "make-random-md5.hugo" $context) .id }} {{- if $lightbox }} {{- end }} diff --git a/layouts/partials/shortcodes/openapi.html b/layouts/partials/shortcodes/openapi.html index 1702e64cab..bbdf375fb7 100644 --- a/layouts/partials/shortcodes/openapi.html +++ b/layouts/partials/shortcodes/openapi.html @@ -1,6 +1,6 @@ {{- $context := .context }} {{- $src := .src }} -{{- $id := cond (or (eq .id nil) (eq .id "")) (partial "make-random-md5.hugo") .id }} +{{- $id := cond (or (eq .id nil) (eq .id "")) (partial "make-random-md5.hugo" $context.Page) .id }} {{- with $context }} {{- with .Page.Resources.Match $src }} {{- range . }}