fix rss images + more compatability hopefully
This commit is contained in:
parent
50a1a14f41
commit
9b3b5a5419
1 changed files with 58 additions and 11 deletions
69
rss.go
69
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 <![CDATA[]]>
|
||||
cdataContent := fmt.Sprintf("<![CDATA[%s]]>", 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 <![CDATA[]]>
|
||||
cdataContent := fmt.Sprintf("<![CDATA[%s]]>", 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))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue