-
Notifications
You must be signed in to change notification settings - Fork 759
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 configurable timestamping for posts #494
Conversation
I suggest closing #493 if this PR is accepted. If any changes are requested I would be happy to do so. |
@panr any thoughts? |
OverviewCompletely revamped the system. You can now set any timestamp format in both the site config as well as page frontmatter. If I missed anything, please let me know. Full development timeline can be seen in my website's repository. Took some time and some revisions but this should work fairly well and is fully compatible with any formatting allowed in Hugo according to their docs. You may notice the following snippet before each with chain:
This statement assigns the .Date value as type time to the variable Any formatting set in page front matter takes priority over any value set in the default page config. If a value is not set it defaults to the Full explainer of what's going on:Post dates use the following setup:
The first line, as stated above, sets the The next line Any time you see After the first else statement, we check whether Last updated timestamps use the following format:
The first We then run through the same logic chain as above, but substituting |
@KatieTheDev I like the second approach much more. I also have to check one more thing in the theme regarding the changes, and I'll let you know after the weekend. |
@panr I'm glad to hear. Hope everything works out for you. I'm excited to get this put in! |
Yesterday — while I was testing it — I produced almost exactly the same code as yours, so I guess we are on the same page here ;-) |
Glad to hear! |
@panr Bug discovered! I will be pushing an update to my documentation provided in the example config file. The bug: If you set your server to UTC timezone and the date format has a timezone abbreviation, it may sometimes show the UTC offset rather than the timezone abbreviation. I'm not sure what causes this, but I was able to fix the bug on my server by changing my timezone to my actual timezone. |
@KatieTheDev long time no see, sorry about that. I revisited this PR and found a bug regarding using the localization tokens (eg: Not sure about the fix, but this helped:
It seems that Can you recreate the bug on your setup as well? |
Can reproduce on |
I can confirm your fix as a fix. I included it in my latest commit. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found few little issues.
What do you think about creating a partial for that "date" section to reduce repeating the same code again and again?
Maybe a good idea would be creating the partials/date.html
and use it later like:
{{ if .Date }}
{{ partial "date" . }}
{{ end }}
and for Lastmod
:
{{ if .Lastmod }}
{{ partial "date" (dict "Date" .Lastmod) }}
{{ end }}
Not sure if the example I've provided for Lastmod
will work out of the box, but replacing context key for .Date
should be enough for not creating a separate partial only for Lastmod
.
I was able to make it all one partial. The name of the partial is post-date. Hope I did it right! |
@panr any updates? |
layouts/_default/single.html
Outdated
[{{- or $.Site.Params.updatedDatePrefix "Updated" -}} :: {{- .Lastmod.Format "2006-01-02" -}}] | ||
{{- partial "post-date" . -}} | ||
{{- if .Lastmod -}} | ||
[{{- partial "post-date" . (dict "Date" .Lastmod) -}}] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This won't work... You are passing the original context and (dict ... )
part is skipped by Hugo (AFAIR).
Also I made a mistake thinking that passing only one value to this partial solves the issue, but unfortunately I missed that also need other props from the site context...
So the new idea is to replicate this partial as post-lastmod
and just use it separately with .Lastmod
key inside instead of .Date
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Funky, guess you're right. For some reason it seemed to be working when I tested it on my computer but I guess for some reason it doesn't work at the end of my CICD pipeline. I will split it and get back to you today.
@KatieTheDev sorry, didn't see your commit 🙇 |
@panr I believe I fixed the issue. |
@@ -6,9 +6,9 @@ <h1 class="post-title"> | |||
<div class="post-meta"> | |||
{{- if .Date -}} | |||
<time class="post-date"> | |||
{{- .Date.Format "2006-01-02" -}} | |||
{{- if $.Site.Params.showLastUpdated -}} | |||
[{{- or $.Site.Params.updatedDatePrefix "Updated" -}} :: {{- .Lastmod.Format "2006-01-02" -}}] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@KatieTheDev Looks good and works well 💪 But I found that you removed the [{{- or $.Site.Params.updatedDatePrefix "Updated" -}} :: ...
string from the brackets. Was it intentional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's a bug, after the fix we will merge it :)
@panr I believe I have fixed it. Unsure if I did it right, please let me know. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
Thank you! |
All credits go to you 🙇 |
In #493, I discussed potential PR ideas for this feature but I decided to just do it. A demonstration of it working can be found at https://katiethe.dev or my site repo.
I have modified index.html, list.html, and single.html. These files all now have configurable timestamping, not just datestamping.
To enable the new feature, set
showPostTime
to true in hugo.toml. If you want seconds enabled, setpostTimeSeconds
to true. Both are false by default.You can also individually set these params to true in front matter.