You Should Be Using Hugo Archetypes
I hate repetitve tasks. They make me bored and vulernable to human error. If I can find a solution to automate repetitive tasks, I will.
If you stop being boring and repetitive, that will be the end of it - I will not look for you, I will not pursue you… but if you don’t, I will look for you, I will find you… and I will automate you. – Liam Neeson, probably
I noticed two reptitive tasks I did before publishing a blog post:
- Append “Let me know what you think on Twitter!” to the end
- Copy+Paste the front matter from prior blog posts
These were trivial tasks, but I wanted to focus on writing content. I needed them complete the moment I created a new blog post.
I created this site using Hugo. I got started by reading the Quick Start guide and skipped the rest of the documentation. I decided to take a learn-as-you-go approach. If I found something I didn’t understand, I searched the documentation or did a Google search.
That’s how I found archetypes. Archetypes are content template files in the archetypes
directory of my project. Archetypes allow me to preconfigure front matter and define a common page structure. It was the perfect solution for my problem.
My project’s archetypes
directory already contained a single file, default.md
. It’s what preconfigured the front matter with title
, date
, and draft
to all content files. But I didn’t want to edit this file because it would affect all new content on my site. I only cared for blog posts.
Hugo uses content-section
to find the most suitable archetype template. The content-section
is posts
for my blog posts, so I needed a posts.md
archetype.
If I didn’t have a posts.md
archetype template, Hugo would use the first archetype file found of these:
- archetypes/default.md
- themes/my-theme/archetypes/posts.md
- themes/my-theme/archetypes/default.md
Here’s how my posts.md
archetype looks like:
---
title: "{{ replace .Name "-" " " | title }}"
date: {{ .Date }}
draft: true
description: "Enter Description"
tags:
- daily writing
---
---
Thanks for reading! Let me know what you think of this post on [Twitter](https://twitter.com/maupanelo).
Now my posts are populated with the front matter I prefer and my repeated elements are added by default.
Thanks for reading! Let me know what you think of this post on Twitter.