From 34c2725fe1c59940d775054f374d7cb6836bf80f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20D=C3=A9ramond?= Date: Tue, 28 Mar 2023 16:01:47 +0200 Subject: [PATCH] Docs: `js-docs` unindent automatically the code inside shortcode (#38349) --- site/layouts/shortcodes/js-docs.html | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/site/layouts/shortcodes/js-docs.html b/site/layouts/shortcodes/js-docs.html index 1a479db678..fad4f034fc 100644 --- a/site/layouts/shortcodes/js-docs.html +++ b/site/layouts/shortcodes/js-docs.html @@ -32,6 +32,27 @@ - {{- highlight $match "js" "" -}} + {{- $unindent := 0 -}} + {{- $found := false -}} + {{- $first_line:= index (split $match "\n") 0 -}} + {{- range $char := split $first_line "" -}} + {{- if and (eq $char " ") (not $found) -}} + {{- $unindent = add $unindent 1 -}} + {{- else -}} + {{- $found = true -}} + {{- end -}} + {{- end -}} + {{- $output := "" -}} + {{- if (gt $unindent 0) -}} + {{- $prefix := (strings.Repeat $unindent " ") -}} + {{- range $line := split $match "\n" -}} + {{- $line = strings.TrimPrefix $prefix $line -}} + {{ $output = printf "%s%s\n" $output $line }} + {{- end -}} + {{- $output = chomp $output -}} + {{- else -}} + {{- $output = $match -}} + {{- end -}} + {{- highlight $output "js" "" -}} {{- end -}}