From 9536c9fb25e8ef197a5e0664432562dbc003d679 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Wed, 25 Dec 2024 19:02:44 -0800 Subject: [PATCH] Allow stripping leading whitespace from code blocks Signed-off-by: Danila Fedorin --- layouts/shortcodes/codelines.html | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/layouts/shortcodes/codelines.html b/layouts/shortcodes/codelines.html index e0d805c..71266ab 100644 --- a/layouts/shortcodes/codelines.html +++ b/layouts/shortcodes/codelines.html @@ -18,6 +18,24 @@ {{- $scratch.Set "hidden" (.Get 5) -}} {{- end -}} +{{- if or (.Page.Params.left_align_code) (.Get 6) -}} +{{- $scratch.Set "prefixLength" -1 -}} +{{- range $line := $lines -}} +{{- $leading := sub (len $line) (len (strings.TrimLeft " " $line)) -}} +{{- if and (ne $line "") (or (eq ($scratch.Get "prefixLength") -1) (le $leading ($scratch.Get "prefixLength"))) -}} +{{- $scratch.Set "prefixLength" $leading -}} +{{- end -}} +{{- end -}} + +{{- $scratch.Set "joinedLines" "" -}} +{{- range $line := $lines -}} +{{- $scratch.Add "joinedLines" (substr $line ($scratch.Get "prefixLength")) -}} +{{- $scratch.Add "joinedLines" "\n" -}} +{{- end -}} +{{- else -}} +{{- $scratch.Set "joinedLines" (delimit $lines "\n") -}} +{{- end -}} + {{- partial "geturl.html" (dict "scratch" $scratch "path" (.Get 1) "lines" (slice (.Get 2) (.Get 3))) -}} {{- partial "linerangestr.html" (dict "scratch" $scratch "from" (.Get 2) "to" (.Get 3)) -}} @@ -29,7 +47,7 @@ "basePath" ($scratch.Get "bestPath") "path" (.Get 1) "comment" ($scratch.Get "comment") - "code" (delimit $lines "\n") + "code" ($scratch.Get "joinedLines") "firstLine" (int (.Get 2)) "lastLine" (int (.Get 3)) "language" (.Get 0)