Dates in post filenames

I have an almost single page Hugo site, which consists of an index that lists all of my posts (under content) sorted by date.

However, I’m finding that as it grows, having all the files under /content is getting rather messy. Is there a way to have the archetype add the date to the filename and configure hugo to ignore that when generating URL’s?

i.e. go from:

  • content
    • eggs.md
    • other.md
    • rain.md

To

  • content
    • 2020-01-01_rain.md
    • 2019-10-05_other.md
    • 2019-09-05_eggs.md

I’ve tried searching the documentation on both archetypes and organization and see no mention of how to name files.

Thank you!

No. Archetypes don’t name files, they are called when creating a file, but require the filename in the operation (hugo new 2020-01-01_rain.md).

What is your goal? You said you have too many files in the directory, but your solution to rename files is not obvious to me. What are you trying to do? :slight_smile:

Thanks for the reply!

Simply to make the long list of files easier to grok, ordered by something consistent like date of post rather than the (effectively random) title of the post. A number that increments would be equally as valuable!

For example: Right now when trying to find the most recently written article I either need to look at the output of Hugo, work out what markdown file that would be and open it, or manually go into all files and check their dates.

If you name them consistently you can use an archetype to set the title and use a function to drop off the front, similar to the example at https://gohugo.io/content-management/archetypes/#create-a-new-archetype-template

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

Perhaps using https://gohugo.io/functions/slicestr/.

Then set your permalink to use :title.

But my recommendation is to use your filesystem or version control to manage/sort dates. :sunglasses:

2 Likes

I section my posts off into folders so when I make a new post in hugo, I do hugo new posts/2020/06/10/my-post-here.md

As for your url, you can then modify your site’s config.toml to reflect

[permalinks]
"posts": "/:filename"

So the post links still stay anchored to the root without the dates or anything in it.

I do think it would be cool to have some capability to adjust / automate post/markdown naming conventions and or folder structures. I think there’s just too many variables to try make everyone happy.

In the end, I’m doing hugo new post/2020/06/12/post-slug/index.md to take advantage of image processing in shortcodes. It’s a lot to type out to make a new post and I’d love to automate or script some sort of shorter solution somehow. Just haven’t invested any time into what the best way to go about that is.

I agree!
Ultimately I created an npm script for creating new posts. I thought I’d share maybe it helps someone coming across this thread. Doesn’t really matter if you use bash or npm scripts.

{
  "scripts": "hugo new --kind post posts/$(date \"+%F\")-new-post"
}
$ npm run new
/content/posts/2020-08-29-new-post created
2 Likes

Nice!