enableInlineShortcodes: true
Ongoing
According to
Repology,
the newest
packaged Hugo
is
.
2022-December-23
As of today, this evolving[1]
article
has been on
the web
for
This article assumes you know the basics about the Hugo static site generator. To learn about Hugo, see Infinite Ink’s #gohugo Portal.
A Hugo shortcode is a chunk of Go Template code[2] that you can use within a Hugo content file. To learn about Hugo shortcodes, see…
and below in this article.
Reusable and one-off Hugo shortcodes
usually live in a Hugo project’s layouts/shortcodes/
directory
but,
starting with
Hugo v0.52,
you
have the option to put a shortcode directly into
a content file
by using
an
“inline shortcode.”
This is especially useful for one-off shortcodes
such as
shortcode 1
and shortcode 2
below.
To use inline shortcodes
you need to
set
enableInlineShortcodes
to true
in your Hugo config file by
putting this in your
config.yaml
:
enableInlineShortcodes: true
Or
this
in your
config.toml
:
enableInlineShortcodes = true
ℹ |
|
Infinite Ink’s
I Use This (www.ii.com/uses/
) article
displays this:
hugo v0.111.3+extended
Each time
Hugo
builds the Infinite Ink website,
the part after
the string
“hugo ”
is generated
by the following
fragment in
the article’s source file
(uses.adoc
[3]).
v{{< hversion.inline >}} {{- hugo.Version -}} {{< /hversion.inline >}}+extended
To learn about
the
contextless[4]
hugo
function,hugo.Version
(used just above here)
and hugo.IsProduction
(used in the next section),
see
gohugo.io/functions/hugo/.
ℹ | The dashes[5]
in
the delimiters
|
On
Infinite Ink’s To-Do, Doing, and Done Lists🚧,
in the section
Writing In Progress & Experiments,
there is a list of articles
that
have not
yet
been published.
For production — i.e.
what you see — this is a list of titles without links,
but
for me,
when I’m developing the Infinite Ink website with the
hugo server
command,
this is a list of titles with links
to these
unpublished
articles.
I do this
by putting my
writing-in-progress
articles
in
a directory named
content/wip/
[6][7]
and
by putting the following
inline shortcode
in
the article’s source file, which is
website-todo-done.adoc
.[3]
{{< wip.inline >}}
<ul>
{{ range (where site.RegularPages.ByTitle "Section" "eq" "wip") }}
<li>
{{ if hugo.IsProduction }}
<em>{{ .Title }}</em>
{{ else }} <!-- not production -->
<a href="{{ .RelPermalink }}"><em>{{ .Title }}</em></a>
{{ end }} <!-- end if-else -->
</li>
{{ end }} <!-- end range -->
</ul>
{{< /wip.inline >}}
💡 | In the emphasized line above:
|
ℹ | I do things to ensure that no WIP[6] article is mentioned anywhere on Infinite Ink other than on Infinite Ink’s To-Do, Doing, and Done Lists🚧. For example, I specify appropriate build options. |
The shortcodes in this section reside in
Infinite Ink’s
layouts/shortcodes/
directory and can be called from any content file
by
using the syntax specified in
the
USAGE
comment
at the top of the shortcode.
{{/* USAGE: {{< build-date >}} Path: layouts/shortcodes/build-date.html Purpose: display current date */}} {{- now.Format "2006-01-02" -}}
Here it is in action:
page built 2023-04-11 ---------- 👆 generated by build-date shortcode
The string
2006-01-02
in the above build-date shortcode
specifies the date format.
Note that
2006
, 01
, and 02
are not a random year, month, and day.
Instead they come from the Go
reference date, which is
.
You can remember the reference date with the following mnemonic.
Mon Jan 2 15:04:05 2006 MST 1 2 3 4 5 6 -7
To learn about this, see:
{{/* USAGE: {{< omnicomment >}} commented out {{< /omnicomment >}} Forked from: github.com/gohugoio/hugoDocs/blob/master/layouts/shortcodes/todo.html Path: layouts/shortcodes/omnicomment.html Purpose: comment in content file written in any markup language Hugo supports Why 1: no comment translation needed when change file from, e.g., .md to .adoc Why 2: comment out big chunk that contains multiple markup comments[8] */}} {{ if .Inner }}{{ end }}
Note that this is a no-op shortcode.
Both
of the below
versions of
the
years-since
shortcode work.
When I first wrote this shortcode
in 2020…
I did not know that bep (Hugo’s lead developer) recommends all lower case snake_case🐍 for user-defined variable names
and I did not know about
now.Year
,
which simplifies things.
years-since
{{- /* USAGE: {{< years-since YYYY >}} EXAMPLE: {{< years-since 2013 >}} Path: layouts/shortcodes/years-since.html Why: as time goes on, this will still be the approximate number of years since YYYY */ -}} {{- $this_year := now.Year -}} {{- $that_year := .Get 0 -}} {{- sub $this_year $that_year -}} {{- /* clear trailing whitespace */ -}}
years-since
{{- /* USAGE: {{< years-since YYYY >}} EXAMPLE: {{< years-since 2013 >}} Path: layouts/shortcodes/years-since.html Why: as time goes on, this will still be the approximate number of years since YYYY */ -}} {{- $thisyear := int (now.Format "2006") -}} {{- $thatyear := .Get 0 -}} {{- sub $thisyear $thatyear -}} {{- /* clear trailing whitespace */ -}}
The
years-since
shortcode is used
in the following sections of Infinite Ink articles.
preamble of Hugo and sitemap.txt,
Endnotes section of Commenting Code Cheatsheet,
What is Markdown? section of Ordinary and Extraordinary Markdown,
and right here to state that the number of years Hugo has existed is ~10.
To avoid whitespace problems, I use
the
Go Template trim-whitespace delimiters {{-
and
-}}
throughout
these
shortcodes.
To remember this syntax, I use the following mnemonic.
{{- -}} 👆 minus (subtract) 👆 | surrounding whitespace | +------------------------+
To learn about Hugo variable names,
such as
$this_year
and
$that_year
,
see Infinite Ink’s
Variable and Parameter Names in Hugo (featuring camelCase🐫 and snake_case🐍).
{{- /* USAGE: {{< get-leaf-text "filename" >}} EXAMPLE: {{< get-leaf-text "config.py" >}} Path: layouts/shortcodes/get-leaf-text.html Purpose: get content of text file located in current leaf bundle */ -}} {{- $filename := .Get 0 -}} {{- $file := .Page.Resources.Get $filename -}} {{- $file.Content -}}
Below are three examples.
{{< highlight python "hl_lines=15,linenos=inline,anchorlinenos=true" >}} {{< get-leaf-text "config.py" >}} {{< /highlight >}}
The above example is used in the source of Infinite Ink’s qutebrowser’s Template config.py.
{{< highlight markdown >}} {{< get-leaf-text "mainmatter-get.pdmd" >}} {{< /highlight >}}
Examples 2 and 3 are both used in
the source of Infinite Ink’s
Links and Footnotes in Markdown.
Note that Example 2 uses the
{{%
and %}}
{{<
and >}}
💡 |
|
{{- /* USAGE: {{< get-assets-text "relative/path/to/filename" >}} EXAMPLE: {{< get-assets-text "qb/config-fragment1.py" >}} Path: layouts/shortcodes/get-assets-text.html Purpose: get content of text file located below assets/text/ */ -}} {{- $file := resources.Get (printf "text/%s" ($.Get 0)) -}} {{- $file.Content -}}
The above
EXAMPLE
get-assets-text
is used in Infinite Ink’s…
💡 |
|
{{/* USAGE: {{< html4-get-bundle-image "filename" "alt text" "width" "link" >}} NOTE: parameters 1 & 2 required, parameters 3 & 4 optional EXAMPLE 1: {{< html4-get-bundle-image "gologo.png" "Go Logo" >}} EXAMPLE 2: {{< html4-get-bundle-image "gologo.png" "Go Logo" "50%" >}} EXAMPLE 3: {{< html4-get-bundle-image "gologo.png" "Go Logo" "25%" "https://blog.golang.org/go-brand" >}} PATH: layouts/shortcodes/html4-get-bundle-image.html PURPOSE: get image located in current bundle */}} {{ $filename := .Get 0 }} {{ $alt := .Get 1 }} {{ $width := .Get 2 }} {{ $link := .Get 3 }} {{ $file := .Page.Resources.Get $filename }} {{ with $link }} <a href="{{ . }}"> {{ end }} <img src="{{ $file.RelPermalink }}" alt="{{ $alt }}" {{ with $width }} width="{{ . }}" {{ end }} > {{ if $link }} </a> {{ end }}
Below are three examples.
{{< html4-get-bundle-image "gologo.png" "Go Logo" "25%" "https://blog.golang.org/go-brand" >}}
ℹ |
|
If your leaf bundle looks something like this…
. └── hugo-tips-fragments/ ├── index.md └── gologo_fuchsia.png
then you can use this Markdown image syntax in index.md
to reference
the image:
 ------------------ 👆 path relative to index.md
which renders as:
ℹ |
|
{{/* USAGE: {{% embed-twitter-list listname %}} EXAMPLE: {{% embed-twitter-list zeitgeist %}} Path: layouts/shortcodes/embed-twitter-list.adoc Purpose: Embed heading and Twitter list in a page written in AsciiDoc */}} {{ $listbasename := .Get 0 }} === @nm/lists/{{- $listbasename }} ++++ <a class="twitter-timeline" href="https://twitter.com/nm/lists/{{- $listbasename -}}?ref_src=twsrc%5Etfw">A Twitter List by @nm <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script> ++++
This shortcode is used in some Infinite Ink portals, for example in the #zeitgeist Portal.
‼ | This shortcode uses
AsciiDoc
syntax and is meant to be used in an
AsciiDoc (.ad or .adoc )
content
source file
and called with
the
{{% and %}}
shortcode delimiters
(not
the
{{< and >}}
shortcode
delimiters). |
Hugo includes built-in shortcodes, which you can read about at:
A good way to learn about writing your own shortcodes is to look at the source code of Hugo’s built-in shortcodes. That code is available on GitHub at:
When I use Hugo’s built-in highlight shortcode and want to emphasize lines, I almost always need to look up the syntax, which can look like this:
{{< highlight html "hl_lines=3-5" >}} html code that will be displayed with lines 3, 4, and 5 emphasized {{< /highlight >}}
Or this:
{{< highlight toml "hl_lines=1 12 33" >}} toml code that will be displayed with lines 1, 12, and 33 emphasized {{< /highlight >}}
To learn about the built-in highlight shortcode, see:
github.com/gohugoio/hugo/blob/master/tpl/tplimpl/embedded/templates/shortcodes/highlight.html
gohugo.io/functions/highlight/ - this Hugo function is used in the built-in highlight shortcode (it’s confusing that a Hugo shortcode and a Hugo function are both named “highlight”😕[9])
💡 | To view a list of all possible highlighting languages, such as
html , python , and toml , see
gohugo.io/content-management/syntax-highlighting#list-of-chroma-highlighting-languages. |
More shortcodes, including admonition shortcodes, are available in the Hugo Documentation repository in this directory:
To use
one of the Hugo docs
shortcodes, copy it
to
somewhere below
your
project’s
layouts/shortcodes/
directory, for example to one of the following
directories.
layouts/shortcodes/ layouts/shortcodes/3rdparty/ layouts/shortcodes/hugodocs/
💡 | {{ $_hugo_config := `{ "version": 1 }` }} This tells Hugo to process this shortcode the way that Hugo v0.54.0, and earlier do. To learn about this, see the Hugo v0.55.0 release notes, |
For more about Hugo, see Infinite Ink’s…
Hugo’s .RenderString
Method (featuring AsciiDoc admonitions in Markdown and Go HTML)
Configuring Security in Hugo (featuring settings needed to use Asciidoctor and Pandoc)🔒
Variable and Parameter Names in Hugo (featuring camelCase🐫 and snake_case🐍)
🔗 Linkified Section Headings in Hugo-Generated Web Pages (featuring Markdown and AsciiDoc examples)
TGIH: Themeless & Gitless Introduction to the Hugo SSG (a tutorial)
Transforming Text with Hugo (featuring plainify
, htmlUnescape
, and more)
Hugo’s Markup Languages: AsciiDoc, HTML, Markdown,
A Way to Compare Hugo’s Markup Languages (featuring inline footnotes)📊
.adoc
could be any of the content file extensions known to Hugo (.ad
, .adoc
, .htm
, .html
, .markdown
, .md
, .mdown
, .org
, .pandoc
, .pdc
, or .rst
).hugo
and site
are contextless by noticing that they do not have a leading dot (.
). To learn more about these global functions, see the Hugo v0.53 release notes, especially the relevant GitHub commit and GitHub issues.-
) is “hyphen-minus,” but its proper name is rarely used. Instead, it is usually called “dash,” “hyphen,” or “minus.” Details are at wikipedia.org’s Hyphen-minus.draft: true
. Only the titles of the WIP articles that are not drafts are listed when I publish Infinite Ink’s To-Do, Doing, and Done Lists🚧 page.@nm@mathstodon.xyz
or
#InfiniteInk
in it.