From 3f62c7960373c044dc014c8355496038ede94e22 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Mon, 16 Nov 2026 12:09:03 +0100 Subject: [PATCH] feat(feed): carry the full text in content:encoded The description of an item is now the plain summary of the post, the full text with absolute links goes in content:encoded (the end marker of a CDATA section inside the text is split), and an item has its categories and its author (dc:creator). Refs #9 --- internal/build/build_test.go | 2 +- internal/build/feed.go | 25 ++++++++++++++++++++----- internal/build/feed_test.go | 36 ++++++++++++++++++++++++++++++++++-- 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/internal/build/build_test.go b/internal/build/build_test.go index 2d750ba..a0b48f8 100644 --- a/internal/build/build_test.go +++ b/internal/build/build_test.go @@ -454,7 +454,7 @@ func TestBuildFeed(t *testing.T) { feed := readOut(t, out, "news/feed.xml") mustContain(t, "feed", feed, "https://kmalloc.org/news/2026/11/shop.html", - `<a href="https://kmalloc.org/people.html">people</a>`) + `The programme is out. See people.

`) } // TestBuildErrorsOnMissingTemplate checks that a missing layout template is named. diff --git a/internal/build/feed.go b/internal/build/feed.go index d9adb7c..b9a1aeb 100644 --- a/internal/build/feed.go +++ b/internal/build/feed.go @@ -15,10 +15,16 @@ import ( // href="/people.html", but not one that is relative to the protocol (href="//host/x"). var siteRelative = regexp.MustCompile(`\b(href|src)="/([^/])`) +// cdata wraps text in a CDATA section; the end marker inside the text is split +// over two sections, so it cannot close the first one. +func cdata(text string) string { + return "", "]]]]>") + "]]>" +} + // feed returns the RSS 2.0 feed of the latest limit posts, which must be sorted -// newest first. Each item carries the full text with absolute links. The -// channel is described by the language l, and newsURL is the address of the -// news page, such as "/news/". +// newest first. Each item has the description as a plain summary and the full +// text, with absolute links, in content:encoded. The channel is described by +// the language l, and newsURL is the address of the news page, such as "/news/". func feed(base string, l site.Lang, newsURL string, posts []renderedPost, limit int) string { if limit < len(posts) { posts = posts[:limit] @@ -32,7 +38,16 @@ func feed(base string, l site.Lang, newsURL string, posts []renderedPost, limit items.WriteString("" + link + "\n") items.WriteString(`` + link + "\n") items.WriteString("" + p.Date.Format(time.RFC1123Z) + "\n") - items.WriteString("" + render.Escape(text) + "\n") + if p.Description != "" { + items.WriteString("" + render.Escape(p.Description) + "\n") + } + if p.Author != "" { + items.WriteString("" + render.Escape(p.Author) + "\n") + } + for _, c := range p.Categories { + items.WriteString("" + render.Escape(c.Name) + "\n") + } + items.WriteString("" + cdata(text) + "\n") items.WriteString("\n") } built := "" @@ -41,7 +56,7 @@ func feed(base string, l site.Lang, newsURL string, posts []renderedPost, limit } url := base + newsURL return ` - + kmalloc.org: ` + render.Escape(l.Nav["news"]) + ` ` + url + ` diff --git a/internal/build/feed_test.go b/internal/build/feed_test.go index bf3b932..78c27a1 100644 --- a/internal/build/feed_test.go +++ b/internal/build/feed_test.go @@ -18,7 +18,7 @@ func feedLang() site.Lang { // TestFeedItems checks the channel, the item fields, the limit and the dates. func TestFeedItems(t *testing.T) { items := []renderedPost{ - {Post: testPost("new", "2026-11-14", "New ", ""), HTML: `

See people.

`}, + {Post: testPost("new", "2026-11-14", "New ", "A summary & more.", "FLUG", "Linux Day"), HTML: `

See people.

`}, {Post: testPost("old", "2026-11-12", "Old", ""), HTML: "

Old text.

"}, {Post: testPost("older", "2026-11-10", "Older", ""), HTML: "

Older text.

"}, } @@ -35,7 +35,11 @@ func TestFeedItems(t *testing.T) { `https://kmalloc.org/news/2026/11/new.html`, "Sat, 14 Nov 2026 12:00:00 +0100", "Sat, 14 Nov 2026 12:00:00 +0100", - `<a href="https://kmalloc.org/people.html">`, + `xmlns:content="http://purl.org/rss/1.0/modules/content/"`, + "A summary & more.", + `See people.

]]>
`, + "FLUG", + "Linux Day", } { if !strings.Contains(got, want) { t.Errorf("feed does not contain %q\n%s", want, got) @@ -49,6 +53,34 @@ func TestFeedItems(t *testing.T) { } } +// TestFeedCarriesTheFullTextAsContent checks that the text is in content:encoded and the +// description stays a plain summary, and that the end marker of a CDATA section in a text +// does not close it. +func TestFeedCarriesTheFullTextAsContent(t *testing.T) { + post := testPost("a", "2026-11-14", "A", "Short.") + post.Author = "Matteo Bini" + items := []renderedPost{{Post: post, HTML: "
a ]]> b
\n

Text.

"}} + got := feed("https://kmalloc.org", feedLang(), "/news/", items, 5) + mustContain(t, "feed", got, "Short.", "Matteo Bini", + `xmlns:dc="http://purl.org/dc/elements/1.1/"`, "
a ]]]]> b
") + if strings.Contains(got, "<") || strings.Contains(got, "<pre>") { + t.Errorf("the description holds markup:\n%s", got) + } + var parsed struct { + Channel struct { + Items []struct { + Content string `xml:"http://purl.org/rss/1.0/modules/content/ encoded"` + } `xml:"item"` + } `xml:"channel"` + } + if err := xml.Unmarshal([]byte(got), &parsed); err != nil { + t.Fatalf("feed is not well-formed XML: %v", err) + } + if len(parsed.Channel.Items) != 1 || parsed.Channel.Items[0].Content != "
a ]]> b
\n

Text.

" { + t.Errorf("content:encoded = %+v, want the text back as written", parsed.Channel.Items) + } +} + // TestFeedLeavesProtocolRelativeLinks checks that "//host/x" is not given the site address. func TestFeedLeavesProtocolRelativeLinks(t *testing.T) { items := []renderedPost{{Post: testPost("a", "2026-11-14", "A", ""), HTML: ` p`}}