fix(posts): updated rss feed to render body content and images
2b0f8ad4
2 file(s) · +17 −10
| 212 | 212 | title = xmlEscape(t) |
|
| 213 | 213 | } |
|
| 214 | 214 | } |
|
| 215 | - | desc := "" |
|
| 215 | + | content := absolutizeURLs(renderMarkdown(p.Content), a.SiteURL) |
|
| 216 | + | desc := cdata(content) |
|
| 216 | 217 | if p.MetaDescription != nil && *p.MetaDescription != "" { |
|
| 217 | 218 | desc = xmlEscape(*p.MetaDescription) |
|
| 218 | - | } else { |
|
| 219 | - | runes := []rune(p.Content) |
|
| 220 | - | n := 200 |
|
| 221 | - | if len(runes) < n { |
|
| 222 | - | n = len(runes) |
|
| 223 | - | } |
|
| 224 | - | desc = xmlEscape(string(runes[:n])) |
|
| 225 | 219 | } |
|
| 226 | 220 | rawDate := p.CreatedAt |
|
| 227 | 221 | if p.PublishedDate != nil { |
|
| 228 | 222 | rawDate = *p.PublishedDate |
|
| 229 | 223 | } |
|
| 230 | 224 | pubDate := toRFC2822(rawDate) |
|
| 231 | - | items.WriteString(" <item>\n <title>" + title + "</title>\n <link>" + link + "</link>\n <guid>" + link + "</guid>\n <description>" + desc + "</description>\n <pubDate>" + pubDate + "</pubDate>\n </item>\n") |
|
| 225 | + | items.WriteString(" <item>\n <title>" + title + "</title>\n <link>" + link + "</link>\n <guid>" + link + "</guid>\n <description>" + desc + "</description>\n <content:encoded>" + cdata(content) + "</content:encoded>\n <pubDate>" + pubDate + "</pubDate>\n </item>\n") |
|
| 232 | 226 | } |
|
| 233 | 227 | lastBuild := "" |
|
| 234 | 228 | if len(posts) > 0 && posts[0].PublishedDate != nil { |
|
| 235 | 229 | lastBuild = toRFC2822(*posts[0].PublishedDate) |
|
| 236 | 230 | } |
|
| 237 | 231 | out := `<?xml version="1.0" encoding="UTF-8"?> |
|
| 238 | - | <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"> |
|
| 232 | + | <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"> |
|
| 239 | 233 | <channel> |
|
| 240 | 234 | <title>` + xmlEscape(blogTitle) + `</title> |
|
| 241 | 235 | <link>` + a.SiteURL + `</link> |
| 499 | 499 | } |
|
| 500 | 500 | return base + "." + ext |
|
| 501 | 501 | } |
|
| 502 | + | ||
| 503 | + | // cdata wraps s in a CDATA section, splitting any "]]>" so it can't terminate it early. |
|
| 504 | + | func cdata(s string) string { |
|
| 505 | + | return "<![CDATA[" + strings.ReplaceAll(s, "]]>", "]]]]><![CDATA[>") + "]]>" |
|
| 506 | + | } |
|
| 507 | + | ||
| 508 | + | var rootRelativeAttr = regexp.MustCompile(`(?i)\b(src|href|poster)=(["'])/([^/])`) |
|
| 509 | + | ||
| 510 | + | // absolutizeURLs rewrites root-relative src/href/poster attributes (e.g. "/files/x.png") |
|
| 511 | + | // to absolute URLs so feed readers can resolve them. |
|
| 512 | + | func absolutizeURLs(html, siteURL string) string { |
|
| 513 | + | return rootRelativeAttr.ReplaceAllString(html, `${1}=${2}`+siteURL+`/${3}`) |
|
| 514 | + | } |