Website/rss.go

122 lines
3.7 KiB
Go
Raw Normal View History

2024-08-14 10:43:07 +00:00
package main
import (
"fmt"
"net/http"
"strings"
2024-08-14 10:43:07 +00:00
"time"
"github.com/gorilla/feeds"
"github.com/russross/blackfriday/v2"
2024-08-14 10:43:07 +00:00
)
2024-08-22 20:10:47 +00:00
func generateAtomFeed(w http.ResponseWriter, blogs []Blog, siteURL string) {
2024-08-14 10:43:07 +00:00
feed := &feeds.Feed{
2024-08-22 20:10:47 +00:00
Title: "Spitfire News",
2024-08-14 10:43:07 +00:00
Link: &feeds.Link{Href: siteURL},
2024-08-22 20:10:47 +00:00
Description: "Blog about Spitfire browser news/updates.", // Blog subtitle (Atom uses "subtitle" for description)
Author: &feeds.Author{Name: "Internet Addict", Email: "noone@none.no"},
2024-08-14 10:43:07 +00:00
Created: time.Now(),
}
// Add self link
feed.Link = &feeds.Link{Href: fmt.Sprintf("%s/rss", siteURL), Rel: "self"}
2024-08-14 10:43:07 +00:00
for _, blog := range blogs {
for _, entry := range blog.Entries {
2024-08-22 20:10:47 +00:00
// Convert Markdown content to HTML
htmlContent := blackfriday.Run([]byte(entry.Content))
// Ensure all image paths are absolute URLs (Idiot proofing)
absoluteContent := strings.ReplaceAll(string(htmlContent), "src=\"/", fmt.Sprintf("src=\"%s/", siteURL))
// Ensure unique and stable ID
entryID := fmt.Sprintf("%s/%s/%d", siteURL, blog.Name, entry.Number)
// Create a summary if needed (using the first 200 characters of the content, for example)
summary := entry.Description
if summary == "" {
if len(entry.Content) > 200 {
summary = entry.Content[:200] + "..."
} else {
summary = entry.Content
}
}
2024-08-14 10:43:07 +00:00
feed.Items = append(feed.Items, &feeds.Item{
2024-08-22 20:10:47 +00:00
Title: entry.Title,
Link: &feeds.Link{Href: entryID, Rel: "alternate"},
Description: summary, // This can be used as the summary
2024-08-22 20:10:47 +00:00
Author: &feeds.Author{Name: entry.Author},
Id: entryID,
2024-08-22 20:10:47 +00:00
Updated: entry.Date,
2024-09-09 19:56:40 +00:00
Content: absoluteContent,
2024-08-14 10:43:07 +00:00
})
}
}
// Generate Atom feed with the correct content-type and charset
w.Header().Set("Content-Type", "application/atom+xml; charset=UTF-8")
2024-08-22 20:10:47 +00:00
atom, err := feed.ToAtom()
2024-08-14 10:43:07 +00:00
if err != nil {
2024-08-22 20:10:47 +00:00
http.Error(w, "Error generating Atom feed", http.StatusInternalServerError)
2024-08-14 10:43:07 +00:00
return
}
2024-08-22 20:10:47 +00:00
w.Write([]byte(atom))
2024-08-14 10:43:07 +00:00
}
2024-08-22 20:10:47 +00:00
func generateBlogAtomFeed(w http.ResponseWriter, blog Blog, siteURL string) {
2024-08-14 10:43:07 +00:00
feed := &feeds.Feed{
Title: blog.Name,
Link: &feeds.Link{Href: fmt.Sprintf("%s/%s", siteURL, blog.Name)},
2024-08-22 20:10:47 +00:00
Description: blog.Name, // Blog subtitle (Atom uses "subtitle" for description)
Author: &feeds.Author{Name: "Internet Addict", Email: "noone@none.no"},
2024-08-14 10:43:07 +00:00
Created: time.Now(),
}
// Add self link
feed.Link = &feeds.Link{Href: fmt.Sprintf("%s/%s/rss", siteURL, blog.Name), Rel: "self"}
2024-08-14 10:43:07 +00:00
for _, entry := range blog.Entries {
2024-08-22 20:10:47 +00:00
// Convert Markdown content to HTML
htmlContent := blackfriday.Run([]byte(entry.Content))
// Ensure all image paths are absolute URLs (Idiot proofing)
absoluteContent := strings.ReplaceAll(string(htmlContent), "src=\"/", fmt.Sprintf("src=\"%s/", siteURL))
// Ensure unique and stable ID
entryID := fmt.Sprintf("%s/%s/%d", siteURL, blog.Name, entry.Number)
// Create a summary if needed (using the first 200 characters of the content, for example)
summary := entry.Description
if summary == "" {
if len(entry.Content) > 200 {
summary = entry.Content[:200] + "..."
} else {
summary = entry.Content
}
}
2024-08-14 10:43:07 +00:00
feed.Items = append(feed.Items, &feeds.Item{
2024-08-22 20:10:47 +00:00
Title: entry.Title,
Link: &feeds.Link{Href: entryID, Rel: "alternate"},
Description: summary, // This can be used as the summary
2024-08-22 20:10:47 +00:00
Author: &feeds.Author{Name: entry.Author},
Id: entryID,
2024-08-22 20:10:47 +00:00
Updated: entry.Date,
2024-09-09 19:56:40 +00:00
Content: absoluteContent,
2024-08-14 10:43:07 +00:00
})
}
// Generate Atom feed with the correct content-type and charset
w.Header().Set("Content-Type", "application/atom+xml; charset=UTF-8")
2024-08-22 20:10:47 +00:00
atom, err := feed.ToAtom()
2024-08-14 10:43:07 +00:00
if err != nil {
2024-08-22 20:10:47 +00:00
http.Error(w, "Error generating Atom feed", http.StatusInternalServerError)
2024-08-14 10:43:07 +00:00
return
}
2024-08-22 20:10:47 +00:00
w.Write([]byte(atom))
2024-08-14 10:43:07 +00:00
}