はじめに
Hugoで長く記事を書いてきたが、新しく記事を作成した際のテンプレートについて全く手を入れておらず、毎回似たような記事のセクションをコピペしたりしていた。
流石に効率が悪いなとやっと気づいたので、テンプレートをカスタマイズをしてみる。
環境
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:
- archetypes/posts.md
- archetypes/default.md
- themes/my-theme/archetypes/posts.md
- themes/my-theme/archetypes/default.md
全てない場合は組み込みのデフォルトのテンプレートになるようだ。
現状のテンプレート
archetypes/posts.md
なし。
archetypes/default.md
---
title: "{{ replace .Name "-" " " | title }}"
date: {{ .Date }}
draft: true
---themes/jane/archetypes/posts.md
---
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
---
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.mdarchetypes/post.md に定義する。
下記のように定義した。
---
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 new 実行時に生成されるデフォルトテンプレートをカスタマイズする
https://yonehub.y10e.com/2019/12/19/20191218_hugo_newpost/
おわりに
Hugoで記事を作成するのは週に数回行っているので、これで少しは記事を書く時間が効率化できたと思う。
今後も改善箇所があれば積極的に導入していこう。