From 9b3b5a54199b23e46e6d1395f5b4cbc2ef68ee35 Mon Sep 17 00:00:00 2001 From: partisan Date: Mon, 26 Aug 2024 10:39:44 +0200 Subject: [PATCH] fix rss images + more compatability hopefully --- rss.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 58 insertions(+), 11 deletions(-) diff --git a/rss.go b/rss.go index 4f5a74f..7b20699 100644 --- a/rss.go +++ b/rss.go @@ -3,10 +3,11 @@ package main import ( "fmt" "net/http" + "strings" "time" "github.com/gorilla/feeds" - "github.com/russross/blackfriday/v2" // Import the Markdown library + "github.com/russross/blackfriday/v2" ) func generateAtomFeed(w http.ResponseWriter, blogs []Blog, siteURL string) { @@ -18,30 +19,53 @@ func generateAtomFeed(w http.ResponseWriter, blogs []Blog, siteURL string) { Created: time.Now(), } + // Add self link + feed.Link = &feeds.Link{Href: fmt.Sprintf("%s/rss", siteURL), Rel: "self"} + for _, blog := range blogs { for _, entry := range blog.Entries { // 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)) + + // Wrap HTML content in + cdataContent := fmt.Sprintf("", absoluteContent) + + // 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 + } + } + feed.Items = append(feed.Items, &feeds.Item{ Title: entry.Title, - Link: &feeds.Link{Href: fmt.Sprintf("%s/%s/%d", siteURL, blog.Name, entry.Number), Rel: "alternate"}, - Description: entry.Description, // This can be used as the summary + Link: &feeds.Link{Href: entryID, Rel: "alternate"}, + Description: summary, // This can be used as the summary Author: &feeds.Author{Name: entry.Author}, - Id: fmt.Sprintf("%s/%s/%d", siteURL, blog.Name, entry.Number), + Id: entryID, Updated: entry.Date, - Content: string(htmlContent), + Content: cdataContent, }) } } + // Generate Atom feed with the correct content-type and charset + w.Header().Set("Content-Type", "application/atom+xml; charset=UTF-8") atom, err := feed.ToAtom() if err != nil { http.Error(w, "Error generating Atom feed", http.StatusInternalServerError) return } - w.Header().Set("Content-Type", "application/atom+xml") w.Write([]byte(atom)) } @@ -54,27 +78,50 @@ func generateBlogAtomFeed(w http.ResponseWriter, blog Blog, siteURL string) { Created: time.Now(), } + // Add self link + feed.Link = &feeds.Link{Href: fmt.Sprintf("%s/%s/rss", siteURL, blog.Name), Rel: "self"} + for _, entry := range blog.Entries { // 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)) + + // Wrap HTML content in + cdataContent := fmt.Sprintf("", absoluteContent) + + // 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 + } + } + feed.Items = append(feed.Items, &feeds.Item{ Title: entry.Title, - Link: &feeds.Link{Href: fmt.Sprintf("%s/%s/%d", siteURL, blog.Name, entry.Number), Rel: "alternate"}, - Description: entry.Description, // This can be used as the summary + Link: &feeds.Link{Href: entryID, Rel: "alternate"}, + Description: summary, // This can be used as the summary Author: &feeds.Author{Name: entry.Author}, - Id: fmt.Sprintf("%s/%s/%d", siteURL, blog.Name, entry.Number), + Id: entryID, Updated: entry.Date, - Content: string(htmlContent), + Content: cdataContent, }) } + // Generate Atom feed with the correct content-type and charset + w.Header().Set("Content-Type", "application/atom+xml; charset=UTF-8") atom, err := feed.ToAtom() if err != nil { http.Error(w, "Error generating Atom feed", http.StatusInternalServerError) return } - w.Header().Set("Content-Type", "application/atom+xml") w.Write([]byte(atom)) }