Hugo (Go)
Links
A static-site generator written in Golang.
Resources
Installing
|
|
|
|
|
|
Creating a new site
|
|
Structure
|
|
- archetypes/ - entities that are smth like markdown templates of content
- config.toml - main configuration file
- content/ - folder that contains markdown content
- data/ - folder that contains configuration files and data that used in generating your site
- layout/ - folder that contains html templates for specific parts of website
- static/ - folder that contains static files like images, fonts, etc.
- themes/ - folder that can contain existing Hugo themes
Starting website
|
|
To running and see draft content:
|
|
The empty white page in just created app is okay
On root route (/) Hugo try to render layout with name:
- list.html
- or
- index.html (index.html takes priority)
Types of pages:
- single (page of the specific post)
- list (page that contains list of posts)
Configuration file
By default it is config.toml
It is possible to use YAML file. Just rename it to config.yml
Default settings:
config.toml
|
|
config.yml
|
|
Templating
Create template of the main page in path /layouts/list.html
Add website title
|
|
.Site.Title:
- Site - context
- it is context of config.yml file
- Title - variable
Add page title
Create file /content/_index.md
|
|
- _index.md - file that contains content of home page (root route - ‘/')
Add title of home page in /layouts/list.html:
|
|
Because the layout is already in context of the page you can just change it:
|
|
to
|
|
Render list of pages in loop
Create new pages /content/about.md and /content/contact.md
|
|
To render list of these pages with their links just add this code in layouts/list.html:
|
|
- range is a loop here. it iterates in .Pages which is a list of data about each page
- .Permalink - link to the specific page
- .Title - the title of the page which is declared in markdown metadata
Default layouts
Create folder /layouts/_default and move layouts/list.html there
Layouts from this folder /layouts/_default will be used for all content by default
Creating template for a single page
Create file /layouts/_default/single.html:
|
|
If you open page /about in the browser you’ll see this html structure.
Display list of pages with specific type
Change the loop in the layout to display list of resources that have type “page”:
|
|
By default markdown resources in /content has type “page”.
If you don’t want to show some specific resource as page you can specify type in metadata:
|
|
Display list of pages/posts of specific section
What is Section?
More information you can find in official doc of Hugo
I understand sections like sub folders in a folder content
Basically, you have a folder content that contains two folders - blog and notes.
In that case, blog and notes are Sections in Hugo.
Display list of resources of section “blog”
|
|
With pagination
|
|
Splitting layouts
Let’s say we have two layouts: for home page layouts/_default/list.html and for single page layouts/_default/single.html
They have pretty similar html structure:
list.html
|
|
single.html
|
|
To avoid repeating code we can make base layout that will contain common html structure.
Create the file /layouts/_default/baseof.html
|
|
{{ block "content" . }}{{ end }}
renders layouts that have the same “name”
For home page let’s change /layouts/_default/list.html:
|
|
And now the baseof layout renders the sub layout list.html for home page
For single page let’s change /layouts/_default/single.html:
|
|
Creating blog section
Create folder /content/blog and put there new file /content/blog/_index.md
|
|
And then create a few blank posts:
|
|
|
|
Layout of posts list
To display list of posts let’s create new layout. But first of all, we need to create new folder /layouts/blog.
Why?
- /layouts/_default could used for general resources like pages
- /layouts/blog here are layouts for resources like blog posts
In /layouts/blog create file list.html:
|
|
It will display list of posts links
Layout of specific post
Let’s create a layout for page of post
In folder /layouts/blog create file single.html:
|
|
This html structure will be used only for posts. It won’t be used for all pages (for example about or contact).
Using partials templates
Partials templates are specific layouts or files that can be used as parts of layout.
Let’s say we have file /layouts/_default/baseof.html:
|
|
Here we have section head that we could make it as partial template.
Create folder /layouts/partials. It is a specific folder for all partials.
In that new folder let’s create file head.html with head section of html structure:
|
|
To use this partial change the file baseof.html:
|
|
Shortcodes
What Are Shortcodes?
More information here (gohugo.io)
Basically, it’s something similar to components in MDX but without React/JSX.
You can put a specific code in your markdown file and it will be rendered like a widget.
Built-in Shortcodes
There are several built-in shortcodes in Hugo that can be used out of box.
For example, shortcode youtube that renders a video on the page:
|
|
Taxonomies
Basically, taxonomies are tags and categories or some new thing that (you can make it) has similar behavior to tags or categories.
I take it that way:
- Sections: One-to-many
- one section (blog)
- many resources
- Taxonomies: Many-to-many
- many taxonomies (tags)
- many resources
Creating a new taxonomy
Let’s call it technologies
First of all, we need to specify new taxonomy in config file:
|
|
notice that we need to specify built-in taxonomies too if you want to use them
Add this in some of your markdown resources:
|
|
To render the list of technologies in each post:
|
|
To render the list of posts that has specific technology:
- file: /layouts/taxonomy/technology.html
- in the browser: /technologies/typescript
|
|
Tags
Tags in Hugo are built-in taxonomies.
Adding tags into the post
|
|
Creating a page with all tags of all resources (posts)
- file: /layouts/tag/list.html
- in the browser: /tags
|
|
Creating a page of specific tag with list of posts
- file: /layouts/taxonomy/tag.html
- in the browser: /tags/<tag_name>
|
|