The Bruised Edge

Experiments in software, media, craft, and life

hugo-new <section> <title-slug>

I really think this bilberry theme is the perfect blog theme for me. It has very useful content types for the variety of things I imagine I’ll want to post about, and is pretty simple in its design and architecture.

The one thing I don’t like about Hugo, though, is the need to take two steps to create and edit a new post. So, I wrote a little Bash script that combines both steps into one (which I have put on my system’s $PATH at /usr/local/bin/hugo-new).

#!/usr/bin/env bash
set -euo pipefail

die_usage() {
  printf 'Usage: %s <section> <title-slug>\n' "${0##*/}" >&2
  printf '  <section> must be one of: article, audio, code, gallery, link, page, quote, status, video\n' >&2
  exit "${1:-64}"
}

# Check we have the right number of arguments
if [[ $# -ne 2 ]]; then
  die_usage
fi

# Check that the supplied section is valid
case $1 in
  article|audio|code|gallery|link|page|quote|status|video)
    ;;
  *)
    printf 'Error: invalid section: %q\n' "$1" >&2
    die_usage
    ;;
esac

# Create path for new Hugo content (includes content type and post title slug)
path="content/$1/$2.md"

# Create the new Hugo content
hugo new "$path"

# Open the new content for editing
"${EDITOR:-nano}" "$path"