From 8c059cbe143fc74ea7a7608358aa44336071352c Mon Sep 17 00:00:00 2001 From: Joe Mooring Date: Fri, 28 Feb 2025 14:15:31 -0800 Subject: [PATCH] theme: Use content adapter for news section --- content/en/news/_content.gotmpl | 30 ++++++++++++ content/en/quick-reference/glossary/_index.md | 3 +- hugo.toml | 15 ++++++ layouts/_default/list.html | 21 ++++---- layouts/_default/list.rss.xml | 24 +++++----- layouts/_default/single.html | 9 +++- layouts/partials/layouts/date.html | 5 ++ layouts/partials/layouts/page-edit.html | 14 +++--- layouts/partials/layouts/toc.html | 30 ++++++------ layouts/partials/news/get-news-items.html | 48 ------------------- 10 files changed, 105 insertions(+), 94 deletions(-) create mode 100644 content/en/news/_content.gotmpl create mode 100644 layouts/partials/layouts/date.html delete mode 100644 layouts/partials/news/get-news-items.html diff --git a/content/en/news/_content.gotmpl b/content/en/news/_content.gotmpl new file mode 100644 index 000000000..5b832ae40 --- /dev/null +++ b/content/en/news/_content.gotmpl @@ -0,0 +1,30 @@ +{{/* Get releases from GitHub. */}} +{{ $u := "https://api.github.com/repos/gohugoio/hugo/releases" }} +{{ $releases := partial "helpers/funcs/get-remote-data.html" $u }} +{{ $releases = where $releases "draft" false }} +{{ $releases = where $releases "prerelease" false }} + +{{/* Add pages. */}} +{{ range $releases | first 20 }} + {{ $publishDate := .published_at | time.AsTime }} + + {{/* Correct the v0.138.0 release date. See https://github.com/gohugoio/hugo/issues/13066. */}} + {{ if eq .name "v0.138.0" }} + {{ $publishDate = "2024-11-06T11:22:34Z" }} + {{ end }} + + {{ $content := dict "mediaType" "text/markdown" "value" "" }} + {{ $dates := dict "publishDate" (time.AsTime $publishDate) }} + {{ $params := dict "permalink" .html_url }} + {{ $build := dict "render" "never" "list" "local" }} + {{ $page := dict + "build" $build + "content" $content + "dates" $dates + "kind" "page" + "params" $params + "path" .name + "title" (printf "Release %s" .name) + }} + {{ $.AddPage $page }} +{{ end }} diff --git a/content/en/quick-reference/glossary/_index.md b/content/en/quick-reference/glossary/_index.md index 1b5111e8e..238d4610d 100644 --- a/content/en/quick-reference/glossary/_index.md +++ b/content/en/quick-reference/glossary/_index.md @@ -3,7 +3,8 @@ title: Glossary description: Terms commonly used throughout the documentation. categories: [quick-reference] keywords: [glossary] -hide_in_this_section: true +params: + hide_in_this_section: true menu: docs: parent: quick-reference diff --git a/hugo.toml b/hugo.toml index 8fff3ea37..a625cb301 100644 --- a/hugo.toml +++ b/hugo.toml @@ -9,6 +9,21 @@ title = "Hugo" # We do redirects via Netlify's _redirects file, generated by Hugo (see "outputs" below). disableAliases = true +[cascade] + [cascade.params] + hide_in_this_section = true + show_publish_date = true + [cascade.target] + path = '{/news/**}' + kind = 'page' + + +[frontmatter] + date = ['date'] + expiryDate = ['expirydate'] + lastmod = [':git', 'lastmod', 'publishdate', 'date'] + publishDate = ['publishdate', 'date'] + [caches.images] dir = ":cacheDir/images" maxAge = "1440h" diff --git a/layouts/_default/list.html b/layouts/_default/list.html index 76513788f..d34a95049 100644 --- a/layouts/_default/list.html +++ b/layouts/_default/list.html @@ -1,15 +1,12 @@ {{ define "main" }} {{ $pages := "" }} - {{ $showDate := false }} {{ if .IsPage }} {{/* We currently have a slightly odd content structure with no top level /docs section. */}} {{ $pages = .CurrentSection.Pages }} {{ else }} + {{ $pages = .Pages }} {{ if eq .Section "news" }} - {{ $pages = partial "news/get-news-items.html" . }} - {{ $showDate = true }} - {{ else }} - {{ $pages = .Pages }} + {{ $pages = $pages.ByPublishDate.Reverse }} {{ end }} {{ end }} @@ -23,12 +20,14 @@ {{ end }} - {{ if $showDate }} -

- {{ .Date.Format "January 2, 2006" }} -

+ href="{{ or .Params.permalink .RelPermalink }}"> + {{ if .Params.show_publish_date }} + {{ with .PublishDate }} +

+ {{ partial "layouts/date.html" . }} +

+ {{ end }} {{ end }}

diff --git a/layouts/_default/list.rss.xml b/layouts/_default/list.rss.xml index ac364a122..90fa22148 100644 --- a/layouts/_default/list.rss.xml +++ b/layouts/_default/list.rss.xml @@ -12,22 +12,20 @@ {{- with .OutputFormats.Get "rss" }} {{ printf "" .Permalink .MediaType | safeHTML }} {{- end }} - - - {{ $news_items := partial "news/get-news-items.html" . }} - - {{- /* Sort, limit, and render lastBuildDate. */}} - {{- $limit := cond (gt site.Config.Services.RSS.Limit 1) site.Config.Services.RSS.Limit 999 }} - {{- $news_items = sort $news_items "PublishDate" "desc" | first $limit }} - {{ (index $news_items 0).PublishDate.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }} - - {{- /* Render items. */}} - {{- range $news_items }} + {{- $limit := cond (gt site.Config.Services.RSS.Limit 0) site.Config.Services.RSS.Limit 999 }} + {{- $pages := "" }} + {{- with site.GetPage "/news" }} + {{- $pages = .Pages.ByPublishDate.Reverse | first $limit }} + {{- else }} + {{- errorf "The list.rss.xml layout was unable to find the 'news' page." }} + {{- end }} + {{ (index $pages 0).PublishDate.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }} + {{- range $pages }} {{ .Title }} - {{ .Permalink }} + {{ or .Params.permalink .Permalink }} {{ .PublishDate.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }} - {{ .Permalink }} + {{ or .Params.permalink .Permalink }} {{ .Summary | transform.XMLEscape | safeHTML }} {{- end }} diff --git a/layouts/_default/single.html b/layouts/_default/single.html index 198f9a91d..57d5313f7 100644 --- a/layouts/_default/single.html +++ b/layouts/_default/single.html @@ -8,7 +8,14 @@ {{ . | markdownify }} {{ end }} - + {{ if .Params.show_publish_date }} + {{ with .PublishDate }} +

+ {{ partial "layouts/date.html" . }} +

+ {{ end }} + {{ end }} {{ $t := debug.Timer "single.categories" }} {{ $categories := .GetTerms "categories" }} {{ with $categories }} diff --git a/layouts/partials/layouts/date.html b/layouts/partials/layouts/date.html new file mode 100644 index 000000000..2ec1450a5 --- /dev/null +++ b/layouts/partials/layouts/date.html @@ -0,0 +1,5 @@ +{{ $humanDate := time.Format "January 2, 2006" . }} +{{ $machineDate := time.Format "2006-01-02T15:04:05-07:00" . }} + diff --git a/layouts/partials/layouts/page-edit.html b/layouts/partials/layouts/page-edit.html index cdbedc4ae..6a976d11a 100644 --- a/layouts/partials/layouts/page-edit.html +++ b/layouts/partials/layouts/page-edit.html @@ -14,11 +14,13 @@ {{ with .File }} - {{ $href := printf "%sedit/master/content/%s/%s" site.Params.ghrepo $.Lang .Path }} -
- Improve this page - + {{ if not .IsContentAdapter }} + {{ $href := printf "%sedit/master/content/%s/%s" site.Params.ghrepo $.Lang .Path }} + + Improve this page + + {{ end }} {{ end }} diff --git a/layouts/partials/layouts/toc.html b/layouts/partials/layouts/toc.html index 04b3116de..b9baa0209 100644 --- a/layouts/partials/layouts/toc.html +++ b/layouts/partials/layouts/toc.html @@ -1,17 +1,19 @@ -{{ with .Fragments.Headings }} -
-

- On this page -

- -
+{{ with .Fragments }} + {{ with .Headings }} +
+

+ On this page +

+ +
+ {{ end }} {{ end }} {{ define "render-toc-level" }} diff --git a/layouts/partials/news/get-news-items.html b/layouts/partials/news/get-news-items.html deleted file mode 100644 index c343f942d..000000000 --- a/layouts/partials/news/get-news-items.html +++ /dev/null @@ -1,48 +0,0 @@ -{{ $news_items := slice }} - -{{/* Get releases from GitHub. */}} -{{ $u := "https://api.github.com/repos/gohugoio/hugo/releases" }} -{{ $releases := partial "helpers/funcs/get-remote-data.html" $u }} -{{ $releases = where $releases "draft" false }} -{{ $releases = where $releases "prerelease" false }} -{{ range $releases | first 20 }} - {{ $publishDate := .published_at | time.AsTime }} - - {{/* Correct the v0.138.0 release date. See https://github.com/gohugoio/hugo/issues/13066. */}} - {{ if eq .name "v0.138.0" }} - {{ $publishDate = "2024-11-06T11:22:34Z" | time.AsTime }} - {{ end }} - - {{ $ctx := dict - "Date" $publishDate - "LinkTitle" (printf "Release %s" .name) - "Permalink" .html_url - "PublishDate" $publishDate - "RelPermalink" .html_url - "Section" "news" - "Summary" "" - "Title" (printf "Release %s" .name) - }} - {{ $news_items = $news_items | append $ctx }} -{{ end }} - -{{/* Get content pages from news section. */}} -{{ range where site.RegularPages "Section" "news" }} - {{ $ctx := dict - "Date" .Date - "LinkTitle" .Title - "Params" (dict "description" .Description) - "Permalink" .Permalink - "PublishDate" .PublishDate - "RelPermalink" .RelPermalink - "Section" "news" - "Summary" .Summary - "Title" .Title - }} - {{ $news_items = $news_items | append $ctx }} -{{ end }} - -{{/* Sort by date in descending order. */}} -{{ $news_items = sort $news_items "Date" "desc" }} - -{{ return $news_items }}