Hugo Tinkering Record: From 0 to 1
Hugo Installation
Official Documentation Tutorial
The official website has 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 it.
brew install hugo
Note: This installation method may not install the latest version of Hugo. If you want to use the latest version of Hugo, please use other installation methods.
Configuration File Format
As a newbie, choosing between yaml, toml, and json for the configuration file format was my first question.
TOML
- Case-sensitive
- Files can only contain UTF-8 encoded Unicode characters
- Insensitive to indentation
YAML
- Files start with ' - ', marking the beginning of the document
- Key-value pairs are separated by colons
- Lists start with hyphens
- Use indentation with one or more spaces to describe nested collections
JSON
- Data is stored in name/value pairs
- Records are separated by commas. Trailing commas without the following attributes are not allowed
- Double quotes wrap 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"
}
Due to the troublesome comment form of JSON and the strict indentation form of YAML, I finally chose TOML as the configuration file format. I can also indent freely, which is convenient for me to view.
Scattered Knowledge Points
- The symbol
:=is an assignment operator in the Python language (mainly called the walrus operator). The walrus operator compresses our code to make it shorter.
var a; //Definition
a = 1; //Assignment
layout/baseof.htmlis the base of all pages- You can divide
header,footer, etc. into parts in thelayout/partialsfolder - Steps to add a new page:
- You need to add a
.mdfile incontent - Add a page with the same name in
layouts/_default - Menu activation
activestate code: - The method described in the official website instructions does not successfully add the
activestate, 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 }}
- Small knowledge point of
positionfunction in css!
.outer {
position: relative;
}
.inner {
position: absolute;
top: 0px;
left: 0px;
}
Shortcodecan shorten the code and write some methods. Similar tofunction, please refer to the writing method: Hugo-basic