Hugo generates empty paragraphs at beginning of or between shortcodes

I have just upgraded the version of Hugo that I use to build my website from 0.124.1 to 0.138.0, and now there are empty paragraphs (<p></p>) generated when I use one of my shortcodes.

The shortcode is basically the same thing as the include shortcode used as an example in the .RenderShortcodes documentation. And it is used like in the docs:

Some paragraph…

{{% include "/snippets/services" %}}
{{% include "/snippets/values" %}}
{{% include "/snippets/leadership" %}}

The snippets have a format like this:

### Title

Content of the snippet…

And the page generated by Hugo always includes an empty paragraph above the title of the snippets.

The only way to remove it is to remove the empty line above the first include and put all the shortcodes on the same line, but that just makes the source more confusing and it doesn’t seem like it should be necessary.

After testing several versions, it seems that this change in rendering occurs since Hugo 0.125.0.

Here is a project I created as a minimal reproducer: GitHub - zecakeh/hugo-empty-paragraphs. The relevant part of the generated “About” page is:

<h3 id="the-team">The team</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer in lorem ut ligula blandit maximus. Morbi mattis sagittis libero quis posuere. Donec viverra arcu ac mattis iaculis. Aliquam erat volutpat. In molestie gravida massa sit amet pulvinar. Aliquam vel mi vitae felis rutrum vestibulum ut et nunc. Nunc eget nisi sit amet lorem viverra vehicula. Phasellus in pharetra libero. Ut in ante sed arcu ullamcorper ullamcorper sed et tellus. Nulla pulvinar nisl diam, nec facilisis dui tempor in. Praesent finibus faucibus condimentum. Nam ac ante mollis justo eleifend cursus at in lorem.</p>
<p>Vestibulum facilisis, nisi aliquam sodales placerat, risus erat rutrum lectus, sit amet consequat felis lectus in mauris. Vestibulum fermentum bibendum hendrerit. Pellentesque at iaculis justo. Suspendisse eleifend condimentum neque cursus elementum. Phasellus malesuada semper luctus. Morbi ut justo vel dui suscipit euismod. Mauris lobortis id elit quis interdum. Sed aliquam elit non dolor dapibus, tristique tincidunt turpis faucibus. Proin venenatis sagittis molestie. Aliquam molestie, elit ut commodo dignissim, sem leo placerat nunc, nec tempus elit justo sit amet augue. Nulla diam lorem, ornare sed consectetur sed, fringilla et quam. Suspendisse gravida vitae libero quis pellentesque. Etiam molestie, lectus vitae varius interdum, odio lorem rhoncus nulla, sit amet vestibulum urna nunc in odio.</p>
<p></p>
<h3 id="services">Services</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer in lorem ut ligula blandit maximus. Morbi mattis sagittis libero quis posuere. Donec viverra arcu ac mattis iaculis. Aliquam erat volutpat. In molestie gravida massa sit amet pulvinar. Aliquam vel mi vitae felis rutrum vestibulum ut et nunc. Nunc eget nisi sit amet lorem viverra vehicula. Phasellus in pharetra libero. Ut in ante sed arcu ullamcorper ullamcorper sed et tellus. Nulla pulvinar nisl diam, nec facilisis dui tempor in. Praesent finibus faucibus condimentum. Nam ac ante mollis justo eleifend cursus at in lorem.</p>
<p>Vestibulum facilisis, nisi aliquam sodales placerat, risus erat rutrum lectus, sit amet consequat felis lectus in mauris. Vestibulum fermentum bibendum hendrerit. Pellentesque at iaculis justo. Suspendisse eleifend condimentum neque cursus elementum. Phasellus malesuada semper luctus. Morbi ut justo vel dui suscipit euismod. Mauris lobortis id elit quis interdum. Sed aliquam elit non dolor dapibus, tristique tincidunt turpis faucibus. Proin venenatis sagittis molestie. Aliquam molestie, elit ut commodo dignissim, sem leo placerat nunc, nec tempus elit justo sit amet augue. Nulla diam lorem, ornare sed consectetur sed, fringilla et quam. Suspendisse gravida vitae libero quis pellentesque. Etiam molestie, lectus vitae varius interdum, odio lorem rhoncus nulla, sit amet vestibulum urna nunc in odio.
</p>
<p></p>
<h3 id="values">Values</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer in lorem ut ligula blandit maximus. Morbi mattis sagittis libero quis posuere. Donec viverra arcu ac mattis iaculis. Aliquam erat volutpat. In molestie gravida massa sit amet pulvinar. Aliquam vel mi vitae felis rutrum vestibulum ut et nunc. Nunc eget nisi sit amet lorem viverra vehicula. Phasellus in pharetra libero. Ut in ante sed arcu ullamcorper ullamcorper sed et tellus. Nulla pulvinar nisl diam, nec facilisis dui tempor in. Praesent finibus faucibus condimentum. Nam ac ante mollis justo eleifend cursus at in lorem.</p>
<p>Vestibulum facilisis, nisi aliquam sodales placerat, risus erat rutrum lectus, sit amet consequat felis lectus in mauris. Vestibulum fermentum bibendum hendrerit. Pellentesque at iaculis justo. Suspendisse eleifend condimentum neque cursus elementum. Phasellus malesuada semper luctus. Morbi ut justo vel dui suscipit euismod. Mauris lobortis id elit quis interdum. Sed aliquam elit non dolor dapibus, tristique tincidunt turpis faucibus. Proin venenatis sagittis molestie. Aliquam molestie, elit ut commodo dignissim, sem leo placerat nunc, nec tempus elit justo sit amet augue. Nulla diam lorem, ornare sed consectetur sed, fringilla et quam. Suspendisse gravida vitae libero quis pellentesque. Etiam molestie, lectus vitae varius interdum, odio lorem rhoncus nulla, sit amet vestibulum urna nunc in odio.
</p>
<p></p>
<h3 id="leadership">Leadership</h3>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer in lorem ut ligula blandit maximus. Morbi mattis sagittis libero quis posuere. Donec viverra arcu ac mattis iaculis. Aliquam erat volutpat. In molestie gravida massa sit amet pulvinar. Aliquam vel mi vitae felis rutrum vestibulum ut et nunc. Nunc eget nisi sit amet lorem viverra vehicula. Phasellus in pharetra libero. Ut in ante sed arcu ullamcorper ullamcorper sed et tellus. Nulla pulvinar nisl diam, nec facilisis dui tempor in. Praesent finibus faucibus condimentum. Nam ac ante mollis justo eleifend cursus at in lorem.</p>
<p>Vestibulum facilisis, nisi aliquam sodales placerat, risus erat rutrum lectus, sit amet consequat felis lectus in mauris. Vestibulum fermentum bibendum hendrerit. Pellentesque at iaculis justo. Suspendisse eleifend condimentum neque cursus elementum. Phasellus malesuada semper luctus. Morbi ut justo vel dui suscipit euismod. Mauris lobortis id elit quis interdum. Sed aliquam elit non dolor dapibus, tristique tincidunt turpis faucibus. Proin venenatis sagittis molestie. Aliquam molestie, elit ut commodo dignissim, sem leo placerat nunc, nec tempus elit justo sit amet augue. Nulla diam lorem, ornare sed consectetur sed, fringilla et quam. Suspendisse gravida vitae libero quis pellentesque. Etiam molestie, lectus vitae varius interdum, odio lorem rhoncus nulla, sit amet vestibulum urna nunc in odio.
</p>

This sounds like Release v0.137.0 · gohugoio/hugo · GitHub

RenderShortcodes used inside Markdown just does not work if the output does not end with a newline, so we fixed it by checking for one and add one if it’s not there.

Note that it’s very common for editors to add a newline to files (e.g. Markdown files to be included), which I guess was the reason why this issue didn’t surface earlier.

You may try something ala

{{% include "/snippets/services" %}}{{% include "/snippets/values" %}}{{% include "/snippets/leadership" %}}

We started to insert empty paragraphs in v0.125.0.

The test below passes with v0.124.1, and fails with v0.125.0 and later.

integration test
func TestFoo(t *testing.T) {
	t.Parallel()

	files := `
-- hugo.toml --
disableKinds = ['section','rss','sitemap','taxonomy','term']
-- layouts/_default/home.html --
{{ .Content }}
-- layouts/_default/single.html --
{{ .Content }}
-- layouts/shortcodes/include.html --
 {{ with site.GetPage (.Get 0) }}
  {{ .RenderShortcodes }}
{{ end }}
-- content/_index.md --
---
title: home
---

a

{{% include "/snippet" %}}

b

-- content/snippet.md --
---
title: snippet
build:
  render: never
  list: never
---

_emphasized_

`

	b := hugolib.Test(t, files)
	b.AssertFileContentEquals("public/index.html",
		"<p>a</p>\n<p><em>emphasized</em></p>\n<p>b</p>\n",
	)
}

got:

"<p>a</p>\n<p></p>\n<p><em>emphasized</em>\n</p>\n<p>b</p>\n"
           -------                        --

want:

"<p>a</p>\n<p><em>emphasized</em></p>\n<p>b</p>\n"

See:

OK, yea, well, then it sounds fixable.

I can confirm that the merged pull request fixes the problem for us. Thanks!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.