From aaad4f7b8fdd96573e9c3c3910d8c8a2c418841b Mon Sep 17 00:00:00 2001 From: Serghei Iakovlev Date: Sun, 14 Apr 2024 10:04:36 +0200 Subject: Use .Site.Lastmod if .Site.Hugo.Version greater than or equal to v0.123.0 --- CHANGELOG.md | 20 +++++++++++++++++--- layouts/_default/list.atom.xml | 6 ++++-- layouts/_default/list.feed.xml | 6 ++++-- layouts/partials/site-last-mod.html | 21 +++++++++++++++++++++ 4 files changed, 46 insertions(+), 7 deletions(-) create mode 100644 layouts/partials/site-last-mod.html diff --git a/CHANGELOG.md b/CHANGELOG.md index fb46543..b3e4913 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased](https://github.com/sergeyklay/gohugo-theme-ed/compare/v0.7.0...HEAD) +### Added + +- Introduced a new partial template `site-last-mod.html` to handle site last + modification date more robustly across Hugo versions. This change accommodates + the deprecation of `.Site.LastChange` in favour of `.Site.Lastmod` for Hugo + versions equal to or greater than 0.123.0. Usage: + ``` + {{ $siteLastMod := partial "site-last-mod.html" . }} + ``` + ### Changed - Updated the minimum required Hugo version for this theme to 0.114.0. @@ -20,9 +30,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 warning introduced in Hugo v0.114.0, ensuring compatibility with future versions of Hugo. - Replace Go script with Node.js implementation for Netlify redirects patching - for Deploy Preview context. This change aims to eliminate the dependency on Go - for the theme's development environment, and streamline the project's technology - stack. + for Deploy Preview context. +- Updated Atom and RSS feed templates to use the `site-last-mod.html` partial for + dynamically setting the site's last modification date. + - In `list.atom.xml`, replaced `site.LastChange` with `$siteLastMod` in the + `` tag. + - In `list.feed.xml`, replaced `site.LastChange` with `$siteLastMod` in the + `` tag. ### Fixed diff --git a/layouts/_default/list.atom.xml b/layouts/_default/list.atom.xml index eabd899..67ff79b 100644 --- a/layouts/_default/list.atom.xml +++ b/layouts/_default/list.atom.xml @@ -14,6 +14,8 @@ {{- $limit := site.Params.feedSize | default 25 -}} {{- $pages = $pages | first $limit -}} +{{- $siteLastMod := partial "site-last-mod.html" . -}} + {{- safeHTML "" }} {{ printf `<![CDATA[%s]]>` (partial "title.html" .) | safeHTML }} @@ -32,8 +34,8 @@ {{ with site.Author.email }}{{ . | html }}{{ end }} {{ end }}{{ with site.Params.Copyright }}{{ $copyright := replace . "{year}" now.Year }}{{ $copyright = replace $copyright "©" "©" }} {{ $copyright | plainify }}{{ end }} - Hugo{{ if not site.LastChange.IsZero }} - {{ site.LastChange.Format "2006-01-02T15:04:05Z07:00" | safeHTML }}{{ end }}{{ $uuid := sha1 (site.BaseURL | absURL) }} + Hugo{{ if ne $siteLastMod "" }} + {{ $siteLastMod.Format "2006-01-02T15:04:05Z07:00" | safeHTML }}{{ end }}{{ $uuid := sha1 (site.BaseURL | absURL) }} urn:uuid:{{ substr $uuid 0 8 }}-{{ substr $uuid 8 4 }}-5{{ substr $uuid 13 3 }}-{{ substr $uuid 16 1 }}9{{ substr $uuid 17 2 }}-{{ substr $uuid 21 12 }}{{- range $pages }} {{- $url := .Permalink | absURL }}{{ $uuid := sha1 (.Permalink | absURL) }}{{ $page := . }} diff --git a/layouts/_default/list.feed.xml b/layouts/_default/list.feed.xml index d66688b..2bc3cb7 100644 --- a/layouts/_default/list.feed.xml +++ b/layouts/_default/list.feed.xml @@ -14,6 +14,8 @@ {{- $limit := site.Params.feedSize | default 25 -}} {{- $pages = $pages | first $limit -}} +{{- $siteLastMod := partial "site-last-mod.html" . -}} + {{- printf "" | safeHTML }} Hugo {{ hugo.Version }}{{ with site.Author.name }} {{ printf `` . | safeHTML }}{{ end }}{{ with site.LanguageCode }} {{ . }}{{ end }}{{ with site.Params.Copyright }}{{ $copyright := replace . "{year}" now.Year }}{{ $copyright = replace $copyright "©" "©" }} - {{ $copyright | plainify }}{{ end }}{{ if not site.LastChange.IsZero }} - {{ site.LastChange.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}{{ end }}{{- range $pages }} + {{ $copyright | plainify }}{{ end }}{{ if ne $siteLastMod "" }} + {{ $siteLastMod.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}{{ end }}{{- range $pages }} {{ printf `<![CDATA[%s]]>` .Title | safeHTML }} {{ .Permalink | absURL }}?utm_source=rss_feed diff --git a/layouts/partials/site-last-mod.html b/layouts/partials/site-last-mod.html new file mode 100644 index 0000000..ecd3900 --- /dev/null +++ b/layouts/partials/site-last-mod.html @@ -0,0 +1,21 @@ +{{- /* The .Site.LastChange was deprecated in Hugo v0.123.0 */ -}} +{{- /* Use .Site.Lastmod if .Site.Hugo.Version greater than or equal to v0.123.0 */ -}} +{{- /* Otherwise, use .Site.LastChange */ -}} + +{{- /* For more see: https://github.com/gohugoio/hugo/releases/tag/v0.123.0 */ -}} + +{{- /* Usage: {{ $siteLastMod := partial "site-last-mod.html" . }} */ -}} + +{{- $siteLastMod := "" -}} + +{{- if ge site.Hugo.Version "0.123.0" -}} + {{- if not site.Lastmod.IsZero -}} + {{- $siteLastMod = site.Lastmod -}} + {{- end -}} +{{- else -}} + {{- if not site.LastChange.IsZero -}} + {{- $siteLastMod = site.LastChange -}} + {{- end -}} +{{- end -}} + +{{- return $siteLastMod -}} -- cgit v1.2.3