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

Add permalink configuration for non-zero padded dates #6488

Closed
wants to merge 1 commit into from

Conversation

look
Copy link
Contributor

@look look commented Nov 9, 2019

This adds new permalink configuration to allow non-zero padded months (:monthnopad) and days (:daynopad) to be used in URLs. For example, 2018-01-06 can be rendered as /2018/1/6.

See: https://discourse.gohugo.io/t/implementing-additional-date-formats-for-permalinks/17860

I also looked into implementing this using strftime escapes, as hinted at by this TODO comment introduced way back in 07978e4:

//TODO: support classic strftime escapes too
// (and pass those through despite not being in the map)

I did find a very nice pure Go strftime library. However, it wasn't clear to me how to fit stftime escapes into the permalink configuration that exists today, so I went with the straightforward solution of adding new configuration.

This adds new permalink configuration to allow non-zero padded
months (:monthnopad) and days (:daynopad) to be used in URLs. For example,
`2018-01-06` can be rendered as `/2018/1/6`.

See: https://discourse.gohugo.io/t/implementing-additional-date-formats-for-permalinks/17860
@CLAassistant
Copy link

CLAassistant commented Nov 9, 2019

CLA assistant check
All committers have signed the CLA.

@bep
Copy link
Member

bep commented Nov 9, 2019

I also looked into implementing this using strftime escapes,

That comment is very old, and I'm not sure how sensible it was.

What would probably make more sense was to use the date format built into Go, e.g. 2006-01-02

Adding the daynopad etc. feels a little off; I guess it's OK if this is it, so to speak. But we could maybe also consider using the Go date constants, e.g /2006/1/2 etc.

@look
Copy link
Contributor Author

look commented Nov 9, 2019

Thanks for the quick feedback.

I'm in favor of providing a way for users to specify their own date format for permalinks, that feels the best to me. And given that the Go community really does not use strftime much, using the standard library's date formatting seems reasonable. 👍

I'd be happy to work on that. However, I'm not sure how to configure that in the permalinks entry. Do you have ideas?

This looks a little weird, but would match the attributeRegexp and then could be passed along for further processing.

permalinks:
  posts: /:format=2006/:format=1/:format=2/:slug

Or perhaps the config could optionally become a map (not sure how hard that would be to implement) to make the format string unambiguous.

permalinks:
  posts:
    format: /2016/1/2/:slug
  pages: /:title

Thoughts?

@bep
Copy link
Member

bep commented Nov 9, 2019

I was more thinking in the line of making "numbers" the special case, e.g.

permalinks:
  posts: /2006/1/2/:slug

@look
Copy link
Contributor Author

look commented Nov 9, 2019

Ah yes, that makes sense. I will try that and see how it works out.

Do you think it will impact anyone who might be using a literal number in their permalinks? It would only be those that are for Go's special case date, I suppose.

@bep
Copy link
Member

bep commented Nov 9, 2019

Do you think it will impact anyone who might be using a literal number in their permalinks?

I didn't think about those ...

We could do:

permalinks:
  posts: /:2006/:1/:2/:slug

The time.Format looks like it's doing what we want:

https://play.golang.org/p/DHSLL0h_cLk

@look
Copy link
Contributor Author

look commented Nov 9, 2019

I like that. Giving it a try now.

look added a commit to look/hugo that referenced this pull request Nov 10, 2019
In the vein of an ancient TODO about supporting custom date formatting with
strftime, this allows `:`-prefixed Go time format strings in permalink segments.
This allows users to customize date-based permalinks any way they need to.

For example, with a date of 2019-11-09, the permalink `/:06/:1/:2` will render
as `/19/11/9`.

See:

gohugoio@07978e4#diff-0688a3b65c7f5d01aa216f8d9b57fd00R111-R112
https://discourse.gohugo.io/t/implementing-additional-date-formats-for-permalinks/17860
gohugoio#6488
@look
Copy link
Contributor Author

look commented Nov 10, 2019

Closed in favor of #6489.

@look look closed this Nov 10, 2019
bep pushed a commit that referenced this pull request Nov 10, 2019
In the vein of an ancient TODO about supporting custom date formatting with
strftime, this allows `:`-prefixed Go time format strings in permalink segments.
This allows users to customize date-based permalinks any way they need to.

For example, with a date of 2019-11-09, the permalink `/:06/:1/:2` will render
as `/19/11/9`.

See:

07978e4#diff-0688a3b65c7f5d01aa216f8d9b57fd00R111-R112
https://discourse.gohugo.io/t/implementing-additional-date-formats-for-permalinks/17860
#6488
bep pushed a commit to gohugoio/hugoDocs that referenced this pull request Nov 27, 2019
In the vein of an ancient TODO about supporting custom date formatting with
strftime, this allows `:`-prefixed Go time format strings in permalink segments.
This allows users to customize date-based permalinks any way they need to.

For example, with a date of 2019-11-09, the permalink `/:06/:1/:2` will render
as `/19/11/9`.

See:

gohugoio/hugo@07978e4#diff-0688a3b65c7f5d01aa216f8d9b57fd00R111-R112
https://discourse.gohugo.io/t/implementing-additional-date-formats-for-permalinks/17860
gohugoio/hugo#6488
bep pushed a commit to gohugoio/hugoDocs that referenced this pull request Jan 27, 2020
In the vein of an ancient TODO about supporting custom date formatting with
strftime, this allows `:`-prefixed Go time format strings in permalink segments.
This allows users to customize date-based permalinks any way they need to.

For example, with a date of 2019-11-09, the permalink `/:06/:1/:2` will render
as `/19/11/9`.

See:

gohugoio/hugo@07978e4#diff-0688a3b65c7f5d01aa216f8d9b57fd00R111-R112
https://discourse.gohugo.io/t/implementing-additional-date-formats-for-permalinks/17860
gohugoio/hugo#6488
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 25, 2022
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.

None yet

3 participants