Weird stray </p> (closing paragraph) tags

Yes, that part of it is odd - exactly what I was saying about images if you use a render-hook and new feature not to wrap images in a paragraph.

I put shortcodes on lines separated by blank lines, it is more readable that way anyway.

As far as I know it is a markdown thing, I have come across it in another markdown based CMS.

I don’t know how to make this any clearer. Please see this example which uses the reference implementation of the CommonMark specification.

When shortcodes are called with the {{<>}} notation, the markdown parser receives placeholders—just a string, as shown in the example above.

Can we please close this topic?

Clicking the HTML tab in that utility shows that your example renders to:

<p>shortcode-1
shortcode-2
shortcode-3</p>
<!-------->
<p>shortcode-1</p>
<p>shortcode-2</p>
<p>shortcode-3</p>

But when you try those two variations with actual shortcodes and in Hugo, that’s not what you get. You get:

  <p>shortcode-1

shortcode-2

shortcode-3
</p>
<!-- raw HTML omitted -->
shortcode-1

shortcode-2

shortcode-3

And here’s a repo to reproduce it: GitHub - lpar/hugo-forum-topic-14929b

A typical shortcode looks like…

<div>shortcode-1</div>

or

<p>shortcode-2</p>

or

<img src="shortcode-3">

Shortcodes in markdown without blank lines

Markdown

{{< shortcode-1 >}}
{{< shortcode-2 >}}
{{< shortcode-3 >}}
{{< shortcode-3 >}}
{{< shortcode-3 >}}

Rendered (invalid HTML as expected)

<p>
  <div>shortcode-1</div>
  <p>shortcode-2</p>
  <img src="shortcode-3">
  <img src="shortcode-3">
  <img src="shortcode-3">
</p>

Shortcodes in markdown with blank lines

Markdown

{{< shortcode-1 >}}

{{< shortcode-2 >}}

{{< shortcode-3 >}}

{{< shortcode-3 >}}

{{< shortcode-3 >}}

Rendered (valid HTML)

<div>shortcode-1</div>
<p>shortcode-2</p>
<img src="shortcode-3">
<img src="shortcode-3">
<img src="shortcode-3">

If you want the shortcodes fully rendered before sending them to the markdown parser, use the {{%%}} notation, and do this in site configuration:

[markup.goldmark.renderer]
unsafe = true

It’s not unsafe if you control the content.

Shortcodes in markdown without blank lines Markdown

Markdown

{{% shortcode-1 %}}
{{% shortcode-2 %}}
{{% shortcode-3 %}}
{{% shortcode-3 %}}
{{% shortcode-3 %}}

Rendered (valid HTML)

<div>shortcode-1</div>
<p>shortcode-2</p>
<img src="shortcode-3">
<img src="shortcode-3">
<img src="shortcode-3">

Shortcodes in markdown with blank lines Markdown

Markdown

{{% shortcode-1 %}}

{{% shortcode-2 %}}

{{% shortcode-3 %}}

{{% shortcode-3 %}}

{{% shortcode-3 %}}

Rendered (valid HTML)

<div>shortcode-1</div>
<p>shortcode-2</p>
<img src="shortcode-3">
<img src="shortcode-3">
<img src="shortcode-3">

Yes, that’s the surprising thing. As I wrote:

A sequence of shortcodes aren’t wrapped with paragraphs if you put Markdown paragraph breaks between them. If you don’t put Markdown paragraph breaks between them, they are wrapped in a paragraph.

I tried putting <span> elements in the shortcodes in case that made a difference, but it doesn’t. I also found another wrinkle:

{{< shortcode-1 >}}

{{< shortcode-2 >}}{{< shortcode-2 >}}

{{< shortcode-3 >}}

This renders to:

<span>shortcode-1</span>

<p><span>shortcode-2</span>
<span>shortcode-2</span>
</p>
<span>shortcode-3</span>

One shortcode on a line on its own: no paragraph wrapper. Two shortcodes on a line on their own: paragraph wrapper.

So it looks as if the rule is: Two or more shortcodes in succession will always get wrapped in a paragraph, unless you separate them with a blank line.

I don’t think the Commonmark spec explains this behavior, and I don’t recall ever seeing it documented.

I guess as long as people can find this discussion and the workaround, there’s nothing else to be done, because changing Hugo’s behavior at this point would likely break thousands of sites.

For anyone stumbling across this in the future…

I’ve updated the example with the four cases on one page, including a detailed explanation of what happens (and why) in each of the four cases.

git clone --single-branch -b hugo-forum-topic-14929 https://github.com/jmooring/hugo-testing hugo-forum-topic-14929
cd hugo-forum-topic-14929
hugo server

It’s a 10 minute read, but worth the effort.

1 Like

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