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

Support Go time format strings in permalinks #6489

Merged
merged 1 commit into from Nov 10, 2019

Conversation

look
Copy link
Contributor

@look look commented 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:

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
@@ -173,8 +192,8 @@ func (l PermalinkExpander) validate(pp string) bool {
}

for _, match := range matches {
k := strings.ToLower(match[0][1:])
Copy link
Contributor Author

@look look Nov 10, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lower casing the key turns out to be a bug: If you use :TITLE as the key in the permalink configuration, it will pass this check, but then when trying to expand the permalink, it will fail because TITLE isn't in the map and the code would panic.

I decided to remove this to lower because it makes date formats like Friday not work because friday is not a valid Go time format.

I added a test for the behavior that non-normalized permalink configuration keys aren't valid. It would pass on master as well, due to the bug noted above.

return callback, true
}

if referenceTime.Format(attr) != attr {
Copy link
Member

@bep bep Nov 10, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Smart!

@bep bep mentioned this pull request Nov 10, 2019
@bep bep merged commit 70a1aa3 into gohugoio:master Nov 10, 2019
2 checks passed
@look
Copy link
Contributor Author

@look look commented Nov 10, 2019

🙇 Thanks for your help with this! Appreciate it.

@github-actions
Copy link

@github-actions github-actions bot commented Jan 25, 2022

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

2 participants