hugo新文章設定
大綱
添加一篇文章
首先先在terminal
中啟動hugo
:
hugo server
建立預設會在posts
資料夾下名為my-first-post.md
的檔案:
hugo new posts/my-first-post.md
打開文章檔案之後會看到以下內容:
+++
title = 'New Post'
date = 2024-01-01T12:00:00+09:00
draft = true
+++
或是
---
title: "New Post"
date: "2024-01-01T12:00:00+09:00"
draft = true
---
兩者之間的差異在下面說明。
hugo支援標頭(front matter)格式
hugo 裡面支援三種格式:TOML
、YAML
和 JSON
1. TOML
使用 +++
做為開頭跟結尾,項目中間使用=
做區隔:
+++
title = "New Post"
date = 2024-01-01T12:00:00+09:00
draft = true
+++
2. YAML
使用 ---
做為開頭跟結尾,項目中間使用:
做區隔:
---
title: "New Post"
date: "2024-01-01T12:00:00+09:00"
draft: true
---
3. JSON
使用 {
以及}
做為開頭跟結尾,項目中間使用:
做區隔:
{
"title": "New Post",
"date": "2024-01-01T12:00:00+09:00",
"draft": true
}
hugo支援標頭(front matter)項目
常見項目
-
title
文章標題。 -
date
發文日期,在TOML
中,date
日期/時間值可以使用不帶"
引號的字串來表示,這與 YAML 和 JSON 不同,後兩者通常要求使用帶引號的字符串來表示日期/時間值。 -
draft
預設的草稿文章,如果沒有更改的話即使使用hugo
指令之後也不會出現在網站上,新建立文章都會有此預設內容,待完善post內容後可以刪除,或是把true
改成false
。如果需要發布草稿文章,可以執行hugo -D
或是hugo --buildDrafts
。發布草稿文章方式:
方式1:hugo -D
方式2:
hugo --buildDrafts
-
author
文章的作者。 -
description
(string
)與頁面摘要在概念上不同,description
通常會在已發佈的 HTML 文件中的head
標籤內作為meta
元素呈現,可以在page
模板中使用 description 方法來訪問這個字串。 -
tags
文章標籤,使用[ "example","tag" ]
表示。hugo 將為每個tags
顯示一個列表頁面,通常可以通過yourdomain.com/tags/
的 URL 組合來訪問。例如,yourdomain.com/tags/
會顯示所有文章標籤及其相關內容。 -
categories
文章分類,使用[ "example","categories" ]
表示。hugo 將為每個categories
顯示一個列表頁面,通常可以通過yourdomain.com/categories/
的 URL 組合來訪問。例如,yourdomain.com/categories/
會顯示所有文章分類及其相關內容。 -
isCJKLanguage
(bool
)如果內容語言屬於CJK
語系(中日韓語系),設為 true。此值決定 hugo 如何計算字數。 -
toc
文章大綱,Table of content,取決於theme是否支援,有兩種方法設定,一是全域globaltoc
parameter,二是單篇文章posttoc
parameter,單篇文章posttoc
優先程度會高於全域globaltoc
。要啟用在全部文章(globally)的話,需要在
hugo.toml
中設定toc
為true
。[params] toc = true
要關掉的話,設定為false即可。
[params] toc = false
在單篇文章中啟用
toc
,需要在文章標頭中設定toc
為true
:+++ title = "New Post" date = 2024-01-01T12:00:00+09:00 draft = true toc = true +++
在單篇文章中取消
toc
,則在文章標頭中設定toc
為false
:+++ title = "New Post" date = 2024-01-01T12:00:00+09:00 draft = true toc = false +++
設定文章標頭(front matter)模板
文章標頭的模板路徑
/archetypes/default.md
hugo 預設會先從根目錄的 archetypes/
文件夾中查找 default.md
。如果在根目錄中有相應的 archetypes/default.md
,它會優先使用這些文件,而不使用themes
中的 archetypes。 • 如果根目錄中沒有相應的 archetypes/default.md
文件,hugo 才會回退到使用主題themes/your-theme/archetypes/default.md
中的模板。
內容
+++
title = 'New Post'
date = 2024-01-01T12:00:00+09:00
draft = true
+++
如上描述有很多內容可以增加,讓未來新增文章時能夠都有完整的內容。 例如:
TOML
+++
title = '{{ replace .File.ContentBaseName "-" " " | title }}'
date = {{ .Date }}
draft = true
author = "Lucas"
description = ""
tags = ["",""]
categories = ["",""]
isCJKLanguage = true
toc = true
+++
YAML
---
title: "{{ replace .File.ContentBaseName "-" " " | title }}"
date: {{ .Date }}
draft: true
author: "Lucas"
description: ""
tags: ["",""]
categories: ["",""]
isCJKLanguage: true
toc: true
---
JSON
{
"title": "{{ replace .File.ContentBaseName \"-\" \" \" | title }}",
"date": "{{ .Date }}",
"draft": true,
"author": "Lucas",
"description": "",
"tags": ["", ""],
"categories": ["", ""],
"isCJKLanguage": true,
"toc": true
}