Modify global slug rule

I have file names:

0101-firstpost.md
0117-second.md
0217-third.md
...

Adding a rule to config.yaml:

permalinks:
  posts: "/:filename"

will give them 0101-firstpost permalinks.

How can I trim the first numbers so that permalinks become firstpost only.

You can add a slug: firstpost to your posts frontmatter and then posts: "/:slug" in the global configuration.

Cutting off the first x characters in a config file is impossible.

Another approach would be to rethink the filenames. If those numbers are there to sort the items on your website have a look at the frontmatter variable weight. You could move that into frontmatter and sort by weight. Then of course you will loose the sorting of the files in your filesystem too, so that might not be the preferred outcome.

1 Like

Those numbers are a quick reference for me to know the date I created these posts in without having to open every file one by one.

This is unfortunate. It would be awesome if one could define his own slug function. Replacing with slug almost solves the issue since it reads “title” property and adds hyphens to it. But if title is different than filename, it must be done manually…

Created issue here: Redefine slug function · Issue #10875 · gohugoio/hugo · GitHub

Are you willing to rename your files using this pattern?

2023-01-17-firstpost.md

I’d prefer to keep it short, but if that’s the only workaround for now then I can go with it

site config

[frontmatter]
date = [':filename', ':default']

https://gohugo.io/getting-started/configuration/#configure-front-matter

You get both date and slug.

1 Like

My problem isn’t with date stamping. My problem is with automatically setting a permalink from a filename.

I want all permalinks to be generated from trimmed filenames by removing the inserted date from the filename.

Did you try it?

Oh, I had to remove permalink property from config.

It’s working now. Thanks.

Given that this functionality already exists for the YYYY-MM-DD format, I don’t see this ever happening:
https://github.com/gohugoio/hugo/issues/10875

Given that this functionality already exists for the YYYY-MM-DD format, shouldn’t it be easily adaptable to other formats like MMDD?

Another example where defining a custom slug function might be useful: a book template that is organized into chapters:

01-intro.md
02-ch2.md
...

Update: I managed to get the desired effect by editing dateAndSlugFromBaseFilename function: