From 7de728fdfec8f28bb5745454677ee7b4c904ee6d Mon Sep 17 00:00:00 2001 From: Rohan Kumar Date: Wed, 29 Nov 2023 00:36:14 -0800 Subject: [PATCH] Avoid data races with codeIndex on feeds --- layouts/_default/list.atom.xml | 1 - layouts/posts/single.html | 2 ++ layouts/shortcodes/codecaption.atom | 17 +++++++++++++++++ layouts/shortcodes/codecaption.rss | 17 +++++++++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 layouts/shortcodes/codecaption.atom create mode 100644 layouts/shortcodes/codecaption.rss diff --git a/layouts/_default/list.atom.xml b/layouts/_default/list.atom.xml index e0dcbe4..6586a50 100644 --- a/layouts/_default/list.atom.xml +++ b/layouts/_default/list.atom.xml @@ -58,7 +58,6 @@ {{ range $pages -}} {{- if not .Params.Private -}} - {{- .Page.Scratch.Set "codeIndex" 1 -}} {{ .Title }} {{ .Permalink }} diff --git a/layouts/posts/single.html b/layouts/posts/single.html index 26006da..303f120 100644 --- a/layouts/posts/single.html +++ b/layouts/posts/single.html @@ -1,6 +1,8 @@ {{- define "main" -}} {{- $canonicalRelPermalink := .RelPermalink | replaceRE "^/~seirdy/" "/" }} {{- .Scratch.Set "codeIndex" 1 -}} +{{- .Scratch.Set "codeIndexAtom" 1 -}} +{{- .Scratch.Set "codeIndexRss" 1 -}}
diff --git a/layouts/shortcodes/codecaption.atom b/layouts/shortcodes/codecaption.atom new file mode 100644 index 0000000..5dac3c7 --- /dev/null +++ b/layouts/shortcodes/codecaption.atom @@ -0,0 +1,17 @@ +{{- $codeIndex := (.Page.Scratch.Get "codeIndexAtom") -}} +{{- $id := (printf `code-%d` $codeIndex) -}} +{{- with .Get "id" -}} + {{- $id = . -}} +{{- end -}} +{{ with .Parent }} + {{- with .Get "id" -}} + {{- $id = . -}} + {{- end -}} +{{- end -}} +
+ + Code snippet {{ $codeIndex }}{{with .Get "lang"}} ({{ . }}){{ end -}} + : +{{ .Inner | .Page.RenderString | safeHTML }} +
+{{- .Page.Scratch.Set "codeIndexAtom" (add 1 $codeIndex) -}} diff --git a/layouts/shortcodes/codecaption.rss b/layouts/shortcodes/codecaption.rss new file mode 100644 index 0000000..ad31422 --- /dev/null +++ b/layouts/shortcodes/codecaption.rss @@ -0,0 +1,17 @@ +{{- $codeIndex := (.Page.Scratch.Get "codeIndexRss") -}} +{{- $id := (printf `code-%d` $codeIndex) -}} +{{- with .Get "id" -}} + {{- $id = . -}} +{{- end -}} +{{ with .Parent }} + {{- with .Get "id" -}} + {{- $id = . -}} + {{- end -}} +{{- end -}} +
+ + Code snippet {{ $codeIndex }}{{with .Get "lang"}} ({{ . }}){{ end -}} + : +{{ .Inner | .Page.RenderString | safeHTML }} +
+{{- .Page.Scratch.Set "codeIndexRss" (add 1 $codeIndex) -}}