Skip to content
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

Merged
merged 1 commit into from
Mar 11, 2018
Merged

Conversation

bep
Copy link
Member

@bep bep commented Mar 11, 2018

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:

[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:

[frontmatter]
date = [ "date",":fileModTime", ":default"]
lastmod = ["lastmod" ,":fileModTime", ":default"]

The current :default values for the different dates are

[frontmatter]
date = ["date","publishDate", "lastmod"]
lastmod = ["lastmod", "date","publishDate"]
publishDate = ["publishDate", "date"]
expiryDate = ["expiryDate"]

The above will now be the same as:

[frontmatter]
date = [":default"]
lastmod = [":default"]
publishDate = [":default"]
expiryDate = [":default"]

An example of a custom configuration using a custom date front matter field:

[frontmatter]
date = [ "myDate", ":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 #285
Closes #3310
Closes #3762
Closes #4340

@bep bep force-pushed the date-filename branch 6 times, most recently from 7fb0b7e to 7f4702a Compare March 11, 2018 13:29
@bep
Copy link
Member Author

bep commented Mar 11, 2018

@kaushalmodi do you understand the description above? (which is a different question than "do you agree with the above?" :-))

@bep bep force-pushed the date-filename branch 5 times, most recently from 58e483e to f6cfe97 Compare March 11, 2018 17:05
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 bep changed the title WORK IN PROGRESS hugolib: Extract date and slug from filename hugolib: Extract date and slug from filename Mar 11, 2018
@bep bep merged commit 68bf151 into gohugoio:master Mar 11, 2018
@kaushalmodi
Copy link
Contributor

@bep Thanks for the ping.

Sorry, but something feels wrong about this:

[frontmatter]
date = [ "date",":fileModTime", ":default"]

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?

@bep
Copy link
Member Author

bep commented Mar 11, 2018

If someone wants to use the fileModTime, why would they want date to the first element?

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.

@kaushalmodi
Copy link
Contributor

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:

lastmod = ["lastmod" ,":fileModTime", ":default"]

@kaushalmodi
Copy link
Contributor

I would not want the "last changed" timestamp if I had a date in front matter.

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.

@bep
Copy link
Member Author

bep commented Mar 11, 2018

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 :git as a new keyword to the above.

@kaushalmodi
Copy link
Contributor

Cool, I am assuming :git will the left-most element of lastmod by default? To mimic current behavior?

@bep
Copy link
Member Author

bep commented Mar 11, 2018

Cool, I am assuming :git will the left-most element of lastmod by default?

Exactly!

@ghost
Copy link

ghost commented Nov 28, 2021

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?

@jmooring
Copy link
Member

@afrophi Please use the forum (https://discourse.gohugo.io) for questions and troubleshooting.

@gohugoio gohugoio locked as resolved and limited conversation to collaborators Nov 28, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Extract date and permalink from filename
3 participants