Si I’m trying to do exactlythe 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.
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
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.
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.
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.