Creating content in Hugo using the Academic Theme can sometimes feel like navigating a maze of directories and file structures. As a seasoned technical program manager, I’ve experienced firsthand the inefficiencies that can arise from a seemingly straightforward process. Let’s explore how we can simplify the creation of new posts and reclaim our time for more meaningful tasks.

The Challenge

Hugo’s Academic Theme imposes a specific directory structure that, while logical, can feel contrived. If you’ve ever attempted to create a new post, you might have found yourself facing a series of steps that can lead to errors if not executed with precision:

  1. Create a directory with a URL-friendly name (no spaces or special characters).
  2. Create a subdirectory for assets.
  3. Create an index.md file with the necessary front matter.
  4. Modify index.md to include the title, summary, and other details.

The multi-step process can become tedious, especially when a simple solution is at hand.

Why It Matters

In the world of content creation, efficiency is key. It’s not just about saving time; it’s about enabling creativity and reducing friction in the workflow. By streamlining the post creation process, we empower ourselves to focus on what truly matters: crafting compelling content that resonates with our audience.

A Simple Solution

Imagine if you could condense all those steps into a single command. Enter our Bash script, which automates the creation of a new post with just one line:

./new "Title of My Awesome New Post"

This script does the heavy lifting for you, setting up the necessary directory structure and pre-filling your index.md file with the appropriate front matter.

Here’s how the magic happens:

#!/bin/bash

# Script to create a new post for Hugo Academic's expected directory structure.

TARGETDIR="./content/post"

# Ensure a title is provided
if [ -z "$1" ]; then
  echo 'You must supply a post title, e.g.'
  echo '  ./new "The Cool Title of My New Post"'
  exit 1
fi

# Sanitize the input for directory naming
TITLE=${1}
SANIN=${1// /-}
SANIN=${SANIN//_/-}
SANIN=${SANIN//[^a-zA-Z0-9\-]/}

# Pre-fill the new file with Hugo Academic theme Front Matter
ISOTIME=$(date -u +'%Y-%m-%dT%H:%M:%SZ')
PREFILLTPL="---
title: '${TITLE}'
subtitle: ''
summary: 
authors:
tags:
categories:
date: \"${ISOTIME}\"
lastmod: \"${ISOTIME}\"
draft: false
projects: []
---
"

# Create directories and files if they don't exist
if [[ ! -e "${TARGETDIR}/${SANIN}/" ]]; then
  mkdir -p "${TARGETDIR}/${SANIN}/assets/"
  touch "${TARGETDIR}/${SANIN}/index.md"
  echo "${PREFILLTPL}" >> "${TARGETDIR}/${SANIN}/index.md"
else
  echo "The directory ${TARGETDIR}/${SANIN}/ already exists! Aborting."
  exit 1
fi

How to Implement

  1. Create a text file named new in your Hugo root directory.
  2. Paste the script above into this file and save it.
  3. Run chmod +x new to make the script executable.
  4. Execute the script with your desired post title using ./new "Name of My New Post".

And just like that, you’ve automated a repetitive task, allowing you to shift your focus back to what you do best: creating engaging content.

Final Thoughts

This simple Bash script is more than just a tool; it’s a reminder that technology, when used thoughtfully, can enhance our creative processes. By approaching our workflows with curiosity and a willingness to iterate, we can continuously improve our efficiency and the quality of our output.

So, the next time you find yourself bogged down by repetitive tasks, consider how you might simplify, streamline, and innovate. After all, progress isn’t just about moving forward; it’s about finding smarter ways to navigate the journey.