improve rss

This commit is contained in:
Niko Abeler 2023-07-21 21:40:32 +02:00
parent e31dbeb18c
commit d736679418
1 changed files with 32 additions and 20 deletions

View File

@ -19,10 +19,15 @@ type RSS struct {
Channel RSSChannel `xml:"channel"` Channel RSSChannel `xml:"channel"`
} }
type RSSDescription struct {
XMLName xml.Name `xml:"description"`
Text string `xml:",cdata"`
}
type RSSChannel struct { type RSSChannel struct {
Title string `xml:"title"` Title string `xml:"title"`
Link string `xml:"link"` Link string `xml:"link"`
Description string `xml:"description"` Description RSSDescription `xml:"description"`
Items []RSSItem `xml:"item"` Items []RSSItem `xml:"item"`
} }
@ -31,7 +36,7 @@ type RSSItem struct {
Title string `xml:"title"` Title string `xml:"title"`
Link string `xml:"link"` Link string `xml:"link"`
PubDate string `xml:"pubDate"` PubDate string `xml:"pubDate"`
Description string `xml:"description"` Description RSSDescription `xml:"description"`
} }
func RenderRSSFeed(config model.SiteConfig, entries []model.Entry) (string, error) { func RenderRSSFeed(config model.SiteConfig, entries []model.Entry) (string, error) {
@ -41,25 +46,32 @@ func RenderRSSFeed(config model.SiteConfig, entries []model.Entry) (string, erro
Channel: RSSChannel{ Channel: RSSChannel{
Title: config.Title, Title: config.Title,
Link: config.FullUrl, Link: config.FullUrl,
Description: config.SubTitle, Description: RSSDescription{
Text: config.SubTitle,
},
Items: make([]RSSItem, 0), Items: make([]RSSItem, 0),
}, },
} }
for _, entry := range entries { for _, entry := range entries {
content := entry.Content() content := entry.Content()
url, _ := url.JoinPath(config.FullUrl, "/posts/", entry.ID(), "/") entryUrl, _ := url.JoinPath(config.FullUrl, "/posts/", url.PathEscape(entry.ID()), "/")
rss.Channel.Items = append(rss.Channel.Items, RSSItem{ rss.Channel.Items = append(rss.Channel.Items, RSSItem{
Guid: url, Guid: entryUrl,
Title: entry.Title(), Title: entry.Title(),
Link: url, Link: entryUrl,
PubDate: entry.PublishedAt().Format(time.RFC1123Z), PubDate: entry.PublishedAt().Format(time.RFC1123Z),
Description: string(content), Description: RSSDescription{
Text: string(content),
},
}) })
} }
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
err := xml.NewEncoder(buf).Encode(rss) encoder := xml.NewEncoder(buf)
encoder.Indent("", " ")
err := encoder.Encode(rss)
if err != nil { if err != nil {
return "", err return "", err
} }