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
This commit is contained in:
parent
4a501d77b3
commit
3f62c79603
3 changed files with 55 additions and 8 deletions
|
|
@ -454,7 +454,7 @@ func TestBuildFeed(t *testing.T) {
|
|||
feed := readOut(t, out, "news/feed.xml")
|
||||
mustContain(t, "feed", feed,
|
||||
"<guid isPermaLink=\"true\">https://kmalloc.org/news/2026/11/shop.html</guid>",
|
||||
`<a href="https://kmalloc.org/people.html">people</a>`)
|
||||
`<content:encoded><![CDATA[<p>The programme is out. See <a href="https://kmalloc.org/people.html">people</a>.</p>`)
|
||||
}
|
||||
|
||||
// TestBuildErrorsOnMissingTemplate checks that a missing layout template is named.
|
||||
|
|
|
|||
|
|
@ -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 "<![CDATA[" + strings.ReplaceAll(text, "]]>", "]]]]><![CDATA[>") + "]]>"
|
||||
}
|
||||
|
||||
// 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>" + link + "</link>\n")
|
||||
items.WriteString(`<guid isPermaLink="true">` + link + "</guid>\n")
|
||||
items.WriteString("<pubDate>" + p.Date.Format(time.RFC1123Z) + "</pubDate>\n")
|
||||
items.WriteString("<description>" + render.Escape(text) + "</description>\n")
|
||||
if p.Description != "" {
|
||||
items.WriteString("<description>" + render.Escape(p.Description) + "</description>\n")
|
||||
}
|
||||
if p.Author != "" {
|
||||
items.WriteString("<dc:creator>" + render.Escape(p.Author) + "</dc:creator>\n")
|
||||
}
|
||||
for _, c := range p.Categories {
|
||||
items.WriteString("<category>" + render.Escape(c.Name) + "</category>\n")
|
||||
}
|
||||
items.WriteString("<content:encoded>" + cdata(text) + "</content:encoded>\n")
|
||||
items.WriteString("</item>\n")
|
||||
}
|
||||
built := ""
|
||||
|
|
@ -41,7 +56,7 @@ func feed(base string, l site.Lang, newsURL string, posts []renderedPost, limit
|
|||
}
|
||||
url := base + newsURL
|
||||
return `<?xml version="1.0" encoding="utf-8"?>
|
||||
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
|
||||
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/">
|
||||
<channel>
|
||||
<title>kmalloc.org: ` + render.Escape(l.Nav["news"]) + `</title>
|
||||
<link>` + url + `</link>
|
||||
|
|
|
|||
|
|
@ -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 <post>", ""), HTML: `<p>See <a href="/people.html">people</a>.</p>`},
|
||||
{Post: testPost("new", "2026-11-14", "New <post>", "A summary & more.", "FLUG", "Linux Day"), HTML: `<p>See <a href="/people.html">people</a>.</p>`},
|
||||
{Post: testPost("old", "2026-11-12", "Old", ""), HTML: "<p>Old text.</p>"},
|
||||
{Post: testPost("older", "2026-11-10", "Older", ""), HTML: "<p>Older text.</p>"},
|
||||
}
|
||||
|
|
@ -35,7 +35,11 @@ func TestFeedItems(t *testing.T) {
|
|||
`<guid isPermaLink="true">https://kmalloc.org/news/2026/11/new.html</guid>`,
|
||||
"<pubDate>Sat, 14 Nov 2026 12:00:00 +0100</pubDate>",
|
||||
"<lastBuildDate>Sat, 14 Nov 2026 12:00:00 +0100</lastBuildDate>",
|
||||
`<a href="https://kmalloc.org/people.html">`,
|
||||
`xmlns:content="http://purl.org/rss/1.0/modules/content/"`,
|
||||
"<description>A summary & more.</description>",
|
||||
`<content:encoded><![CDATA[<p>See <a href="https://kmalloc.org/people.html">people</a>.</p>]]></content:encoded>`,
|
||||
"<category>FLUG</category>",
|
||||
"<category>Linux Day</category>",
|
||||
} {
|
||||
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: "<pre>a ]]> b</pre>\n<p>Text.</p>"}}
|
||||
got := feed("https://kmalloc.org", feedLang(), "/news/", items, 5)
|
||||
mustContain(t, "feed", got, "<description>Short.</description>", "<dc:creator>Matteo Bini</dc:creator>",
|
||||
`xmlns:dc="http://purl.org/dc/elements/1.1/"`, "<pre>a ]]]]><![CDATA[> b</pre>")
|
||||
if strings.Contains(got, "<description><") || 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 != "<pre>a ]]> b</pre>\n<p>Text.</p>" {
|
||||
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: `<img src="//cdn.example/x.png"> <a href="/p.html">p</a>`}}
|
||||
|
|
|
|||
Loading…
Reference in a new issue