CONTENT MANAGEMENT

Archetypes

概述

一個內容檔案由 前置內容 和標記語言組成。標記語言通常是 Markdown,但 Hugo 也支持其他 內容格式。前置內容可以是 TOML、YAML 或 JSON。

hugo new content 命令會在 content 目錄中創建一個新檔案,並使用原型作為範本。這是預設的原型:

archetypes/default.md
     
---
date: '{{ .Date }}'
draft: true
title: '{{ replace .File.ContentBaseName `-` ` ` | title }}'
---
+++
date = '{{ .Date }}'
draft = true
title = '{{ replace .File.ContentBaseName `-` ` ` | title }}'
+++
{
   "date": "{{ .Date }}",
   "draft": true,
   "title": "{{ replace .File.ContentBaseName `-` ` ` | title }}"
}

當您創建新內容時,Hugo 會評估原型中的 範本操作。例如:

hugo new content posts/my-first-post.md

使用上面顯示的預設原型,Hugo 創建了以下內容檔案:

content/posts/my-first-post.md
     
---
date: "2023-08-24T11:49:46-07:00"
draft: true
title: My First Post
---
+++
date = '2023-08-24T11:49:46-07:00'
draft = true
title = 'My First Post'
+++
{
   "date": "2023-08-24T11:49:46-07:00",
   "draft": true,
   "title": "My First Post"
}

您可以為一個或多個 內容類型 創建原型。例如,對於文章使用一個原型,對於其他所有內容使用預設原型:

archetypes/
├── default.md
└── posts.md

查找順序

Hugo 會在專案根目錄中的 archetypes 目錄尋找原型,若找不到會回退到主題或已安裝模組中的 archetypes 目錄。特定內容類型的原型會優先於預設原型。

例如,使用以下命令:

hugo new content posts/my-first-post.md

原型查找順序為:

  1. archetypes/posts.md
  2. archetypes/default.md
  3. themes/my-theme/archetypes/posts.md
  4. themes/my-theme/archetypes/default.md

如果這些都不存在,Hugo 會使用內建的預設原型。

函數與上下文

您可以在原型中使用任何 範本函數。如上所示,預設原型使用 replace 函數,將標題中的連字符替換為空格。

原型接收以下 上下文

Date
(string) 當前日期和時間,符合 RFC3339 格式。
File
(hugolib.fileInfo) 返回當前頁面的檔案信息。請參閱 詳細資料
Type
(string) 從頂層目錄名稱推斷的 內容類型,或透過 hugo new content 命令中的 --kind 參數指定的內容類型。
Site
(page.Site) 當前的網站物件。請參閱 詳細資料

日期格式

若要以不同格式插入日期和時間,可以使用 time.Now 函數:

archetypes/default.md
     
---
date: '{{ time.Now.Format "2006-01-02" }}'
draft: true
title: '{{ replace .File.ContentBaseName `-` ` ` | title }}'
---
+++
date = '{{ time.Now.Format "2006-01-02" }}'
draft = true
title = '{{ replace .File.ContentBaseName `-` ` ` | title }}'
+++
{
   "date": "{{ time.Now.Format \"2006-01-02\" }}",
   "draft": true,
   "title": "{{ replace .File.ContentBaseName `-` ` ` | title }}"
}

包含內容

儘管通常用作前置內容範本,您也可以使用原型來填充內容。

例如,在一個文檔網站中,您可能會有一個函數(內容類型)部分。此部分中的每個頁面應遵循相同的格式:簡短的描述、函數簽名、示例和備註。我們可以提前填充頁面,提醒內容作者遵守標準格式。

archetypes/functions.md
---
date: '{{ .Date }}'
draft: true
title: '{{ replace .File.ContentBaseName `-` ` ` | title }}'
---

簡短描述此函數所執行的操作,使用簡單現在時並以第三人稱單數形式表達。例如:

`someFunction` 會返回字串 `s` 重複 `n` 次。

## 簽名

```text
func someFunction(s string, n int) string
```

## 示例

一個或多個實際示例,每個示例都放在圍繞的程式碼區塊中。

## 備註

任何需要說明的附加信息。

雖然您可以在內容主體中包含 範本操作,但請記住,Hugo 只會在創建內容時執行這些操作。在大多數情況下,請將範本操作放在 範本 中,讓 Hugo 在每次 建立 網站時都評估這些操作。

頁面包

您也可以為 頁面包 創建原型。

例如,在一個攝影網站中,您可能會有一個畫廊的部分(內容類型)。每個畫廊都是包含內容和圖片的頁面包。

為畫廊創建一個原型:

archetypes/
├── galleries/
│   ├── images/
│   │   └── .gitkeep
│   └── index.md      <-- 與 default.md 相同格式
└── default.md

原型中的子目錄必須包含至少一個檔案。若沒有檔案,Hugo 在創建新內容時不會創建子目錄。檔案的名稱和大小無關。上面的範例包含了 .gitkeep 檔案,一個常用於 Git 存儲庫中保留空目錄的空檔案。

若要創建新的畫廊:

hugo new galleries/bryce-canyon

這將產生:

content/
├── galleries/
│   └── bryce-canyon/
│       ├── images/
│       │   └── .gitkeep
│       └── index.md
└── _index.md

指定原型

使用 --kind 命令行標誌來指定創建內容時所使用的原型。

例如,假設您的網站有兩個部分:文章和教程。為每個內容類型創建一個原型:

archetypes/
├── articles.md
├── default.md
└── tutorials.md

若要使用文章原型創建文章:

hugo new content articles/something.md

若要使用教程原型創建文章:

hugo new content --kind tutorials articles/something.md