Creating Hugo Shortcodes

Published Oct 14, 2022
Updated Nov 3, 2025
1 minutes read
Note

This old post is translated by AI.

##Final Result

Prepared info, warning, danger, normal.

{{%/* hint info */%}}
Info box.
{{%/* /hint */%}}
 
{{%/* hint warning */%}}
Warning box.
{{%/* /hint */%}}
 
{{%/* hint danger */%}}
Danger box.
{{%/* /hint */%}}
 
{{%/* hint normal */%}}
Normal box.
{{%/* /hint */%}}

Output example (actual rendered result omitted)

##How to Create

Reference: Article by Nelis Oostens

I'm using Nelis Oostens' code entirely under The Unlicense.

###layouts/shortcodes/hint.html

In Hugo shortcodes, regardless of which theme you're using, layouts/shortcodes is where shortcode definition files are placed, and the filename (in this case hint) becomes the shortcode name.

{`{{ $type := .Get 0 }}`}
{`{{ printf "<blockquote className=\"md-hint %s\">" $type | htmlUnescape | safeHTML }}`}
{`{{ .Inner | safeHTML }}`}
{`{{ printf "</blockquote>" | htmlUnescape | safeHTML }}`}

###theme/**/assets/scss/custom.scss

Define CSS in scss. The location to write varies by theme. For the Stack theme I'm using, it's theme/hugo-theme-stack/assets/scss/custom.scss.

The reference article at the beginning seems to get color codes from some variable, but that variable wasn't written in the article, so I hardcoded them. I painstakingly color-picked using Windows PowerToys' color picker.

.md-hint {
    &.info {
        border-left-color: rgba(102, 187, 255, 1);
        background-color: rgba(102, 187, 255, 0.25);
    }
 
    &.warning {
        border-left-color: rgba(255, 221, 102, 1);
        background-color: rgba(255, 221, 102, 0.25);
    }
 
    &.danger {
        border-left-color: rgba(255, 102, 102, 1);
        background-color: rgba(255, 102, 102, 0.25);
    }
 
    &.normal {
        border-left-color: rgba(91, 93, 94, 1);
        background-color: rgba(91, 93, 94, 0.05);
    }
}

##Now Just Use It!

Use it like this.

{{%/* hint info */%}}
Info box.
{{%/* /hint */%}}

Since Hugo uses Go as the backend, shortcode creation uses a somewhat Go-like special syntax. Well... I guess it's easier than PHP though.