Hugo 配置固定链接
Hugo 配置固定链接。
这是默认配置:
permalinks:
page: {}
section: {}
taxonomy: {}
term: {}
[permalinks]
[permalinks.page]
[permalinks.section]
[permalinks.taxonomy]
[permalinks.term]
{
"permalinks": {
"page": {},
"section": {},
"taxonomy": {},
"term": {}
}
}
为每个顶级栏目定义 URL 模式。每个 URL 模式可以针对给定的语言和/或页面类型。
url front matter 字段会覆盖任何匹配的固定链接模式。
单语言示例
使用此内容结构:
content/
├── posts/
│ ├── bash-in-slow-motion.md
│ └── tls-in-a-nutshell.md
├── tutorials/
│ ├── git-for-beginners.md
│ └── javascript-bundling-with-hugo.md
└── _index.md在 “training” 下渲染教程,在 “articles” 下渲染帖子,并使用基于日期的层次结构:
permalinks:
page:
posts: /articles/:year/:month/:slug/
tutorials: /training/:slug/
section:
posts: /articles/
tutorials: /training/
[permalinks]
[permalinks.page]
posts = '/articles/:year/:month/:slug/'
tutorials = '/training/:slug/'
[permalinks.section]
posts = '/articles/'
tutorials = '/training/'
{
"permalinks": {
"page": {
"posts": "/articles/:year/:month/:slug/",
"tutorials": "/training/:slug/"
},
"section": {
"posts": "/articles/",
"tutorials": "/training/"
}
}
}
发布站点的结构将是:
public/
├── articles/
│ ├── 2023/
│ │ ├── 04/
│ │ │ └── bash-in-slow-motion/
│ │ │ └── index.html
│ │ └── 06/
│ │ └── tls-in-a-nutshell/
│ │ └── index.html
│ └── index.html
├── training/
│ ├── git-for-beginners/
│ │ └── index.html
│ ├── javascript-bundling-with-hugo/
│ │ └── index.html
│ └── index.html
└── index.html要为内容根目录中的常规页面创建基于日期的层次结构:
permalinks:
page:
/: /:year/:month/:slug/
[permalinks]
[permalinks.page]
'/' = '/:year/:month/:slug/'
{
"permalinks": {
"page": {
"/": "/:year/:month/:slug/"
}
}
}
对分类术语使用相同的方法。例如,要省略 URL 的分类段:
permalinks:
term:
tags: /:slug/
[permalinks]
[permalinks.term]
tags = '/:slug/'
{
"permalinks": {
"term": {
"tags": "/:slug/"
}
}
}
多语言示例
将 permalinks 配置用作本地化策略的组成部分。
使用此内容结构:
content/
├── en/
│ ├── books/
│ │ ├── les-miserables.md
│ │ └── the-hunchback-of-notre-dame.md
│ └── _index.md
└── es/
├── books/
│ ├── les-miserables.md
│ └── the-hunchback-of-notre-dame.md
└── _index.md使用此站点配置:
defaultContentLanguage: en
defaultContentLanguageInSubdir: true
languages:
en:
contentDir: content/en
languageCode: en-US
languageDirection: ltr
languageName: English
permalinks:
page:
books: /books/:slug/
section:
books: /books/
weight: 1
es:
contentDir: content/es
languageCode: es-ES
languageDirection: ltr
languageName: Español
permalinks:
page:
books: /libros/:slug/
section:
books: /libros/
weight: 2
defaultContentLanguage = 'en'
defaultContentLanguageInSubdir = true
[languages]
[languages.en]
contentDir = 'content/en'
languageCode = 'en-US'
languageDirection = 'ltr'
languageName = 'English'
weight = 1
[languages.en.permalinks]
[languages.en.permalinks.page]
books = '/books/:slug/'
[languages.en.permalinks.section]
books = '/books/'
[languages.es]
contentDir = 'content/es'
languageCode = 'es-ES'
languageDirection = 'ltr'
languageName = 'Español'
weight = 2
[languages.es.permalinks]
[languages.es.permalinks.page]
books = '/libros/:slug/'
[languages.es.permalinks.section]
books = '/libros/'
{
"defaultContentLanguage": "en",
"defaultContentLanguageInSubdir": true,
"languages": {
"en": {
"contentDir": "content/en",
"languageCode": "en-US",
"languageDirection": "ltr",
"languageName": "English",
"permalinks": {
"page": {
"books": "/books/:slug/"
},
"section": {
"books": "/books/"
}
},
"weight": 1
},
"es": {
"contentDir": "content/es",
"languageCode": "es-ES",
"languageDirection": "ltr",
"languageName": "Español",
"permalinks": {
"page": {
"books": "/libros/:slug/"
},
"section": {
"books": "/libros/"
}
},
"weight": 2
}
}
}
发布站点的结构将是:
public/
├── en/
│ ├── books/
│ │ ├── les-miserables/
│ │ │ └── index.html
│ │ ├── the-hunchback-of-notre-dame/
│ │ │ └── index.html
│ │ └── index.html
│ └── index.html
├── es/
│ ├── libros/
│ │ ├── les-miserables/
│ │ │ └── index.html
│ │ ├── the-hunchback-of-notre-dame/
│ │ │ └── index.html
│ │ └── index.html
│ └── index.html
└── index.html令牌
在定义 URL 模式时使用这些令牌。
:year- front matter
date字段中定义的 4 位年份。 :month- front matter
date字段中定义的 2 位月份。 :monthname- front matter
date字段中定义的月份名称。 :day- front matter
date字段中定义的 2 位日期。 :weekday- front matter
date字段中定义的 1 位星期几(星期日 =0)。 :weekdayname- front matter
date字段中定义的星期几名称。 :yearday- front matter
date字段中定义的 1 至 3 位一年中的第几天。 :section- 内容的部分。
:sectionslug- New in v0.149.0
- 使用 slugified 部分名称的内容部分。slugified 部分名称是 front matter 中定义的
slug,否则是 front matter 中定义的title,否则是自动标题。 :sections- 内容的部分层次结构。您可以使用 slice 语法 选择部分:
:sections[1:]包括除第一个之外的所有部分,:sections[:last]包括除最后一个之外的所有部分,:sections[last]仅包括最后一个,:sections[1:2]包括第 2 和第 3 部分。请注意,此 slice 访问不会抛出任何越界错误,因此您不必精确。 :sectionslugs- New in v0.149.0
- 使用 slugified 部分名称的内容部分层次结构。slugified 部分名称是 front matter 中定义的
slug,否则是 front matter 中定义的title,否则是自动标题。您可以使用 slice 语法 选择部分::sectionslugs[1:]包括除第一个之外的所有部分,:sectionslugs[:last]包括除最后一个之外的所有部分,:sectionslugs[last]仅包括最后一个,:sectionslugs[1:2]包括第 2 和第 3 部分。请注意,此 slice 访问不会抛出任何越界错误,因此您不必精确。 :title- front matter 中定义的
title,否则是自动标题。Hugo 为没有文件支持的部分、分类法和术语页面自动生成标题。 :slug- front matter 中定义的
slug,否则是 front matter 中定义的title,否则是自动标题。Hugo 为没有文件支持的部分、分类法和术语页面自动生成标题。 :filename- 内容的文件名(不含扩展名),适用于
page页面类型。Deprecated in v0.144.0
:filename标记已弃用。请改用:contentbasename。 :slugorfilename- front matter 中定义的
slug,否则是内容的文件名(不含扩展名),适用于page页面类型。Deprecated in v0.144.0
:slugorfilename标记已弃用。请改用:slugorcontentbasename。 :contentbasename- New in v0.144.0
- 内容基本名称。
:slugorcontentbasename- New in v0.144.0
- front matter 中定义的
slug,否则是 内容基本名称。
对于时间相关的值,您还可以使用 Go time 包 中定义的布局字符串组件。例如:
permalinks:
posts: /:06/:1/:2/:title/
[permalinks]
posts = '/:06/:1/:2/:title/'
{
"permalinks": {
"posts": "/:06/:1/:2/:title/"
}
}