Skip to content

Instantly share code, notes, and snippets.

@maxrodrigo
Created April 29, 2020 16:01
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxrodrigo/34996d1ea036555edb9b692d496fab19 to your computer and use it in GitHub Desktop.
Save maxrodrigo/34996d1ea036555edb9b692d496fab19 to your computer and use it in GitHub Desktop.
Details disclosure and summary elements hugo shortcode.
{{ .Scratch.Set "lastp" (sub (.Params | len) 1) }}
<details {{ if .Get 0 | eq "open"}} open {{ end }}>
<summary>
{{ .Get (.Scratch.Get "lastp") }}
</summary>
{{ .Inner | markdownify }}
</details>
@maxrodrigo
Copy link
Author

Usage

Default closed block

Input

{{<details  "This is the summary text">}}
Your markdown!
{{</details>}}

Output

This is the summary text Your markdown!

Opened block

Input

{{<details  open "This is the summary text">}}
This is an open details element
{{</details>}}

Ouput

This is the summary text This is an open details element

@loikein
Copy link

loikein commented May 8, 2021

Hey! Thank you so much for publishing this code, it really helped me a lot.

Here is my slightly twisted version:

{{ .Scratch.Set "lastp" (sub (.Params | len) 1) }}
<details {{ if .Get 0 | eq "open"}} open {{ end }}>
    <summary>
        {{ i18n "click_to_open" }}{{ .Get (.Scratch.Get "lastp") | markdownify }}
    </summary>
    {{ .Inner | markdownify }}
</details>

@exkuretrol
Copy link

exkuretrol commented Jan 23, 2023

Thanks for your sharing your code! Here is my version using named parameters:

{{ $open := .Get "open" | default "false" }}

<details
    {{ if eq $open "true"}}
        open
    {{ end }}
>
    <summary>
        {{ i18n "click_to_open" | default "Click to Open" }}
        {{ with .Get "title" }}
            {{ . | markdownify }}
        {{ end }}
    </summary>
    {{ .Inner | markdownify }}
</details>

@maxrodrigo
Copy link
Author

That's great!
Thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment