diff --git a/hugolib/embedded_shortcodes_test.go b/hugolib/embedded_shortcodes_test.go index 3a6220b532f..27be27051f3 100644 --- a/hugolib/embedded_shortcodes_test.go +++ b/hugolib/embedded_shortcodes_test.go @@ -87,6 +87,15 @@ void do(); {{< /highlight >}}`, `(?s)
}}
+ (message "hello")
+
+ (message "hello again")
+ {{< /highlight >}}`,
+ `(?s)\(message "hello"\)
+
+\(message "hello again"\)
`,
+ },
{`{{< highlight java "style=friendly" >}}
void do();
{{< /highlight >}}`,
diff --git a/tpl/tplimpl/embedded/templates.autogen.go b/tpl/tplimpl/embedded/templates.autogen.go
index ed9ba35ac30..e7e3f9149e1 100644
--- a/tpl/tplimpl/embedded/templates.autogen.go
+++ b/tpl/tplimpl/embedded/templates.autogen.go
@@ -339,7 +339,29 @@ if (!doNotTrack) {
`},
{`shortcodes/gist.html`, ``},
- {`shortcodes/highlight.html`, `{{ if len .Params | eq 2 }}{{ highlight (trim .Inner "\n\r") (.Get 0) (.Get 1) }}{{ else }}{{ highlight (trim .Inner "\n\r") (.Get 0) "" }}{{ end }}`},
+ {`shortcodes/highlight.html`, `{{- $code := (trim .Inner "\n\r") -}}
+{{- $lang := .Get 0 -}}
+{{- $options := .Get 1 | default "" -}}
+{{- $code_ending_in_line_with_spaces := (findRE "[\\r\\n][[:blank:]]+$" $code) -}}
+{{/* Ref: https://github.com/gohugoio/hugo/issues/4717#issuecomment-449375012 */}}
+{{- with $code_ending_in_line_with_spaces -}}
+ {{- $code = (replace $code "\r" "") -}} {{/* Windows -> Unix style line endings for ease in further parsing */}}
+ {{- $offset := (trim (index . 0) "\n") -}}
+ {{- $lines := (split $code "\n") -}}
+ {{- $num_lines := (len $lines) -}}
+ {{- $scratch := newScratch -}}
+ {{- $scratch.Add "lines_minus_offset" (slice) -}}
+ {{- range $i, $line := $lines -}}
+ {{- $line_minus_offset := (strings.TrimPrefix $offset $line) -}}
+ {{- if (lt $i (sub $num_lines 1)) -}} {{/* Do not add the last blank line */}}
+ {{- $scratch.Add "lines_minus_offset" (slice $line_minus_offset) -}}
+ {{- end -}}
+ {{- end -}}
+ {{- $code = (delimit ($scratch.Get "lines_minus_offset") "\n") -}}
+ {{- $scratch.Delete "lines_minus_offset" -}}
+{{- end -}}
+{{- highlight $code $lang $options -}}
+`},
{`shortcodes/instagram.html`, `{{- $pc := .Page.Site.Config.Privacy.Instagram -}}
{{- if not $pc.Disable -}}
{{- if $pc.Simple -}}
diff --git a/tpl/tplimpl/embedded/templates/shortcodes/highlight.html b/tpl/tplimpl/embedded/templates/shortcodes/highlight.html
index b063f92ad04..aa09b48b245 100644
--- a/tpl/tplimpl/embedded/templates/shortcodes/highlight.html
+++ b/tpl/tplimpl/embedded/templates/shortcodes/highlight.html
@@ -1 +1,22 @@
-{{ if len .Params | eq 2 }}{{ highlight (trim .Inner "\n\r") (.Get 0) (.Get 1) }}{{ else }}{{ highlight (trim .Inner "\n\r") (.Get 0) "" }}{{ end }}
\ No newline at end of file
+{{- $code := (trim .Inner "\n\r") -}}
+{{- $lang := .Get 0 -}}
+{{- $options := .Get 1 | default "" -}}
+{{- $code_ending_in_line_with_spaces := (findRE "[\\r\\n][[:blank:]]+$" $code) -}}
+{{/* Ref: https://github.com/gohugoio/hugo/issues/4717#issuecomment-449375012 */}}
+{{- with $code_ending_in_line_with_spaces -}}
+ {{- $code = (replace $code "\r" "") -}} {{/* Windows -> Unix style line endings for ease in further parsing */}}
+ {{- $offset := (trim (index . 0) "\n") -}}
+ {{- $lines := (split $code "\n") -}}
+ {{- $num_lines := (len $lines) -}}
+ {{- $scratch := newScratch -}}
+ {{- $scratch.Add "lines_minus_offset" (slice) -}}
+ {{- range $i, $line := $lines -}}
+ {{- $line_minus_offset := (strings.TrimPrefix $offset $line) -}}
+ {{- if (lt $i (sub $num_lines 1)) -}} {{/* Do not add the last blank line */}}
+ {{- $scratch.Add "lines_minus_offset" (slice $line_minus_offset) -}}
+ {{- end -}}
+ {{- end -}}
+ {{- $code = (delimit ($scratch.Get "lines_minus_offset") "\n") -}}
+ {{- $scratch.Delete "lines_minus_offset" -}}
+{{- end -}}
+{{- highlight $code $lang $options -}}