title: >- Portals: Doorways to Infinite Ink's Website & Beyond
This article assumes you know about…
Unicode,[2]
and the difference between a literal (what-you-see-is-what-you-get) string and a non-literal (interpreted) string.
In Hugo, a web page is built from text that resides in…
layout files (including shortcodes),
the page’s primary source file (front matter and main matter),
data files,
resource files,
and static files.
Text can be interpreted as…
HTML,
Go Template code (also known as Go HTML),[3]
Markdown,
Org-mode markup,
raw literal (uninterpreted) plain text,
or other things.
For text that can be defined by a user, for example the title of a page, Hugo does things to ensure the safety of the HTML that Hugo generates. For example, if this is in the front matter of a page’s primary source file:
title: >- Portals: Doorways to Infinite Ink's Website & Beyond
And if one of the page’s layout files includes
{{ .Title }}
,
Hugo (with its default settings),
will generate this:
Portals: Doorways to Infinite Ink's Website & Beyond
Note that…
'
was transformed into '
and
&
was transformed into &
.
This is called entity escaping.
To prevent Hugo from doing the above entity escaping, you can do either of the following two options.
In the layout file, use
{{ .Title | safeHTML }}
(instead of
{{ .Title }}
) or
in the Hugo project’s config file, specify the following.
outputFormats:
html:
isPlainText: true
In the Infinite Ink Hugo project,
I do #2, which means I do not need to use
| safeHTML
in Infinite Ink’s layout files, but I do sometimes
need to use
| plainify
,
| htmlUnescape
, and
other text-transformation functions.
Suppose the following is in the front matter of the primary source file of an Infinite Ink page.
title: >- Portals 🚪︎:<br>Doorways to Infinite Ink's Website <nobr>& Beyond🚪</nobr>
Note that this title
variable
contains…
This title
can be used in many places on the
Infinite Ink
website, for example…
in a <title>
element,
in an <h1>
element,
in lists of pages on relevant see-also sections and portals,
in relevant “next” and “previous” link titles,
and in a “Share on Mastodon” link.
In the
Infinite Ink
layout files,
I
sometimes use
{{ .Title }}
,
but other times I need to remove
some or all of the HTML in the title.
The following sections show
how to use some
Hugo functions
to
transform
this title.
isPlainText
for HTML outputAs I wrote above, the Infinite Ink website’s Hugo config file includes this:
outputFormats:
html:
isPlainText: true
‼ | The results that I get in the following transformations depend on this setting. |
<title>
elementThe <title>
element is displayed in a browser’s title bar
and should contain only plain text (i.e., no HTML). For details
about the <title>
element,
see
developer.mozilla.org/en-US/docs/Web/HTML/Element/title.
For the following front-matter title
variable…
title: >- Portals 🚪︎:<br>Doorways to Infinite Ink's Website <nobr>& Beyond🚪</nobr>
…my
goal is for Hugo to generate the
web page’s <head>
section
and have it contain
something like this:
<title>Portals 🚪︎: Doorways to Infinite Ink's Website & Beyond🚪</title>
Note that there are no HTML entities or HTML tags in this.
{{ .Title | safeHTML }}
Portals 🚪︎:
Doorways to Infinite Ink's Website& Beyond🚪
Note that is is the same as
{{ .Title }}
because Infinite Ink’s config has isPlainText
set to true
.
To learn about
safeHTML
,
see
gohugo.io/functions/safehtml/.
{{ .Title | plainify }}
Portals 🚪︎: Doorways to Infinite Ink's Website & Beyond🚪
Note that
plainify
removed
the <br>
, <nobr>
,
and
</nobr>
tags.
To learn about
plainify
,
see
gohugo.io/functions/plainify/.
{{ .Title | htmlUnescape }}
Portals 🚪︎:
Doorways to Infinite Ink's Website& Beyond🚪
Note that
htmlUnescape
transformed
the HTML numeric and
named entities
to their corresponding literal
Unicode[2]
characters.
To learn about
htmlUnescape
,
see
gohugo.io/functions/htmlUnescape/.
{{ .Title | htmlEscape }}
Portals 🚪︎:<br>Doorways to Infinite Ink's Website <nobr>& Beyond🚪</nobr>
Note that
htmlEscape
transformed
<
,
>
,
'
,
and
&
to their corresponding HTML named
or numeric
entities.
This is called
entity escaping.
To learn about
htmlEscape
,
see
gohugo.io/functions/htmlescape/.
{{ .Title | plainify | htmlUnescape }}
(my goal)Portals 🚪︎: Doorways to Infinite Ink's Website & Beyond🚪
Since
whitespace
does not matter in the <title>
element, this satisfies
my goal!
{{ .Title | plainify | htmlUnescape | urlquery }}
(sometimes used in share buttons)Portals%E2%80%8A%F0%9F%9A%AA%EF%B8%8E%3A%0ADoorways+to+Infinite%C2%A0Ink%27s+Website+%26+Beyond%F0%9F%9A%AA
This tranformation is useful for some share buttons
and links,
for example in the
“Share this page on Mastodon”
link
below.
To learn about
urlquery
,
see
gohugo.io/functions/urlquery/.
For more about Hugo, see Infinite Ink’s…
Hugo Shortcodes: Including Go Templates in Hugo Content Files
TGIH: Themeless & Gitless Introduction to the Hugo SSG (a tutorial)
🔗 Linkified Section Headings in Hugo-Generated Web Pages (featuring Markdown and AsciiDoc examples)
Variable and Parameter Names in Hugo (featuring camelCase🐫 and snake_case🐍)
Hugo’s .RenderString
Method (featuring AsciiDoc admonitions in Markdown and Go HTML)
Hugo’s Markup Languages: AsciiDoc, HTML, Markdown,
Configuring Security in Hugo (featuring settings needed to use Asciidoctor and Pandoc)🔒
A Way to Compare Hugo’s Markup Languages (featuring inline footnotes)📊
<nobr>
is deprecated, but, for now, I still use it.🤷'
and "
are also known as a dumb, neutral, straight, typewriter, or vertical quotation marks.@nm@mathstodon.xyz
or
#InfiniteInk
in it.