-
-
Notifications
You must be signed in to change notification settings - Fork 7.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
hugolib: Extract date and slug from filename #4494
Conversation
7fb0b7e
to
7f4702a
Compare
@kaushalmodi do you understand the description above? (which is a different question than "do you agree with the above?" :-)) |
58e483e
to
f6cfe97
Compare
This commit makes it possible to extract the date from the content filename. Also, the filenames in these cases will make for very poor permalinks, so we will also use the remaining part as the page `slug` if that value is not set in front matter. This should make it easier to move content from Jekyll to Hugo. To enable, put this in your `config.toml`: ```toml [frontmatter] date = [":filename", ":default"] ``` This commit is also a spring cleaning of how the different dates are configured in Hugo. Hugo will check for dates following the configuration from left to right, starting with `:filename` etc. So, if you want to use the `file modification time`, this can be a good configuration: ```toml [frontmatter] date = [ "date",":fileModTime", ":default"] lastmod = ["lastmod" ,":fileModTime", ":default"] ``` The current `:default` values for the different dates are ```toml [frontmatter] date = ["date","publishDate", "lastmod"] lastmod = ["lastmod", "date","publishDate"] publishDate = ["publishDate", "date"] expiryDate = ["expiryDate"] ``` The above will now be the same as: ```toml [frontmatter] date = [":default"] lastmod = [":default"] publishDate = [":default"] expiryDate = [":default"] ``` Note: * We have some built-in aliases to the above: lastmod => modified, publishDate => pubdate, published and expiryDate => unpublishdate. * If you want a new configuration for, say, `date`, you can provide only that line, and the rest will be preserved. * All the keywords to the right that does not start with a ":" maps to front matter parameters, and can be any date param (e.g. `myCustomDateParam`). * The keywords to the left are the **4 predefined dates in Hugo**, i.e. they are constant values. * The current "special date handlers" are `:fileModTime` and `:filename`. We will soon add `:git` to that list. Fixes gohugoio#285 Closes gohugoio#3310 Closes gohugoio#3762 Closes gohugoio#4340
@bep Thanks for the ping. Sorry, but something feels wrong about this:
If someone wants to use the fileModTime, why would they want date to the first element? Also, :default already has the date element. Was that "date" element as the first one a typo? |
I'm not saying they should. I just saying that this is how I would have configured it. I would not want the "last changed" timestamp if I had a date in front matter. As to the rest, I suggest you take it for a spin and see how it works. |
If the user has date defined in the front-matter, it will never be set to the :fileModTime. Is that what you meant to do? May be you meant to say: "So, if you want to use the file modification time as a fall-back in absence of date in front-matter, this can be a good configuration:"? .. and of course, the same point for:
|
OK, that's what I meant by my alternative sentence above.. in that case, that config makes sense. Your original description read as if the user wanted to use fileModTime as the higher precedence. All in all, this feature looks great! This will really satisfy folks who want to sync pubdate with date or vice versa, or whatever :) Thanks. |
It will take the first date found from left to right. Up to the end user. The flexibility is endless. I'm about to add |
Cool, I am assuming :git will the left-most element of lastmod by default? To mimic current behavior? |
Exactly! |
The slug part only works when the markdown files are in root. Due to this, it breaks my category pages which are folders in the content folder root and the links to them and the layout stops working, Any way around this? |
@afrophi Please use the forum (https://discourse.gohugo.io) for questions and troubleshooting. |
This commit makes it possible to extract the date from the content filename. Also, the filenames in these cases will make for very poor permalinks, so we will also use the remaining part as the page
slug
if that value is not set in front matter.This should make it easier to move content from Jekyll to Hugo.
To enable, put this in your
config.toml
:This commit is also a spring cleaning of how the different dates are configured in Hugo. Hugo will check for dates following the configuration from left to right, starting with
:filename
etc.So, if you want to use the
file modification time
, this can be a good configuration:The current
:default
values for the different dates areThe above will now be the same as:
An example of a custom configuration using a custom date front matter field:
Note:
date
, you can provide only that line, and the rest will be preserved.myCustomDateParam
).:fileModTime
and:filename
. We will soon add:git
to that list.Fixes #285
Closes #3310
Closes #3762
Closes #4340