Adding Hugo Aliases and Templates
Sat, Jun 19, 2021

Recently I added some command line aliases to more quickly add new posts to Hugo, the static site generator that powers this blog. So for example I can type hnp New Post and it will run hugo new --kind post-bundle posts/2021-06-19-new-post. Then using Hugo’s archetypes it will create a new blog post in the format I like, with the date prefixing the folder name. I used a bash function instead of a true alias to allow me to write out the titles in plain english.

Here is the full function:

hnp() {
  title=`echo "$@"`
  slug=`echo $title | sed 's/ /-/g' | tr '[:upper:]' '[:lower:]'`
  hugo new --kind post-bundle posts/`date +"%Y-%m-%d"`-$slug 
}

Then there was setting up Hugo’s archetypes to set the title properly. Unfortunatly Hugo’s archetypes or the hugo new command do not support input parmeters as far as I can tell. The title of the post will always be the path of the post, so my titles were looking something like “2021 06 19 New Post”. I could of course edit it each time, but that would be tedious. So what I ended up doing was using the Replace function to remove the current date from the title. So my archetype looks like this:

---
{{- $day := now.Format "2006-01-02-"}}
title:  "{{ replace .Name $day ""  | humanize | title }}"
date: {{ .Date }}
authors: ["Ernst Rullmann"]
tags: []
---

Notice that we use now.Format instead of .Date.Format? That’s because in this context .Date is a string, not an actual date object. These sorts of inconsistancies can make Hugo really frustrating to use. I’ve figured out most workflows now, but I keep feeling like I’m making compromises to get it to work. I hope to keep seeing improvements with future Hugo updates.