Set .Page.Params from filename

Hi!

Si I’m trying to do exactly the same thing as @tania namely setting some param (like date, weight and title) from the filename, so that I can for instance use them in a range in the context of a list of content.

I thought it would have been easy to extract a $value from the .File.BaseFileName with functions like trim and replaceRE and use it to override a .Page.Param like .Weight , .Date or .Title (would it be set in the page front-matter or not)…

I would like to do so without an archetype nor a bash script because while they seem to be both working solutions they’re not so convenient/flexible as they require running ‘manual’ operations (creating the new page or executing the script).

@bep, this was not possible 2 years ago. Is Hugo now offering such a possibility? Or is there another way to do so ?

If not, alternatively, maybe the function range .Pages.ByParam could be extended to something like this: range .Pages.By$value ?
In which case we wouldn’t even need to be able to override any .Page.Param.

I believe filenames are great pieces of metadata.

Thanks for your help!

As far as I know, the only metadata that can be extracted from a filename is date. See:
https://gohugo.io/getting-started/configuration/#configure-front-matter

:filename

Fetches the date from the content file’s filename. For example, 2018-02-22-mypage.md will extract the date 2018-02-22 .

does it work with page bundles?

content/blog/2020/2020-09-10_test/index.md ?

Thanks for your answer. That’s a start. Do you know how it can be accessed from a layout template and used in a range?

Yes, it does.

single.html

Date: {{ .Date }}

list.html

{{ range .Site.RegularPages.ByDate }} 

Thanks! I’ll test this in the evening. Very promising.

I guess date must then be formatted like this YYYY-MM-DD?
I was thinking of something with a bit more flexibility so that it can fit my organization’s naming convention like: YYMMDD_post-title.

And for special kinds like documentation or FAQ adding weight like this: 000_YYMMDD_post-title

Any ideas for .Weight and .Title ?

Yes, that is correct.

None that would allow you to group or sort while ranging through a collection of pages. You can certainly parse the filename, but as you pointed out in your original post, that does meet your needs.

Thanks a lot for your insights.

So I guess next step is to open an issue for feature request?

Yes, but please search through the issues and pull requests first, including closed issues and pull requests. For example:

It might be handy to use a regex to parse filenames, and allow different patterns for each content type. Something like:

[frontmatter.books]
# Example: 2010-09-14_my-title_100.md
filenamePattern = `^(\d{4}-\d{2}-\d{2})_(.+)_(\d+)`
date = $1
title = $2
weight = $3

[frontmatter.movies]
# Example: 2010-09-14_99_my-title.md
filenamePattern = `^(\d{4}-\d{2}-\d{2})_(.+)_(\d+)`
date = $1
title = $3
weight = $2

I suspect implementation would be expensive, and I also suspect it would be somewhat fragile in use.

If something like this were implemented, it would never satisfy all use cases. If it were me, I’d write a script to parse the filename and insert frontmatter accordingly. You could even create a directory that’s constantly monitored for new files, and trigger the script when a new file appears.

Great! Thanks a lot!

I see this has been a recurrent demand from the community and that nice solutions have been brought to the table.
I will have a close look at these.

Your solution seems very convenient as well.

I get that writing a script would provide an immediate working hack but isn’t a very clean solution and is not so much adapted for distributed / multi author set up with no CI/CD.

I’m thinking Rmarkdown provides an easy method with ymlthis integration in blogdown as well but all those are different stories.

Following up on this, I opened the issue.