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 裡面支援三種格式:TOMLYAMLJSON

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)項目

常見項目

  1. title
    文章標題。

  2. date
    發文日期,在 TOML 中,date日期/時間值可以使用不帶"引號的字串來表示,這與 YAML 和 JSON 不同,後兩者通常要求使用帶引號的字符串來表示日期/時間值。

  3. draft
    預設的草稿文章,如果沒有更改的話即使使用hugo指令之後也不會出現在網站上,新建立文章都會有此預設內容,待完善post內容後可以刪除,或是把true改成false。如果需要發布草稿文章,可以執行 hugo -D 或是hugo --buildDrafts

    發布草稿文章方式:
    方式1:

    hugo -D
    

    方式2:

    hugo --buildDrafts
    
  4. author
    文章的作者。

  5. description
    (string)與頁面摘要在概念上不同,description 通常會在已發佈的 HTML 文件中的 head 標籤內作為 meta 元素呈現,可以在page模板中使用 description 方法來訪問這個字串。

  6. tags
    文章標籤,使用[ "example","tag" ]表示。hugo 將為每個 tags 顯示一個列表頁面,通常可以通過 yourdomain.com/tags/ 的 URL 組合來訪問。例如,yourdomain.com/tags/ 會顯示所有文章標籤及其相關內容。

  7. categories
    文章分類,使用[ "example","categories" ]表示。hugo 將為每個 categories 顯示一個列表頁面,通常可以通過 yourdomain.com/categories/ 的 URL 組合來訪問。例如,yourdomain.com/categories/ 會顯示所有文章分類及其相關內容。

  8. isCJKLanguage
    (bool)如果內容語言屬於 CJK 語系(中日韓語系),設為 true。此值決定 hugo 如何計算字數。

  9. toc
    文章大綱,Table of content,取決於theme是否支援,有兩種方法設定,一是全域global toc parameter,二是單篇文章post toc parameter,單篇文章post toc 優先程度會高於全域global toc

    要啟用在全部文章(globally)的話,需要在hugo.toml中設定toctrue

     [params]
     toc = true
    

    要關掉的話,設定為false即可。

    [params]
    toc = false
    

    在單篇文章中啟用toc,需要在文章標頭中設定toctrue

    +++
    title = "New Post"
    date = 2024-01-01T12:00:00+09:00
    draft = true
    toc = true
    +++
    

    在單篇文章中取消toc,則在文章標頭中設定tocfalse

    +++
    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
}