Improve RSS feed for news section (#2521)

This commit is contained in:
Joe Mooring 2024-04-07 10:48:01 -07:00 committed by GitHub
parent 898d7c2619
commit 2c61534616
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 155 additions and 1 deletions

View File

@ -1,4 +1,4 @@
---
title: Hugo News
title: News
aliases: [/release-notes/]
---

57
layouts/news/list.html Normal file
View File

@ -0,0 +1,57 @@
{{ define "main" }}
<div class="w-100 ph4 ph5-ns pb5 pb6-ns pt1 pt3-ns ">
<article class="cf pa3 pa4-m pa4-l nested-copy-line-height nested-img">
<h1 class="primary-color-dark">
{{ .Title }}
</h1>
<div class="nested-copy-line-height">
{{ .Content }}
</div>
</article>
<div class="flex flex-wrap">
{{ $interior_classes := $.Site.Params.flex_box_interior_classes }}
<section class="flex-ns flex-wrap justify-between w-100 w-80-nsTK v-top">
{{ $news_items := slice }}
{{/* Get releases from GitHub. */}}
{{ $u := "https://api.github.com/repos/gohugoio/hugo/releases" }}
{{ $releases := partial "utilities/get-remote-data.html" $u }}
{{ $releases = where $releases "draft" false }}
{{ $releases = where $releases "prerelease" false }}
{{ range $releases | first 20 }}
{{ $ctx := dict
"Date" (.published_at | time.AsTime)
"Title" (printf "Release %s" .name)
"Permalink" .html_url
"Section" "news"
"Summary" ""
}}
{{ $news_items = $news_items | append $ctx }}
{{ end }}
{{/* Get content pages from news section. */}}
{{ range .Pages }}
{{ $ctx := dict
"Date" .Date
"Title" .Title
"RelPermalink" .RelPermalink
"Section" "news"
"Summary" .Summary
"Params" (dict "description" .Description)
}}
{{ $news_items = $news_items | append $ctx }}
{{ end }}
{{/* Sort by date (descending) and render. */}}
{{ range sort $news_items "Date" "desc" }}
{{ partial "boxes-section-summaries.html" (dict "context" . "classes" $interior_classes "fullcontent" false) }}
{{ end }}
</section>
</div>
</div>
{{ end }}

74
layouts/news/list.xml Normal file
View File

@ -0,0 +1,74 @@
{{- printf "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>" | safeHTML }}
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
{{- $title := printf "%s on %s" .Title site.Title }}
{{- $description := printf "Recent content in %s on %s" .Title site.Title }}
{{- if .IsHome }}
{{- $title = site.Title }}
{{- $description := printf "Recent content on %s" site.Title }}
{{- end }}
<title>{{ $title }}</title>
<description>{{ $description }}</description>
<link>{{ .Permalink }}</link>
<generator>Hugo {{ hugo.Version }}</generator>
<language>{{ or site.Language.LanguageCode site.Language.Lang }}</language>
{{- with site.Copyright }}
<copyright>{{ . }}</copyright>
{{- end }}
{{- with .OutputFormats.Get "RSS" }}
{{ printf "<atom:link href=%q rel=\"self\" type=%q />" .Permalink .MediaType | safeHTML }}
{{- end }}
{{- $news_items := slice }}
{{- /* Get releases from GitHub. */}}
{{- $u := "https://api.github.com/repos/gohugoio/hugo/releases" }}
{{- $releases := partial "utilities/get-remote-data.html" $u }}
{{- $releases = where $releases "draft" false }}
{{- $releases = where $releases "prerelease" false }}
{{- range $releases | first 20 }}
{{- $summary := printf
"Hugo %s was released on %s. See [release notes](%s) for details."
.tag_name
(.published_at | time.AsTime | time.Format "2 Jan 2006")
.html_url
}}
{{- $ctx := dict
"PublishDate" (.published_at | time.AsTime)
"Title" (printf "Release %s" .name)
"Permalink" .html_url
"Section" "news"
"Summary" ($summary | $.Page.RenderString)
}}
{{- $news_items = $news_items | append $ctx }}
{{- end }}
{{- /* Get content pages from news section. */}}
{{- range .Pages }}
{{- $ctx := dict
"PublishDate" .PublishDate
"Title" .Title
"RelPermalink" .RelPermalink
"Section" "news"
"Summary" .Summary
"Params" (dict "description" .Description)
}}
{{- $news_items = $news_items | append $ctx }}
{{- end }}
{{- /* Sort by PublishDate (descending) and render. */}}
{{- $limit := cond (gt site.Config.Services.RSS.Limit 1) site.Config.Services.RSS.Limit 999 }}
{{- range sort $news_items "PublishDate" "desc" | first $limit }}
<item>
<title>{{ .Title }}</title>
<link>{{ .Permalink }}</link>
<pubDate>{{ .PublishDate.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</pubDate>
<guid>{{ .Permalink }}</guid>
<description>{{ .Summary | transform.XMLEscape | safeHTML }}</description>
</item>
{{- end }}
<lastBuildDate>{{ (index (sort $news_items "PublishDate" "desc") 0).PublishDate.Format "Mon, 02 Jan 2006 15:04:05 -0700" | safeHTML }}</lastBuildDate>
</channel>
</rss>

View File

@ -0,0 +1,23 @@
{{/*
Parses the serialized data from the given URL and returns a map or an array.
Supports CSV, JSON, TOML, YAML, and XML.
@param {string} . The URL from which to retrieve the serialized data.
@returns {any}
@example {{ partial "get-remote-data.html" "https://example.org/foo.json" }}
*/}}
{{ $url := . }}
{{ $data := dict }}
{{ with resources.GetRemote $url }}
{{ with .Err }}
{{ errorf "%s" . }}
{{ else }}
{{ $data = .Content | transform.Unmarshal }}
{{ end }}
{{ else }}
{{ errorf "Unable to get remote resource %q" $url }}
{{ end }}
{{ return $data }}