From 9741756906ced27e146a93381e885342d6adca5b Mon Sep 17 00:00:00 2001 From: Joe Mooring Date: Fri, 26 Sep 2025 12:13:16 -0700 Subject: [PATCH] content: Optionally disable whitespace trimming in fenced code blocks --- content/en/contribute/documentation.md | 12 +++++++++ content/en/templates/introduction.md | 2 +- layouts/_markup/render-codeblock.html | 34 ++++++++++++++++++-------- 3 files changed, 37 insertions(+), 11 deletions(-) diff --git a/content/en/contribute/documentation.md b/content/en/contribute/documentation.md index 91d345b60..33f1e8a89 100644 --- a/content/en/contribute/documentation.md +++ b/content/en/contribute/documentation.md @@ -268,6 +268,18 @@ To wrap the code block within an initially-opened `details` element using a non- ``` ```` +Whitespace trimming is enabled by default. To override this behavior and preserve leading and trailing spaces: + +````text +```go-html-template {trim=false} + +{{ if eq $foo "bar" }} + {{ print "foo is bar" }} +{{ end }} + +``` +```` + ### Shortcode calls Use this syntax : diff --git a/content/en/templates/introduction.md b/content/en/templates/introduction.md index 9d94032c9..26928420d 100644 --- a/content/en/templates/introduction.md +++ b/content/en/templates/introduction.md @@ -110,7 +110,7 @@ In the example above: Hugo renders the above to: -```html +```html {trim=false}

my page title

diff --git a/layouts/_markup/render-codeblock.html b/layouts/_markup/render-codeblock.html index 13725ffcd..0f1d09d1f 100644 --- a/layouts/_markup/render-codeblock.html +++ b/layouts/_markup/render-codeblock.html @@ -10,6 +10,7 @@ may also specify the following parameters: @param {bool} [details=false] Whether to wrap the highlighted code block within a details element. @param {bool} [open=false] Whether to initially display the content of the details element. @param {string} [summary=Details] The content of the details summary element rendered from Markdown to HTML. +@param {bool} [trim=true] Whether to trim leading and trailing whitespace from the content of the code block. @returns {template.HTML} @@ -30,6 +31,7 @@ may also specify the following parameters: {{- $details := false }} {{- $open := "" }} {{- $summary := or .Attributes.summary "Details" | .Page.RenderString }} +{{- $trim := true }} {{- $ext := strings.TrimPrefix "." (path.Ext $file) }} {{- $lang := or .Type $ext "text" }} {{- if in (slice "html" "gotmpl") $lang }} @@ -39,30 +41,38 @@ may also specify the following parameters: {{- $lang = "text" }} {{- end }} -{{- with .Attributes.copy }} - {{- if in (slice true "true" 1) . }} +{{- if collections.IsSet .Attributes "copy" }} + {{- if in (slice true "true" 1) .Attributes.copy }} {{- $copy = true }} - {{- else if in (slice false "false" 0) . }} + {{- else if in (slice false "false" 0) .Attributes.copy }} {{- $copy = false }} {{- end }} {{- end }} -{{- with .Attributes.details }} - {{- if in (slice true "true" 1) . }} +{{- if collections.IsSet .Attributes "details" }} + {{- if in (slice true "true" 1) .Attributes.details }} {{- $details = true }} - {{- else if in (slice false "false" 0) . }} + {{- else if in (slice false "false" 0) .Attributes.details }} {{- $details = false }} {{- end }} {{- end }} -{{- with .Attributes.open }} - {{- if in (slice true "true" 1) . }} +{{- if collections.IsSet .Attributes "open" }} + {{- if in (slice true "true" 1) .Attributes.open }} {{- $open = "open" }} - {{- else if in (slice false "false" 0) . }} + {{- else if in (slice false "false" 0) .Attributes.open }} {{- $open = "" }} {{- end }} {{- end }} +{{- if collections.IsSet .Attributes "trim" }} + {{- if in (slice true "true" 1) .Attributes.trim }} + {{- $trim = true }} + {{- else if in (slice false "false" 0) .Attributes.trim }} + {{- $trim = false }} + {{- end }} +{{- end }} + {{- if $details }}
{{ $summary }} @@ -89,7 +99,11 @@ may also specify the following parameters: {{- end }}
- {{- transform.Highlight (strings.TrimSpace .Inner) $lang .Options }} + {{- $content := .Inner }} + {{- if $trim }} + {{- $content = strings.TrimSpace .Inner }} + {{- end }} + {{- transform.Highlight $content $lang .Options }}