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 configurable timestamping for posts #494

Merged
merged 12 commits into from
Nov 19, 2024
Merged

Conversation

KatieTheDev
Copy link
Contributor

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, set postTimeSeconds to true. Both are false by default.

You can also individually set these params to true in front matter.

@KatieTheDev KatieTheDev marked this pull request as draft October 26, 2024 18:08
@KatieTheDev KatieTheDev marked this pull request as ready for review October 26, 2024 18:09
@KatieTheDev
Copy link
Contributor Author

I suggest closing #493 if this PR is accepted. If any changes are requested I would be happy to do so.

@KatieTheDev
Copy link
Contributor Author

@panr any thoughts?

@KatieTheDev
Copy link
Contributor Author

KatieTheDev commented Oct 27, 2024

Overview

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

{{- $date := .Date | time -}}

This statement assigns the .Date value as type time to the variable $date. This is done because Hugo can't seem to access the .Date value inside the with chain otherwise.

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 "2006-01-02" format, which is what you originally had everything set to. This is done to ensure this is not a breaking change for any websites. This also means there is a safe default if people choose not to mess with the new config option. Documentation is provided on the README.md example config. This example is what I used to create my website, so I assume that is a common option.

Full explainer of what's going on:

Post dates use the following setup:

{{- $date := .Date | time -}}
{{- with .Params.dateFormat -}}
  {{- $date.Format . -}}
{{- else -}}
  {{- with .Site.Params.dateFormat -}}
    {{- $date.Format . -}}
  {{- else -}}
    {{- $date.Format "2006-01-02" -}}
  {{- end -}}
{{- end -}}

The first line, as stated above, sets the $date variable to equal the existing .Date variable, set in front matter. This allows the timestamp to be passed down into the rest of the with statements.

The next line {{- with .Params.dateFormat -}} checks whether the dateFormat value is set in front matter. In the new archetype I added, this is commented out by default. This is done to ensure users do not accidentally override their settings in the site config.

Any time you see {{- $date.Format . -}} or similar, it is using the format specified in the with statement to set the date format, and it is then displayed as text on the page.

After the first else statement, we check whether dateFormat is set in site config with {{- with .Site.Params.dateFormat -}}. If it is, we use that format. Otherwise, Hugo will move to the final else statement, setting the date format as "2006-01-02".

Last updated timestamps use the following format:

{{- if $.Site.Params.showLastUpdated -}}
  {{- $lastmod := .Lastmod | time -}}
  {{- with .Params.dateFormat -}}
     [{{- $lastmod.Format . -}}]
  {{- else -}}
    {{- with .Site.Params.dateFormat -}}
       [{{- $lastmod.Format . -}}]
    {{- else -}}
       [{{- $lastmod.Format "2006-01-02" -}}]
    {{- end -}}
  {{- end -}}
{{- end -}}

The first if statement checks whether the slowLastUpdated parameter is set in the site config. This is similar to how it is checked in the original setup. Next, $lastmod is set the same way as $date was above.

We then run through the same logic chain as above, but substituting $date for $lastmod. The brackets and   present in the date formats shown are for appearance. This places the last-modified time in brackets, similar to your original code. The   makes it so that you have spacing between the post date and the modified date. I thought it was somewhat ugly and lacked some much-needed space without it. Feel free to remove that (or ask me to) if you would rather preserve the original look. This is the only cosmetic change I have made with this PR.

@panr
Copy link
Owner

panr commented Oct 27, 2024

@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.

@KatieTheDev
Copy link
Contributor Author

@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!

@panr
Copy link
Owner

panr commented Oct 27, 2024

@panr I'm glad to hear. Hope everything works out for you.

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 ;-)

@KatieTheDev
Copy link
Contributor Author

@panr I'm glad to hear. Hope everything works out for you.

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!

@KatieTheDev
Copy link
Contributor Author

@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.

@panr
Copy link
Owner

panr commented Nov 6, 2024

@KatieTheDev long time no see, sorry about that. I revisited this PR and found a bug regarding using the localization tokens (eg: :data_long etc.). I couldn't make it work 🤔

Not sure about the fix, but this helped:

{{- $date := .Date -}}
{{- with .Params.dateFormat -}}
       {{- $date | time.Format . -}}
{{- else -}}
       {{- with .Site.Params.dateFormat -}}
            {{- $date | time.Format . -}}
       {{- else -}}
            {{- $date.Format "2006-01-02" -}}
       {{- end -}}
{{- end -}}

It seems that time.Format is not being called properly when we try to declare time as a part of $date. Or maybe it's a bug with my version of Hugo (hugo v0.133.0-c9777473d1369f812d727a6c07dc57ad7be7bf62+extended).

Can you recreate the bug on your setup as well?

@KatieTheDev
Copy link
Contributor Author

Can reproduce on hugo v0.135.0-f30603c47f5205e30ef83c70419f57d7eb7175ab+extended windows/amd64 BuildDate=2024-09-27T13:17:08Z VendorInfo=gohugoio. Working on fix.

@KatieTheDev
Copy link
Contributor Author

I can confirm your fix as a fix. I included it in my latest commit.

Copy link
Owner

@panr panr left a 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.

layouts/_default/index.html Outdated Show resolved Hide resolved
layouts/_default/list.html Outdated Show resolved Hide resolved
layouts/_default/single.html Outdated Show resolved Hide resolved
@KatieTheDev
Copy link
Contributor Author

I was able to make it all one partial. The name of the partial is post-date. Hope I did it right!

@KatieTheDev
Copy link
Contributor Author

@panr any updates?

[{{- or $.Site.Params.updatedDatePrefix "Updated" -}} :: {{- .Lastmod.Format "2006-01-02" -}}]
{{- partial "post-date" . -}}
{{- if .Lastmod -}}
 [{{- partial "post-date" . (dict "Date" .Lastmod) -}}]
Copy link
Owner

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...

Copy link
Owner

Choose a reason for hiding this comment

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

For example:

image

Copy link
Contributor Author

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.

@panr
Copy link
Owner

panr commented Nov 18, 2024

@KatieTheDev sorry, didn't see your commit 🙇

@KatieTheDev
Copy link
Contributor Author

@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" -}}]
Copy link
Owner

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?

Copy link
Owner

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

@KatieTheDev
Copy link
Contributor Author

@panr I believe I have fixed it. Unsure if I did it right, please let me know.

Copy link
Owner

@panr panr left a comment

Choose a reason for hiding this comment

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

Looks good!

@panr panr merged commit c779a70 into panr:master Nov 19, 2024
@KatieTheDev
Copy link
Contributor Author

Thank you!

@panr
Copy link
Owner

panr commented Nov 19, 2024

All credits go to you 🙇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants