How I simplified my Hugo blog post creation (with a little help from AI)
Here's how I turned my cumbersome six-step Hugo post creation process into a single command using a shell script - with Amazon Q Developer doing the heavy lifting. A quick win for lazy developers like me, who'd rather focus on writing than remembering dates and fiddling with filenames.
Dennis Traub
Amazon Employee
Published Oct 29, 2024
I wrote this little script to simplify creating new posts in my new Hugo-based private blog. Well, actually Amazon Q Developer wrote the script - I just asked nicely, checked for errors, and tweaked the result until it did exactly what I wanted.
Now, I'm a complete Hugo n00b, so the only way I know to create new posts is by running:
Simple enough, right? Well, there are a few things that make this more cumbersome than it needs to be:
- I like to organize my posts by date (e.g.,
posts/2024-10-29/
), but I never seem to remember today’s date. - As far as I know, post filenames can’t have spaces, so I have to manually convert “My title” to
my-title-with-dashes
- sure, maybe camelCase works too, but that’s even more mental overhead. - I constantly forget to add the
.md
extension at the end.
So my typical workflow looks like this:
- Type
hugo new posts/
- Stop to look up today’s date
- Continue with
2024-10-29/title-with-dashes
- Hit enter
- Realize I forgot the
.md
extension (again!) - Rename the file to
posts/2024-10-29/title-with-dashes.md
Now, I should mention that Hugo is a great framework with a huge community, so there’s probably a better way to do this. But as a newbie, this is what I found in the docs, and these are the pain points I ran into.
Six steps just to create a new post? I’m a lazy developer, so I knew there had to be a better way. Here’s the shell script I now use from my project’s root folder:
And here’s how it works:
Remember that part where I said Amazon Q Developer wrote this? Here’s how that actually went down. I literally just opened Q’s chat in my IDE and had this conversation:
Me:
Create a shell script that turns
./new "[title]"
into:Example: Today is October 29th, 2024. When I run
./new "my title"
, execute hugo new post/2024-10-29/my-title.md
Amazon Q Developer:
Certainly! I’ll create a shell script named
new
that accomplishes what you’ve described. Here’s the script:To use this script:
- Save it in a file named
new
(without any extension) in the root directory of your project. - Make the script executable by running
chmod +x /path/to/new
- Now you can use the script as you described. For example:
new "My title"
This script does the following:
- It checks if a title was provided as an argument.
- It gets the current date in the YYYY-MM-DD format.
- It converts the provided title to lowercase and replaces spaces with dashes.
- It constructs the Hugo command with the formatted date and title.
- It executes the Hugo command to create the new post.
With this script, when you run
new "My title"
on October 29th, 2024, it will execute:Remember to ensure that you save this script in the root folder of your project so that it creates new posts in the correct location.
And that was it! Kind of feels like cheating, right? But the script works exactly as I wanted it to, and I’d probably have written something similar myself - just with more trial and error, visits to Stack Overflow, and time spent looking up shell commands I don’t use often enough to remember.
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.