ivoa / ivoa-web

ivoa.net website static web pages
Creative Commons Attribution Share Alike 4.0 International
3 stars 2 forks source link

Add partial with created and updated dates to page template #9

Closed JeremyMcCormick closed 5 months ago

JeremyMcCormick commented 5 months ago

For instance, this will show the created and updated dates using information from git:

{{ $creationDate := now }}
{{ $updatedDate := now }}
{{ if .GitInfo }}
  {{ $creationDate = .GitInfo.AuthorDate }}
  {{ $updatedDate = .GitInfo.CommitterDate }}
{{ end }}

<div class="date">
  <p>Created: {{- $creationDate.Format "January 02, 2006" -}}</p>
  {{ if ne $creationDate.Unix $updatedDate.Unix }}
    <p>Updated: {{- $updatedDate.Format "January 02, 2006" -}}</p>
  {{ end }}
</div>

There is a partial page-dates.html but this might work better as it is automatic.

gmantele commented 5 months ago

I might have forgotten to mention that in the page-dates.html I proposed through #8 , I make the Updated date visible only if different from the Created date. Because of some weird coincidence, I sometimes have few minutes difference between both dates though the document has been created/updated in the same time. That's why I compare the two dates with a delta of 5 minutes. If this is not desired here, ignore this delta (as Jeremy did in the example above).

gmantele commented 5 months ago

@JeremyMcCormick , in your example, you explicitly use .GitInfo dates. I have also found useful to use .Date and .LastMod as they can have a value depending on which information is available. That is configured in hugo.toml (or hugo.yaml).

In my case, I configured they as follows:

enableGitInfo = true

[frontmatter]
  date        = ['date', 'lastmod', ':fileModTime']
  lastmod     = ['lastmod', ':git', ':fileModTime', 'date', 'publishDate']

It means the following:

By the way, are you sure about .GitInfo.CommitterDate? It seems to rather be .GitInfo.CommitDate (see Hugo GitInfo).

For more readability, I also recommend using this syntax (with instead of if), though it is really a personal preference and not a recommandation (using if and prefixing properties to get with .GitInfo also works):

{{ with .GitInfo }}
  {{ $creationDate = .AuthorDate }}
  {{ $updatedDate = .CommitDate }}
{{ end }}
gmantele commented 5 months ago

Sorry about the mistake in partials/page-dates.html. I've just fixed it and committed directly in main branch. I also inserted this partial in all single pages. Feel free to remove this line if not desired.

To get the GitInfo through this partial, one should add enableGitInfo = true in hugo.toml or add the appropriate option when running Hugo (server or not).

I also noticed that this partial display date and time. In your example, you display only the date. It is probably better as there is no need to deal with any delta. So, feel free to adapte this partial if you prefer to display only the date.

JeremyMcCormick commented 5 months ago

We'll use your partial. I'll close this out.