内容结构
在 Hugo 中可以通过 .md 来定义网页展示的页面内容,大致分为两类
single page: 单页文章,普通.md文档或index.md文档list page: 文章目录,修改_index.md文件
所有的 .md 有两部分组成:
Front Matter: 前置信息,定义了页面的元数据Content: 页面内容,使用Markdown格式编写
---
title: "我的文档"
date: 2026-07-31
tags: ["教程", "Hugo"]
---
这里是正文,用 Markdown 写。Front Matter
Front Matter 是文档的元数据
title: Example # page 显示的标题
description: Example # page 显示的概述
date: 2024-02-02T04:14:54-08:00 # 文档创建日期
draft: false # 是否是草稿,控制 hugo 是否 build
params: # 自定义参数
author: John Smith
weight: 10 # 用于在 page collection 中排序
keywords: # 文档的关键字
- key1
- key2其他保留字段
见官方文档。此外, Front Matter 支持 yaml、toml、json 格式
-
yaml: 使用---包裹MARKDOWN--- title: "我的文档" date: 2026-07-31 tags: ["教程", "Hugo"] --- 这里是正文,用 Markdown 写。--- title: "我的文档" date: 2026-07-31 tags: ["教程", "Hugo"] --- 这里是正文,用 Markdown 写。 -
toml: 使用+++包裹MARKDOWN+++ title = "我的文档" date = 2026-07-31 tags = ["教程", "Hugo"] +++ 这里是正文,用 Markdown 写。+++ title = "我的文档" date = 2026-07-31 tags = ["教程", "Hugo"] +++ 这里是正文,用 Markdown 写。 -
json: 使用{}包裹MARKDOWN{ "title": "我的文档", "date": "2026-07-31", "tags": ["教程", "Hugo"] } 这里是正文,用 Markdown 写。{ "title": "我的文档", "date": "2026-07-31", "tags": ["教程", "Hugo"] } 这里是正文,用 Markdown 写。
archetypes
作用
使用 hugo new <path> 可以快速地在 content/ 下创建自带 Front Matter 的 .md 文件。这些 Front Matter 就来自于 archetypes
demo/
└── archetypes/
└── default.md # 默认模板可通过修改 default.md 实现自定义默认的 Front Matter 。
定义
可以为 content type(top -level directory 文件夹名)定义单独的 archetypes 。hugo 会根据 <path> 路径匹配目标 content type。
-
单文本
TXTdemo/ └── archetypes/ ├── posts.md # posts 的 archetypes └── default.md # 默认模板demo/ └── archetypes/ ├── posts.md # posts 的 archetypes └── default.md # 默认模板 -
page bundlesTXTarchetypes/ ├── galleries/ <-- content type │ ├── images/ │ │ └── .gitkeep │ └── index.md <-- same format as default.md └── default.mdarchetypes/ ├── galleries/ <-- content type │ ├── images/ │ │ └── .gitkeep │ └── index.md <-- same format as default.md └── default.md
hugo new content posts/my-first-post.md 的 content type 是 posts,archetypes 的查找顺序为
archetypes/posts.mdthemes/my-theme/archetypes/posts.mdarchetypes/default.mdthemes/my-theme/archetypes/default.md
此外,也可以通过 --kind 参数指定 archetypes
hugo new content --kind galleries articles/something.mdShortcodes
作用
通过 Shortcodes
可以在 Markdown 中快速嵌入拓展内容,例如 youtube 视频、bilili 视频、x 帖子等。本质就是 hugo 会将 shortcodes 翻译成 html 代码, 例如 Details shortcode
See the details
This is a bold word.
See the details
This is a bold word.
会被编译为
<details>
<summary>See the details</summary>
<p>This is a <strong>bold</strong> word.</p>
</details>Param shortcode
通过 Param shortcode,可以在 .md 中引用 Front Matter 或配置文件中参数
---
title: Example
date: 2025-01-15T23:29:46-08:00
params:
color: red
size: medium
---
<!-- 引用 params.color 的值 -->
We found a \{\{% param "color" %\}\} shirt.