Hugo New Post Title

Dave Naylor

| 3 minutes

Categories: Hugo

Hugo New Post Title

Automating the little things

It’s been a few weeks since my last post but I haven’t been sitting on my hands doing nothing—oh no.

I may write about the project that’s currently consuming a fair bit of my time but in short I’m porting a large established WordPress site over to Hugo. I’m probably about 50% of the way there and enjoying the process along the way.

I’m learning more about Hugo each day and I’m now beginning to think like a Hugo developer rather than a WordPress one.

Anyway, it struck me that as I learn a new technique to solve a problem or write a neat code snippet, it would be a good idea to document what I’ve done on here. It might help someone else out and save them a bit of head scratching.

Here’s my first submission.

New post title manipulation

When you create a new post in Hugo, depending on your chosen content structure, you could type something like this:

hugo new post/2020/this-is-a-lovely-new-post/

That command creates a new directory in the posts/2020 path and adds-in an file. That’s how I started off doing things.

When Hugo creates the file, it looks for an associated archetype defaulting to installation-root/archetypes/

Post titles are defined in that markdown file like this:

title: "{{ replace .Name "-" " " | title }}"

Hugo’s replace function takes .Name (this-is-a-lovely-new-post) and replaces hyphens with spaces. So we end up with “this is a lovely new post”. That’s then piped to Hugo’s title function and we end up with a post title looking something like “This is a Lovely New Post

All good and you can stick with that ad-infinitum if you’re happy.

Tidy up the mess

A problem I encountered concerns the management of posts. If my 2020 directory is full of differently named posts it can become difficult to locate individual posts at first glance.

A date weighted sort filter would quickly fix the problem but it’d be better to have the posts named with a date prefix. Something like “2020-09-30-this-is-a-lovely-post” That was a no brainer for me so I introduced that format for my posts.

I then had the problem of having to created posts like this:

hugo new post/2020/2020-09-30-this-is-a-lovely-new-post/

Since the default archetype would automatically format the title, I’d end up with a post titled “2020 09 30 This is a Lovely New Post” which would then require editing to remove the date. That’s rubbish.

The solution is to make use of another Hugo function: replaceRE. That replaces all occurrences of a regular expression with the replacement pattern.

So I need to remove the first 11 characters from my newly created blog post title via replaceRE and regex. Here’s how:

Edit your archetype in use, or your custom one, and replace the title line with this one:

title: "{{ replace (replaceRE "^(.{0,11})" "" .Name) "-" " " | title }}"

replaceRE runs before replace deleting the first 11 characters before the original replace runs and gives us our nicely formatted title.

If your date string differs to mine, just amend the regex to suit your setup.



Previous post

E-mail Attachments

Next post

Silence Is Golden