My Hugo Shortcodes
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.


My Hugo Shortcodes

These are shortcodes that I have created for the Hugo static blogging engine.

To use them, copy the HTML files into your-hugo-blog/layouts/shortcodes/ or your-hugo-blog/themes/current-theme/layouts/shortcodes/. I do not suggest the second approach. Create the shortcodes directory if it does not exist. You can learn more about shortcodes from Hugo documentation.

Codecaption (codecaption.html)

Adds title to code blocks.


{{< codecaption lang="html" title="Code caption shortcode" >}}
<figure class="code">
    <span>{{ .Get "title" }}</span>
  <div class="codewrapper">
    {{ highlight .Inner (.Get "lang") "linenos=true" }}
{{< /codecaption >}}

Results in:

Captioned codeblock

Image Caption (imgcap.html)

This shortcode adds captions to pictures. Due to the way the original css file was organized, this shortcode does not use <figure> and <figcaption>. Alt will also be set to title.


{{< imgcap title="Sample caption" src="/images/2016/thetheme/1.png" >}}

Results in HTML:

<span class="caption-wrapper">
  <img class="caption" src="/images/2016/thetheme/1.png" title="Sample caption" alt="Sample caption">
  <span class="caption-text">Sample caption</span>

Wikipedia Link Generator (wp.html)

I attempted to the re-create the Jekyll Wikipedia link generator by Keltia. You can see the source here.

You can see the discussion on Hugo forums here with my original gist.

It has some shortcomings. There are definitely better ways to do this. Personally I prefer the named parameter version because the shortcode is easier to write but this should work for both.


You can either use named parameters:

  • {{< wp tag="VIC_cipher" >}}
  • {{< wp tag="VIC_cipher" lang="fr" >}}
  • {{< wp tag="VIC_cipher" lang="fr" title="" >}}
  • {{< wp tag="VIC_cipher" title="VIC Cipher" >}}
  • {{< wp tag="VIC_cipher" lang="en" title="VIC Cipher" >}}

Rendered to:

<a href="">VIC_cipher</a>
<a href="">VIC_cipher</a>
<a href="">VIC_cipher</a>
<a href="">VIC Cipher</a>
<a href="">VIC Cipher</a>

Or positional:

  • {{< wp VIC_cipher >}}
  • {{< wp VIC_cipher fr >}}
  • {{< wp VIC_cipher "" fr >}}
  • {{< wp VIC_cipher "VIC Cipher" >}}
  • {{< wp VIC_cipher "VIC Cipher" fr >}}
<a href="">VIC_cipher</a>
<a href="">VIC_cipher</a>
<a href="">VIC_cipher</a>
<a href="">VIC Cipher</a>
<a href="">VIC Cipher</a>

It does not remove the underlines from tag to use in title. I have not found a Hugo template function for that yet (perhaps there is one).

Octopress blockquote (blockquote.html)

Port of the Octopress blockquote plugin at

It supports author, link, source and title. If title is not provided and link is longer than 32 characters, it is cut at the last forward slash similar to the original plugin.

It can be used as follows (also in tests/wp-test.markdown):

Normal quote:
{{< blockquote >}}
  This is a simple quote.
{{< /blockquote >}}

Quote with author
{{< blockquote author="Author2" >}}
  This is a quote with only an Author named Author2.
{{< /blockquote >}}

Quote with author and source
{{< blockquote author="Author3" source="Source" >}}
  This is a quote from Author3 and source "source."
{{< /blockquote >}}

Quote with author and link
{{< blockquote author="Author4" link="" >}}
  This is a quote from Author4 and links to
{{< /blockquote >}}

Quote with author, link and title
{{< blockquote author="Author5" link="" title="Google" >}}
  This is a quote from Author5 and links to with title "Google."
{{< /blockquote >}}

Quote with author and a link longer than 32 characters, string is first cut at 32 characters then everything after the last forward slash is removed
{{< blockquote author="Author6" link="" >}}
  This is a quote from Author5 and links to which is longer than 32 characters.
  <br>And this is a new line in the quote with the HTML br tag.
{{< /blockquote >}}

Test from the Octopress blockquote page at:
{{< blockquote author="@allanbranch" link="" >}}
  Over the past 24 hours I've been reflecting on my life & I've realized only one thing. I need a medieval battle axe.
{{< blockquote >}}

Results in:


Year (year.html)

Displays current year - based on shortcode tutorial at

Usage: {{< year >}}.

Editor Snippets

I have created snippets for both Sublime Text and Atom.

Sublime Text Completions

Sublime text completions are stored in a .sublime-completion file in User directory.

   "scope": "text.html.markdown - source - meta.tag, punctuation.definition.tag.begin",

      { "trigger": "codecap\tCodecaption Hugo Shortcode", "contents": "{{< codecaption title=\"$1\" lang=\"$2\" >}}\n${3:default text}\n{{< /codecaption >}}" },
      { "trigger": "imgcap\tImagecaption Hugo Shortcode", "contents": "{{< imgcap title=\"$1\" src=\"/images/2017/${2:1.png}\" >}}" },
      { "trigger": "wp\tWikipedia Link Generator Hugo Shortcode", "contents": "{{< imgcap tag=\"$1\" lang=\"$2\" title=\"$3\" >}}" },
      { "trigger": "blockquote\tBlock quote Hugo Shortcode", "contents": "{{< blockquote author=\"$1\" link=\"$2\" title=\"$3\" >}}\n{$4:quote text}\n{{< /codecaption >}}" }

VS Code Snippets

VS Code snippets look nicer than the other two:

    "codecaption": {
		"prefix": "codecap",
		"body": [
			"{{< codecaption title=\"${1:title}\" lang=\"${2:language}\" >}}",
			"{{< /codecaption >}}",
		"description": "codecaption Hugo shortcode"
	"imgcap": {
		"prefix": "imgcap",
		"body": [
			"{{< imgcap title=\"${1:title}\" src=\"${2:filename}.png\" >}}",
		"description": "image caption Hugo shortcode"

Atom Snippets

I use Atom editor these days so I created a couple of snippets to insert these shortcodes while editing Markdown files. In order to read on how to create snippets please refer to Atom's Snippets package.

Open your snippets file (on Windows it's File > Open Your Snippets) and paste the following in the file:

    'prefix': 'codecap'
    'body': """
    {{< codecaption title="$1" lang="$2"  >}}
    {{< /codecaption >}}
    'prefix': 'imgcap'
    'body': '{{< imgcap title="$1" src="/images/2016/$2" >}}'
    'prefix': 'wp'
    'body': '{{< wp tag="$1" lang="$2" title="$3" >}}'
    'prefix': 'blockquote'
    'body': """
    {{< blockquote author="$1" link="$2" title="$3" >}}
    {{< /blockquote >}}

My original mistake was to repeat '.source.gfm' before the imgcap snippet, seems like cson keys should not be repeated.

You can trigger the shortcodes by entering imgcap and codecap respectively and then pressing enter. You can change these by modifying the prefix in the code above. After inserting the shortcode, the cursor will go to the first location which is designated by $1 which is title in both cases. After entering the value you can go to $2 and $3 by pressing tab.


I don't know, MIT I guess.