Chapter 3

Shortcodes

Hugo uses Markdown for its simple content format. However, there are a lot of things that Markdown doesn’t support well. You could use pure HTML to expand possibilities.

But this happens to be a bad idea. Everyone uses Markdown because it’s pure and simple to read even non-rendered. You should avoid HTML to keep it as simple as possible.

To avoid this limitations, Hugo created shortcodes. A shortcode is a simple snippet inside a page.

The Relearn theme provides multiple shortcodes on top of existing ones.

Attachments

List of files attached to a page

Button

Nice buttons on your page

Children

List the child pages of a page

Expand

Displays an expandable/collapsible section of text on your page

Include

Displays content from other Markdown files

Mermaid

Generation of diagram and flowchart from text in a similar manner as Markdown

Notice

Disclaimers to help you structure your page

Site param

Get value of site params variables in your page

Swagger

Adds UI for your Swagger / OpenAPI Specifications

Tabbed views

Synchronize selection of content in different tabbed views

Attachments

The attachments shortcode displays a list of files attached to a page with adjustable color, title and icon.

Usage

The shortcurt lists files found in a specific folder.

{{% attachments /%}}

Currently, it support two implementations for pages

  1. If your page is a Markdown file, attachements must be placed in a folder named like your page and ending with .files.

    • content
      • _index.md
      • page.files
        • attachment.pdf
      • page.md
  2. If your page is a folder, attachements must be placed in a nested ‘files’ folder.

    • content
      • _index.md
      • page
        • index.md
        • files
          • attachment.pdf

Be aware that if you use a multilingual website, you will need to have as many folders as languages.

Parameter

Name Optional Default Notes
style yes transparent The color scheme used to highlight the box content.

- by severity: info, note, tip, warning
- by brand color: primary, secondary
- by color: blue, green, grey, orange, red
- by special color: default, transparent
title yes see notes Arbitray text for the box title. Depending on the style there may be a default title. Any given value will overwrite the default.

- for severity styles: the matching title for the severity
- for all other colors: Attachments

If you want no title, you have to set this parameter to " " (a non empty string filled with spaces)
icon yes see notes Font Awesome icon name set to the left of the title. Depending on the style there may be a default icon. Any given value will overwrite the default.

- for severity styles: a nice matching icon for the severity
- for all other colors: paperclip

If you want no icon, you have to set this parameter to " " (a non empty string filled with spaces)
sort yes asc Sorting the output in ascending or descending order.
pattern yes .* A regular expressions, used to filter the attachments by file name. For example:

- to match a file suffix of ‘jpg’, use .*jpg (not *.jpg)
- to match file names ending in jpg or png, use .*(jpg|png)

Examples

Custom title, list of attachments ending in pdf or mp4

Show markup

Info styled box, descending sort order

Show markup

Style and icons

For further examples for style, title and icon, see the notice shortcode documentation. The parameter are working the same way for both shortcodes, besides having different defaults.

Button

A button is a just a clickable button with optional icon.

{{% button href="https://gohugo.io/" %}}Get Hugo{{% /button %}}
{{% button href="https://gohugo.io/" icon="fas fa-download" %}}Get Hugo with icon{{% /button %}}
{{% button href="https://gohugo.io/" icon="fas fa-download" icon-position="right" %}}Get Hugo with icon right{{% /button %}}

Get Hugo Get Hugo with icon Get Hugo with icon right

Children

Use the children shortcode to list the child pages of a page and the further descendants (children’s children). By default, the shortcode displays links to the child pages.

Usage

Parameter Default Description
page current Specify the page name (section name) to display children for
containerstyle “ul” Choose the style used to group all children. It could be any HTML tag name
style “li” Choose the style used to display each descendant. It could be any HTML tag name
showhidden “false” When true, child pages hidden from the menu will be displayed
description “false” Allows you to include a short text under each page in the list. When no description exists for the page, children shortcode takes the first 70 words of your content. Read more info about summaries on gohugo.io
depth 1 Enter a number to specify the depth of descendants to display. For example, if the value is 2, the shortcode will display 2 levels of child pages. Tips: set 999 to get all descendants
sort ordersectionsby Sort children by weight, to sort on menu order - title, to sort alphabetically on menu label. If not set it is sorted by the ordersectionsby setting of the site and the pages frontmatter

Demo

{{% children  %}}
{{% children description="true" %}}
  • page X
  • This is a plain page test, and the beginning of a YAML multiline description...

  • page 1
  • This is a demo child page

  • page 2
  • This is a demo child page with no description. So its content is used as description.

  • page 3
  • This is a demo child page

{{% children depth="999" showhidden="true" %}}
{{% children containerstyle="div" style="h2" depth="3" description="true" %}}

page X

This is a plain page test, and the beginning of a YAML multiline description...

page 1

This is a demo child page

page 1-1

This is a demo child page

page 1-1-2

This is a demo child page

page 1-1-3

This is a demo child page

page 2

This is a demo child page with no description. So its content is used as description.

page 3

This is a demo child page

page 3-1

This is a plain page test nested in a parent

{{% children containerstyle="div" style="div" depth="999" %}}

page X

This is a plain demo child page.

page 1

This is a demo child page.

Subpages of this page

page 1-1-2-1

This is a plain demo child page.

page 1-1-2-2

This is a plain demo child page.

page 1-1-3

This is a plain demo child page.

page 2

This is a demo child page with no description.

So its content is used as description.

page 3

This is a demo child page.

Subpages of this page

page 3-1

This is a plain demo child page.

Expand

The Expand shortcode displays an expandable/collapsible section of text on your page.

Usage

{{% expand [ <string> [ "true" | "false" ] ] %}}
Yes!
{{% /expand %}}

The first optional parameter defines the text that appears next to the expand/collapse icon. The default text is "Expand me...".

The second optional parameter controls if the block is initially shown as expanded ("true") or collapsed ("false"). The default ist "false".

Examples

I'll tell you a secret...

All defaults

Expand me...
Show markup
{{% expand %}}
Yes, you did it!
{{% /expand %}}

Initially expanded

Expand me...

No need to press you!

Show markup
{{% expand "Expand me..." "true" %}}
No need to press you!
{{% /expand %}}

Arbitrary text

Show me almost endless possibilities
Show markup

Include

The include shortcode includes other files from your project inside of the current file. This can even contain Markdown and will be taken into account when generating the table of contents.

Usage

{{% include <string> [ "true" | "false" ] %}}

The first required parameter is the path to the file to be included.

If the file’s content will be displayed as HTML, the second optional parameter controls if the first heading of the included file should be displayed ("true")- which is the default - or be hidden.

Examples

Arbitray content

You can add standard markdown syntax:

  • multiple paragraphs
  • bullet point lists
  • emphasized, bold and even bold emphasized text
  • links
  • etc.
...and even source code

the possiblities are endless (almost - including other shortcodes may or may not work) (almost - including other shortcodes may or may not work)

Show markup

Mermaid

Mermaid is a library helping you to generate diagram and flowcharts from text, in a similar manner as Markdown.

Note

This only works in modern browsers.

Warning

Due to limitations with Mermaid, it is currently not possible to use Mermaid code fences in an initially collapsed expand shortcode. This is a know issue and can’t be fixed by this theme.

Usage

Just insert your Mermaid code in the mermaid shortcode like this:

{{< mermaid [ align=("left"|"right"|"center"|"justify") ] >}}
classDiagram
    Person *-- Dog
{{< /mermaid >}}

You can set an optional align attribute which defaults to "center".

If you don’t need alignment you can use the alternative syntax using code fences if you have turned off guessSyntax for the markup.highlight setting (see below):

```mermaid
classDiagram
    Person *-- Dog
```

The generated graphs can be be panned by dragging them and zoomed by using the mousewheel. On mobile devices you can use finger gestures.

Examples

Flowchart

%%{init:{"theme":"forest"}}%% graph LR; A[Hard edge] -->|Link text| B(Round edge) B --> C{Decision} C -->|One| D[Result one] C -->|Two| E[Result two]
Show markup
{{< mermaid align="left" >}}
%%{init:{"theme":"forest"}}%%
graph LR;
    A[Hard edge] -->|Link text| B(Round edge)
    B --> C{<strong>Decision</strong>}
    C -->|One| D[Result one]
    C -->|Two| E[Result two]
{{< /mermaid >}}

Sequence

sequenceDiagram participant Alice participant Bob Alice->>John: Hello John, how are you? loop Healthcheck John->John: Fight against hypochondria end Note right of John: Rational thoughts
prevail... John-->Alice: Great! John->Bob: How about you? Bob-->John: Jolly good!
Show markup
{{< mermaid >}}
sequenceDiagram
    participant Alice
    participant Bob
    Alice->>John: Hello John, how are you?
    loop Healthcheck
        John->John: Fight against hypochondria
    end
    Note right of John: Rational thoughts <br/>prevail...
    John-->Alice: Great!
    John->Bob: How about you?
    Bob-->John: Jolly good!
{{< /mermaid >}}

GANTT

gantt dateFormat YYYY-MM-DD title Adding GANTT diagram functionality to Mermaid section A section Completed task :done, des1, 2014-01-06,2014-01-08 Active task :active, des2, 2014-01-09, 3d Future task : des3, after des2, 5d Future task2 : des4, after des3, 5d section Critical tasks Completed task in the critical line :crit, done, 2014-01-06,24h Implement parser and jison :crit, done, after des1, 2d Create tests for parser :crit, active, 3d Future task in critical line :crit, 5d Create tests for renderer :2d Add to Mermaid :1d
Show markup
{{< mermaid >}}
gantt
        dateFormat  YYYY-MM-DD
        title Adding GANTT diagram functionality to Mermaid
        section A section
        Completed task            :done,    des1, 2014-01-06,2014-01-08
        Active task               :active,  des2, 2014-01-09, 3d
        Future task               :         des3, after des2, 5d
        Future task2              :         des4, after des3, 5d
        section Critical tasks
        Completed task in the critical line :crit, done, 2014-01-06,24h
        Implement parser and jison          :crit, done, after des1, 2d
        Create tests for parser             :crit, active, 3d
        Future task in critical line        :crit, 5d
        Create tests for renderer           :2d
        Add to Mermaid                      :1d
{{< /mermaid >}}

Class

classDiagram Class01 <|-- AveryLongClass : Cool Class03 *-- Class04 Class05 o-- Class06 Class07 .. Class08 Class09 --> C2 : Where am i? Class09 --* C3 Class09 --|> Class07 Class07 : equals() Class07 : Object[] elementData Class01 : size() Class01 : int chimp Class01 : int gorilla Class08 <--> C2: Cool label
Show markup
{{< mermaid >}}
classDiagram
    Class01 <|-- AveryLongClass : Cool
    Class03 *-- Class04
    Class05 o-- Class06
    Class07 .. Class08
    Class09 --> C2 : Where am i?
    Class09 --* C3
    Class09 --|> Class07
    Class07 : equals()
    Class07 : Object[] elementData
    Class01 : size()
    Class01 : int chimp
    Class01 : int gorilla
    Class08 <--> C2: Cool label
{{< /mermaid >}}

State

stateDiagram-v2 open: Open Door closed: Closed Door locked: Locked Door open --> closed: Close closed --> locked: Lock locked --> closed: Unlock closed --> open: Open
Show markup
```mermaid
stateDiagram-v2
    open: Open Door
    closed: Closed Door
    locked: Locked Door
    open   --> closed: Close
    closed --> locked: Lock
    locked --> closed: Unlock
    closed --> open: Open
```

Configuration

Mermaid is configured with default settings. You can customize Mermaid’s default settings for all of your files thru a JSON object in your config.toml, override these settings per page thru your pages frontmatter or override these setting per diagramm thru diagram directives.

The JSON object of your config.toml / frontmatter is forwarded into Mermaid’s mermaid.initialize() function.

See Mermaid documentation for all allowed settings.

The theme setting can also be set by your used color variant. This will be the sitewide default and can - again - be overridden by your settings in config.toml, frontmatter or diagram directives.

Note

If you want to use mermaid codefences, you have to turn off guessSyntax for the markup.highlight setting.

Example

[params]
  mermaidInitialize = "{ \"theme\": \"dark\" }"

[markup]
  [markup.highlight]
    # if set to `guessSyntax = true`, there will be no unstyled code even if no language
    # was given BUT mermaid code fences will not work anymore! So this is a mandatory
    # setting for your site
    guessSyntax = false

or pages frontmatter

+++
mermaidInitialize = "{ \"theme\": \"dark\" }"
+++

Notice

The notice shortcode shows various types of disclaimers with adjustable color, title and icon to help you structure your page.

There may be pirates

It is all about the boxes.

Usage

While the examples are using named parameter you are free to use positional aswell.

{{% notice style="primary" title="There may be pirates" icon="skull-crossbones" %}}
It is all about the boxes.
{{% /notice %}}
{{% notice primary "There may be pirates" "skull-crossbones" %}}
It is all about the boxes.
{{% /notice %}}

Parameter

Name Position Optional Default Notes
style 1 yes default The color scheme used to highlight the box content.

- by severity: info, note, tip, warning
- by brand color: primary, secondary
- by color: blue, green, grey, orange, red
- by special color: default, transparent
title 2 yes see notes Arbitray text for the box title. Depending on the style there may be a default title. Any given value will overwrite the default.

- for severity styles: the matching title for the severity
- for all other colors: no title

If you want no title for a severity style, you have to set this parameter to " " (a non empty string filled with spaces)
icon 3 yes see notes Font Awesome icon name set to the left of the title. Depending on the style there may be a default icon. Any given value will overwrite the default.

- for severity styles: a nice matching icon for the severity
- for all other colors: no icon

If you want no icon for a severity style, you have to set this parameter to " " (a non empty string filled with spaces)

Examples

By Severity

Info with markup

Info

An information disclaimer

You can add standard markdown syntax:

  • multiple paragraphs
  • bullet point lists
  • emphasized, bold and even bold emphasized text
  • links
  • etc.
...and even source code

the possiblities are endless (almost - including other shortcodes may or may not work)

Show markup

Note

Note

A notice disclaimer

Show markup

Tip

Tip

A tip disclaimer

Show markup

Warning

Warning

A warning disclaimer

Show markup

Warning with non-default title and icon

Here are dragons

A warning disclaimer

Show markup

Warning without a title and icon

A warning disclaimer

Show markup

By Brand Colors

Primary with title only

Primary

A primary disclaimer

Show markup

Secondary with icon only

A secondary disclaimer

Show markup

By Color

Blue without a title and icon

A blue disclaimer

Show markup

Green with title only

Green

A green disclaimer

Show markup

Grey with icon only

A grey disclaimer

Show markup

Orange with title and icon

Orange

A orange disclaimer

Show markup

Red

A red disclaimer

Show markup

By Special Color

Default with title and icon

Pay Attention to this Note!

Some serious information.

Show markup

Transparent with title and icon

Pay Attention to this Note!

Some serious information.

Show markup

Site param

The siteparam shortcode is used to help you print values of site params.

Usage

{{% siteparam <string> %}}

The first required parameter is the name of the site param to be displayed.

Examples

For instance, in this current site, the editURL variable is used in config.toml

[params]
  editURL = "https://github.com/McShelby/hugo-theme-relearn/edit/main/exampleSite/content/"

Use the siteparam shortcode to display its value.

`editURL` Value : {{% siteparam "editURL" %}}

is displayed as

editURL Value : https://github.com/McShelby/hugo-theme-relearn/edit/main/exampleSite/content/

Swagger

This shortcode uses the RapiDoc library to display your OpenAPI Specifications.

Note

This only works in modern browsers.

Configuration

Swagger is configured with default settings. You can customize Swagger’s default settings for all of your files thru a JSON object in your config.toml or override these settings per page thru your pages frontmatter.

The JSON object of your config.toml / frontmatter is forwarded into Swagger’s initialization. At the moment, only the theme setting is supported.

The theme setting can also be set by your used color variant. This will be the sitewide default and can - again - be overridden by your settings in config.toml or frontmatter.

Example

[params]
  swaggerInitialize = "{ \"theme\": \"dark\" }"

Usage

Just insert give the URL to your OpenAPI Specification like this:

{{< swagger src="https://petstore3.swagger.io/api/v3/openapi.json" >}}

If your page is a leaf or branch bundle, you can also use relative URLs:

{{< swagger src="petstore.json" >}}

The src parameter is mandatory and can be either an absolute or a relative URL.

Example

Tabbed views

Choose which content to see across the page. Very handy for providing code snippets for multiple languages or providing configuration in different formats.

Code example

{{< tabs >}}
{{% tab name="python" %}}
```python
print("Hello World!")
```
{{% /tab %}}
{{% tab name="R" %}}
```R
> print("Hello World!")
```
{{% /tab %}}
{{% tab name="Bash" %}}
```Bash
echo "Hello World!"
```
{{% /tab %}}
{{< /tabs >}}

Renders as:

print("Hello World!")
> print("Hello World!")
echo "Hello World!"

Tab views with the same tabs that belong to the same group sychronize their selection:

print("Hello World!")
> print("Hello World!")
echo "Hello World!"

Config example

{{< tabs groupId="config" >}}
{{% tab name="json" %}}
```json
{
  "Hello": "World"
}
```
{{% /tab %}}
{{% tab name="XML" %}}
```xml
<Hello>World</Hello>
```
{{% /tab %}}
{{% tab name="properties" %}}
```properties
Hello = World
```
{{% /tab %}}
{{< /tabs >}}

Renders as:

{
  "Hello": "World"
}
<Hello>World</Hello>
Hello = World
Warning

When using tab views with different content sets, make sure to use a common groupId for equal sets but distinct groupId for different sets. The groupId defaults to 'default'. Take this into account across the whole site! The tab selection is restored automatically based on the groupId and if it cannot find a tab item because it came from the 'default' group on a different page then all tabs will be empty at first.