Hugo

Hugo releated notes. Current take some notes when reading official docuemts

Reading documents

目录结构

.
├── archetypes
├── config.toml
├── content
├── data
├── layouts
├── static
└── themes

archetypes: 用于hugo new,Lookup Order for Archetypes:

  • archetypes/posts.md
  • archetypes/default.md
  • themes//archetypes/posts.md
  • themes//archetypes/default.md (Auto-generated with hugo new site)

配置

文档组织

Hugo believes that you organize your content with a purpose. The same structure that works to organize your source content is used to organize the rendered site.

  • content/[section]/[post-title.md] 对应 [website-url]/[section]/[post-title]/
  • 每个section都可以有一个_index.md,不是十分明白,似乎可以自定义section主页的内容?
  • .md头部添加 slug: [new-title] 可以定义新的链接地址,对应 [website-url]/[section]/[new-title]/
  • url: /new/url 对应 [website-url]/new/url

扉页 - Front Matter

Front Matter Variables

模板

Base Template Lookup Order

  • /layouts/section/<TYPE>-baseof.html
  • /themes/<THEME>/layouts/section/<TYPE>-baseof.html
  • /layouts/<TYPE>/baseof.html
  • /themes/<THEME>/layouts/<TYPE>/baseof.html
  • /layouts/section/baseof.html
  • /themes/<THEME>/layouts/section/baseof.html
  • /layouts/_default/<TYPE>-baseof.html
  • /themes/<THEME>/layouts/_default/<TYPE>-baseof.html
  • /layouts/_default/baseof.html
  • /themes/<THEME>/layouts/_default/baseof.html

这意味着,对于每一个section都可以有自己独立的模板。我可以为笔记,项目展示提供不同的网页样式。

Hugo 的模板函数:

路径

{{ "mystyle.css" | absURL  }} → "https://example.com/hugo/mystyle.css"
{{ "mystyle.css" | relURL  }} → "/hugo/mystyle.css"

Filter posts

{{ if or ( eq .Kind "page" ) ( eq .Kind "section" ) }}
    {{ $.Scratch.Set "posts" (where .Site.RegularPages "Section" .Section) }}
{{ else if eq .Kind "home" }}
    {{ $.Scratch.Set "posts" (where .Site.RegularPages "Section" "post") }}
{{ else }}
    {{ $.Scratch.Set "posts" .Data.Pages }}
{{ end }}

{{ $posts := $.Scratch.Get "posts" }}

IsMenuCurrent

This function has strict request for config.toml, the example below will works.

[[menu.main]]
    name = "Post"
    url = "/"
    weight = 1

[[menu.main]]
    name = "Note"
    url = "/note/"
    weight = 2