Overview of the HTML details tag
The details tag creates a toggleable element which we can use to hide sensitive information like spoilers. It has a summary attribute which is visible to the reader and a button which they can click to show then content.
<details>
<summary>Spoilers!</summary>
Lot of sensitive information!
</details>
Creating Toggle Headings using summary and HTML heading tags
Since summary is an HTML element, we can nest other HTML elements in it. If we want to create something like Notion’s toggle headings (/toggleh1), we can do something like
<details>
<summary><h3>Spoilers!</h3></summary>
Spoilers
</details>
This shows the <h3> header but below the taggle i.e not inline. See this fiddle for the minimal example. Searching for this led me to this article which suggested this CSS fix to the problem
details summary > * {
display: inline;
}
This CSS snippet inlines all children of the details summary element.
Here’s the fix in action!
Using the details element in Hugo markdown
Since version 0.140, Hugo has supported details as a shortcode. We can put arbitrary HTML in the summary e.g
{{< details summary="<h3>Major Spoilers!</h3>" >}}
{{< /details >}}
Using a custom Hugo Shortcodes to generate toggle headings
I created a custom shortcode called tog with tag and summary attributes to support toggle headers. Here it is in action! Also linked is a gist with the minimal necessary HTML + CSS to create and use toggle headings in Hugo.
Shortcode HTML
<summary>
{{ $tag := .Get "tag" | default "h3" }}
{{ $heading := printf "<%s>%s%s>" $tag (.Get "summary") $tag | safeHTML }}
{{ $heading }}
</summary>
<div>
{{ .Inner }} </div>
</details>
Usage
{{< tog tag="h4" summary="Usage" >}}
{{< /tog >}}