Hugo Tinkering Log: From 0 to 1
Hugo Installation
Official Documentation Tutorial
The official website provides detailed installation tutorials for different systems: https://gohugo.io/installation/
Brew
If you are a Mac user, you can use the brew command to install.
brew install hugoNote: This installation method may not install the latest version of hugo. If you want to use the latest version of hugo, please use other methods.
Configuration File Format
As a novice, deciding whether to choose yaml, toml, or json for the configuration file format was my first question.
TOML
- Case sensitive
- Files must contain only UTF-8 encoded Unicode characters
- Indentation insensitive
YAML
- Files start with ' - ' to mark the beginning of the document
- Key-value pairs are separated by colons
- Lists start with a hyphen
- Nested collections are described using indentation with one or more spaces
JSON
- Data is stored in name/value pairs
- Records are separated by commas. Trailing commas without the following properties are not allowed
- Double quotes enclose property names and strings
- Single quotes are not allowed
String Differences
All formats support Strings, but JSON does not support multiline strings, nor does it support comments.
# TOML
key = "String Value"
multiline = """\
The quick brown \
fox jumps over \
the lazy dog.\
"""# YAML
key : String Value
multilinePreservedLinebreaks:
|
L1 - The quick brown
L2 - fox jumps over
L3 - the lazy dog.
multilineReplaceLinebreaksWithWhitespace:
>
This sentence ist just too long to keep it
on the same line.// JSON
{
"key": "String Value"
}Because of the troublesome comment format in JSON and the strict indentation format in YAML, I ultimately chose TOML as the configuration file format, which also allows me to indent freely, making it easier for me to view.
Miscellaneous Knowledge Points
- This symbol
:=is the assignment operator in the Python language (primarily called the Walrus Operator), and the Walrus Operator compacts our code to make it shorter.
var a; // Definition
a = 1; // Assignmentlayout/baseof.htmlis the foundation for all pages- The
layout/partialsfolder can separate sections likeheader,footer, etc. - Steps for adding a new page:
- You need to add an
.mdfile incontent - Add a page with the same name in
layouts/_default - Menu activation
activestatus code: - The method described in the official documentation did not successfully add the
activestatus, so I looked for other ways to determineactive
{{ range .Site.Menus.main }}
{{- $menu_item_url := (cond (strings.HasSuffix .URL "/") .URL (printf "%s/" .URL) ) | absLangURL }}
{{- $page_url:= $currentPage.Permalink | absLangURL }}
<li>
<a {{- if eq $menu_item_url $page_url }} class="active" {{- end }} href="{{ .URL }}">
{{ .Pre }}
<span>{{ .Name }}</span>
</a>
</li>
{{ end }}- A minor knowledge point about the
positionfeature in CSS!
.outer {
position: relative;
}
.inner {
position: absolute;
top: 0px;
left: 0px;
}Shortcodecan shorten code and write methods. Similar to afunction, refer to the following for specific usage: Hugo-basic