RSS

Add git commit date as last update date in hugo page

Hugo page template: Use .GitInfo or .Page.Lastmod as last update date in hugo page

Use .GitInfo or .Page.Lastmod

Hugo have build-in support to get the last Git revision information for every content file through Page.Lastmod or .GitInfo variable. Page.Lastmod is fetched from .GitInfo.AuthorDate.

To enable Hugo get git commit info, first need change enableGitInfo to true in config.toml:

# config.toml

enableGitInfo = true

Then in any page template can use .GitInfo or .Page.Lastmod to get current file’s the latest git commit info.

Last Update:
<time datetime="{{ .Page.Lastmod.Format "Mon Jan 10 17:13:38 2020 -0700" }}" class="text-muted">
  {{ $.Page.Lastmod.Format "January 02, 2006" }}
</time>

Troubleshooting

.GitInfo is null in local hugo serve

You need commit file to make .GitInfo have validate data.

Per hugo document:

The .GitInfo feature must be enabled in your Hugo project by passing –enableGitInfo flag on the command line or by setting enableGitInfo to true in your site’s configuration file.

This is not accurate, setting enableGitInfo to true in your site’s configuration file is not enough. If you run a dev server by hugo serve, the .GitInfo also is null. You have to add --enableGitInfo to get .GitInfo:

$ hugo serve --enableGitInfo

Check the content of .GitInfo

You may use jsonify to serialize .GitInfo and display it on page:

{{ .GitInfo | jsonify }}

Here is an example output of {{ .GitInfo | jsonify }}:

{
  "hash":"f9ede6c53eed4753abe9f4ad5303a5c0fffc7b5a",
  "abbreviatedHash":"f9ede6c",
  "subject":"show last update through git commit info",
  "authorName":"...",
  "authorEmail":"...",
  "authorDate":"2020-02-13T15:01:40-08:00",
  "commitDate":"2020-02-13T15:01:40-08:00"
}

Example usage of .GitInfo:

{{- if .GitInfo }}
  <p style="font-size: small; color: grey;">Last commit: {{ .GitInfo.AbbreviatedHash }} {{.GitInfo.CommitDate}}</p>
{{- end }}

or use with:

{{ with .GitInfo }}
  <p style="font-size: small; color: grey;">Page version: {{ .AbbreviatedHash }} {{ .CommitDate.Format "2006-01-02"}}</p>
{{- end }}

Related pages:

References

OmniLock - Block / Hide App on iOS

Block distractive apps from appearing on the Home Screen and App Library, enhance your focus and reduce screen time.

DNS Firewall for iOS and Mac OS

Encrypted your DNS to protect your privacy and firewall to block phishing, malicious domains, block ads in all browsers and apps

Ad