はじめに

Hugoで長く記事を書いてきたが、新しく記事を作成した際のテンプレートについて全く手を入れておらず、毎回似たような記事のセクションをコピペしたりしていた。
流石に効率が悪いなとやっと気づいたので、テンプレートをカスタマイズをしてみる。

環境

1
2
3
Mac OS Sonoma 14.5
Docker Desktop 4.30.0 (149282)
hugo v0.122.0+extended linux/amd64 BuildDate=unknown

実現方法

Hugoの公式ページを見るとテンプレートはカスタマイズできるようだ。

Hugo looks for archetypes in the archetypes directory in the root of your project, falling back to the archetypes directory in themes or installed modules. An archetype for a specific content type takes precedence over the default archetype.

とある。

以下のような検索順序でテンプレートになる.mdファイルを探してくれるようだ。

The archetype lookup order is:

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

全てない場合は組み込みのデフォルトのテンプレートになるようだ。

現状のテンプレート

archetypes/posts.md

なし。

archetypes/default.md

1
2
3
4
5
---
title: "{{ replace .Name "-" " " | title }}"
date: {{ .Date }}
draft: true
---

themes/jane/archetypes/posts.md

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
---
title: "{{ replace .TranslationBaseName "-" " " | title }}"
date: {{ .Date }}
lastmod: {{ .Date }}
draft: true
keywords: []
description: ""
tags: []
categories: []
author: ""

# Uncomment to pin article to front page
# weight: 1
# You can also close(false) or open(true) something for this content.
# P.S. comment can only be closed
comment: false
toc: false
autoCollapseToc: false
# You can also define another contentCopyright. e.g. contentCopyright: "This is another copyright."
contentCopyright: false
reward: false
mathjax: false

# Uncomment to add to the homepage's dropdown menu; weight = order of article
# menu:
#   main:
#     parent: "docs"
#     weight: 1
---

<!--more-->

themes/jane/archetypes/default.md

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
---
title: "{{ replace .TranslationBaseName "-" " " | title }}"
date: {{ .Date }}
lastmod: {{ .Date }}
draft: true
keywords: []
description: ""
tags: []
categories: []
author: ""

# Uncomment to pin article to front page
# weight: 1
# You can also close(false) or open(true) something for this content.
# P.S. comment can only be closed
comment: false
toc: false
autoCollapseToc: false
# You can also define another contentCopyright. e.g. contentCopyright: "This is another copyright."
contentCopyright: false
reward: false
mathjax: false

# Uncomment to add to the homepage's dropdown menu; weight = order of article
# menu:
#   main:
#     parent: "docs"
#     weight: 1
---

<!--more-->

独自のテンプレートを作成する

今回は、archetypes/posts.md archetypes/post.md に定義する。
下記のように定義した。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
---
title: "{{ replace .TranslationBaseName "-" " " | title }}"
date: {{ .Date }}
lastmod: {{ .Date }}
draft: true
keywords: []
description: ""
tags: []
categories: []
author: "kbushi"

# Uncomment to pin article to front page
# weight: 1
# You can also close(false) or open(true) something for this content.
# P.S. comment can only be closed
comment: false
toc: true
autoCollapseToc: true
# You can also define another contentCopyright. e.g. contentCopyright: "This is another copyright."
contentCopyright: false
reward: false
mathjax: false

# Uncomment to add to the homepage's dropdown menu; weight = order of article
# menu:
#   main:
#     parent: "docs"
#     weight: 1
---

<!--more-->

## はじめに

## 環境

## 参考

## おわりに

定義後に、hugo newを実行するとこのテンプレートで記事が作成される。

躓いた箇所

今回、archetypes/posts.mdが検索順序として最初に参照されるが、このファイルを作成してもテンプレートとして反映されなかった。
これは、自分の環境のディレクトリ構造として、content/post以下が記事ディレクトリとなっているためである。
そのため、archetypes/posts.md ではなく、archetypes/post.md として定義する必要があった。
archetypesには気をつけたい。

ChatGPTに聞いた時の会話ログのリンクも貼っておく。
https://chatgpt.com/share/296d875c-d9ba-4419-9914-d340347ff993

※ChatGPTがなかったら、結構な時間を取られていたな~.時代の進歩に感謝。

参考

おわりに

Hugoで記事を作成するのは週に数回行っているので、これで少しは記事を書く時間が効率化できたと思う。
今後も改善箇所があれば積極的に導入していこう。