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

Conversation

Projects
None yet
2 participants
@bep
Member

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

This comment has been minimized.

Member

bep commented Mar 11, 2018

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

hugolib: Extract date and slug from filename
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 #285
Closes #3310
Closes #3762
Closes #4340

@bep bep changed the title from WORK IN PROGRESS hugolib: Extract date and slug from filename to hugolib: Extract date and slug from filename Mar 11, 2018

@bep bep merged commit 68bf151 into gohugoio:master Mar 11, 2018

3 of 5 checks passed

continuous-integration/appveyor/branch Waiting for AppVeyor build to complete
Details
continuous-integration/travis-ci/push The Travis CI build is in progress
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
license/cla Contributor License Agreement is signed.
Details
@kaushalmodi

This comment has been minimized.

Member

kaushalmodi commented Mar 11, 2018

@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

This comment has been minimized.

Member

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

This comment has been minimized.

Member

kaushalmodi commented Mar 11, 2018

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

This comment has been minimized.

Member

kaushalmodi commented Mar 11, 2018

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

This comment has been minimized.

Member

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

This comment has been minimized.

Member

kaushalmodi commented Mar 11, 2018

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

@bep

This comment has been minimized.

Member

bep commented Mar 11, 2018

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

Exactly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment