HUGO
Menu
GitHub 87548 stars Mastodon

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/"
   }
}

Last updated: January 1, 0001
Improve this page