motorino/internal/build/feed.go
tgiachi 4ab8f8d122
All checks were successful
check / check (pull_request) Successful in 30s
fix: find code blocks with the Markdown parser and make the escape work everywhere
The hand written fence scanner took a line that starts with a code span for a
fence and missed fences in quotes and lists. The code blocks are now the ones
goldmark finds. A backslash before a placeholder is turned into a marker that
survives the Markdown and the list expansion, so it works in a code span and
next to a block name, and two backslashes are a literal one. The em dash rule
reports the line of the body, refuses padded entities and checks the header keys
by name, the feed leaves protocol-relative links alone, base_url needs a host
and a language file cannot set it.
2026-11-15 10:38:56 +01:00

54 lines
2 KiB
Go

// SPDX-License-Identifier: GPL-3.0-or-later
package build
import (
"regexp"
"strings"
"time"
"git.kmalloc.org/squid/motorino/internal/render"
"git.kmalloc.org/squid/motorino/internal/site"
)
// siteRelative matches a link that starts at the root of the site, such as
// href="/people.html", but not one that is relative to the protocol (href="//host/x").
var siteRelative = regexp.MustCompile(`\b(href|src)="/([^/])`)
// 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/".
func feed(base string, l site.Lang, newsURL string, posts []renderedPost, limit int) string {
if limit < len(posts) {
posts = posts[:limit]
}
var items strings.Builder
for _, p := range posts {
link := base + p.Path
text := siteRelative.ReplaceAllString(p.HTML, `$1="`+base+`/$2`)
items.WriteString("<item>\n")
items.WriteString("<title>" + render.Escape(p.Title) + "</title>\n")
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")
items.WriteString("</item>\n")
}
built := ""
if len(posts) > 0 {
built = "<lastBuildDate>" + posts[0].Date.Format(time.RFC1123Z) + "</lastBuildDate>\n"
}
url := base + newsURL
return `<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>kmalloc.org: ` + render.Escape(l.Nav["news"]) + `</title>
<link>` + url + `</link>
<description>` + render.Escape(l.Strings["news_p"]) + `</description>
<language>` + l.Code + `</language>
` + built + `<atom:link href="` + url + `feed.xml" rel="self" type="application/rss+xml"/>
` + items.String() + `</channel>
</rss>
`
}