Bug when using details shortcode

I’m having issue using the details shortcode. When writing like this:

---
title: "Sample"
---

Paragraph.

{{< details summary="summary 1" >}}
details1
{{< /details >}}
{{< details summary="summary 2" >}}
details2
{{< /details >}}

There will be <p>\n</p> in the rendered result:

				<p>Paragraph.</p>
<p>
<details>
  <summary>summary 1</summary>
  <p>details1</p>
</details>


<details>
  <summary>summary 2</summary>
  <p>details2</p>
</details>
</p>

See Trim whitespace before details tag for details shortcode by BLumia · Pull Request #13215 · gohugoio/hugo · GitHub

Is it a bug? I wrote a patch to fix such an issue by trimming the whitespace before the details HTML tag (the previous link), is that a proper fix?

Thanks!

No, this is not a bug.

Two or more sequential lines of Markdown are one paragraph. So, without a blank line between the two shortcode calls, they’re encapsulated in a single paragraph element.

And that’s invalid, because details elements are block level elements (i.e., they cannot be placed within a paragraph element).

Two or more sequential lines of Markdown are one paragraph. So, without a blank line between the two shortcode calls, they’re encapsulated in a single paragraph element.

Yeah I understand that, BUT it seems chrome doesn’t parse the html in such manner?

See this screenshot in practice, the browser (chrome) parse it as there is a <p></p> after the paragraph, and another paragraph after the second <details>.

Browsers try to correct invalid HTML, so you’re looking at the browser’s interpretation of the HTML, not the actual HTML.

And that’s invalid, because details elements are block level elements (i.e., they cannot be placed within a paragraph element).

I guess this is the reason, but I don’t want it to be in a paragraph element in the first place (i.e. I originally want to simply write <details> tag but cannot because I haven’t enable unsafe. I wasn’t expect these details tag are in a paragraph). So isn’t trimming the whitespace before the details tag the solution to do so?

Put a blank line between the two shortcode calls. They are block level elements.

Thanks! I guess it counts as intended.

I’m porting my old blog from jekyll to hugo, the markdown file that having this issue have a bunch of details tag. I still want to avoid adding blank lines in my original markdown file because doing so will make it even more unreadable. I guess I might want to enable unsafe and keep using raw html tag instead of the details shortcode.

Thanks again for your fast response!

Hi there,

I’m using this shortcode, which work fine, and has the specificity to load the CSS dedicated to < details> element only once:

Thanks for the tip! I was using that shortcode comes with the official hugo build. Currently I choose to simply override the shortcode by putting my patched version to the website’s layout folder (layouts\shortcodes\details.html) so I could use my version that trims the unwanted whitespaces :slight_smile:

Within the details shortcode you can remove all the whitespace you want, even reducing it to:

{{- .Inner -}}

But unless you add a blank line between the two shortcode calls, the rendered shortcodes will be wrapped within a paragraph. We’ve already had this conversation.

But unless you add a blank line between the two shortcode calls, the rendered shortcodes will be wrapped within a paragraph. We’ve already had this conversation.

Indeed, it will be wrapped in a pair of <p> tags, but for my own use-case, as long as the browser parse them as empty paragraph (which is the case), I can simply using p:empty css selector to make them hidden. Without the trimming, the browser makes the first (opening) <p> non-empty, which caused my original issue.

My old Jekyll site actually have a p:empty css rule that hides the empty <p></p> tag probably was workaround some jekyll issue (to far away I can’t remember why it was added). The css file is ported to my hugo site as-is so I only noticed the non-empty paragraph issue before and didn’t realize it wasn’t the root cause.

Understood.

I just wanted to confirm that you are deliberately choosing to publish invalid HTML.