Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7a491f4

Browse files
committedDec 1, 2024··
fix: rss feed
fix: rss feed fix: rss feed fix: rss feed fix: rss feed
1 parent 064ff72 commit 7a491f4

File tree

3 files changed

+53
-16
lines changed

3 files changed

+53
-16
lines changed
 

‎pkg/jatt.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
type Jatt struct {
1616
reader *reader.Reader
17-
rss *rss.Rss
17+
rss *rss.RssSvc
1818
renderer *renderer.Renderer
1919
writer *writer.Writer
2020
}
@@ -32,8 +32,8 @@ func (j *Jatt) Run() {
3232
timeStart := time.Now()
3333

3434
files := j.reader.Read()
35-
feed := j.rss.GenerateFeed(files)
3635
renderedFiles := j.renderer.Render(files)
36+
feed := j.rss.GenerateFeed(files, renderedFiles)
3737
j.writer.WriteFiles(renderedFiles)
3838
j.writer.WriteRSSFeed(feed)
3939
j.writer.CopyStatic()

‎pkg/renderer/renderer.go

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type Renderer struct {
2323
type RenderedFile struct {
2424
Name string
2525
Html []byte
26+
ContentHtml []byte
2627
FrontMatter reader.FrontMatter
2728
}
2829

@@ -61,6 +62,7 @@ func (r *Renderer) Render(files []reader.File) []RenderedFile {
6162
renderedFiles = append(renderedFiles, RenderedFile{
6263
Name: file.Name,
6364
Html: buf.Bytes(),
65+
ContentHtml: html,
6466
FrontMatter: file.FrontMatter,
6567
})
6668

‎pkg/rss/rss.go

+49-14
Original file line numberDiff line numberDiff line change
@@ -7,55 +7,90 @@ import (
77

88
"github.com/devmegablaster/jatt/internal/config"
99
"github.com/devmegablaster/jatt/pkg/reader"
10+
"github.com/devmegablaster/jatt/pkg/renderer"
1011
"github.com/devmegablaster/jatt/pkg/styles"
1112
)
1213

13-
type Rss struct {
14+
type RssSvc struct {
1415
cfg config.JattConfig
1516
}
1617

17-
func New(cfg config.JattConfig) *Rss {
18-
return &Rss{
18+
func New(cfg config.JattConfig) *RssSvc {
19+
return &RssSvc{
1920
cfg: cfg,
2021
}
2122
}
2223

23-
type Feed struct {
24-
Title string `xml:"title"`
25-
Description string `xml:"description"`
26-
Link string `xml:"link"`
27-
PubDate time.Time `xml:"pubDate"`
28-
Items []reader.ListingItem `xml:"item"`
24+
type Rss struct {
25+
XMLName xml.Name `xml:"rss"`
26+
Version string `xml:"version,attr"`
27+
Channel Channel `xml:"channel"`
28+
}
29+
30+
type Channel struct {
31+
Title string `xml:"title"`
32+
Description string `xml:"description"`
33+
Link string `xml:"link"`
34+
PubDate time.Time `xml:"pubDate"`
35+
Items []Item `xml:"item"`
36+
}
37+
38+
type Item struct {
39+
Title string `xml:"title"`
40+
Link string `xml:"link"`
41+
Description string `xml:"description"`
42+
PubDate time.Time `xml:"pubDate"`
43+
Content string `xml:"content:encoded"`
2944
}
3045

31-
func (r *Rss) GenerateFeed(files []reader.File) []byte {
32-
posts := []reader.ListingItem{}
46+
func (r *RssSvc) GenerateFeed(files []reader.File, renderedFiles []renderer.RenderedFile) []byte {
47+
posts := []Item{}
3348

3449
for _, file := range files {
3550
if file.FrontMatter.Draft {
3651
continue
3752
}
3853

3954
if file.FrontMatter.Layout == "listing" {
40-
for _, item := range file.Listing {
41-
posts = append(posts, item)
55+
for index, item := range file.Listing {
56+
t, err := time.Parse("2006-01-02", item.Date)
57+
if err != nil {
58+
t = time.Now()
59+
fmt.Println(styles.ErrorStyle.Render("Error parsing date"))
60+
fmt.Println(styles.ErrorStyle.Render(err.Error()))
61+
}
62+
63+
posts = append(posts, Item{
64+
Title: item.Title,
65+
Link: item.URL,
66+
Description: item.Description,
67+
PubDate: t,
68+
Content: string(renderedFiles[index].ContentHtml),
69+
})
4270
}
4371
}
4472
}
4573

46-
feed := Feed{
74+
channel := Channel{
4775
Title: r.cfg.SiteConfig.Title,
4876
Description: r.cfg.SiteConfig.Description,
4977
Link: r.cfg.SiteConfig.BaseURL,
5078
PubDate: time.Now(),
5179
Items: posts,
5280
}
5381

82+
feed := Rss{
83+
Version: "2.0",
84+
Channel: channel,
85+
}
86+
5487
enc, err := xml.MarshalIndent(&feed, "", " ")
5588
if err != nil {
5689
fmt.Println(styles.ErrorStyle.Render("Error generating RSS feed"))
5790
}
5891

92+
enc = []byte(xml.Header + string(enc))
93+
5994
fmt.Println(styles.DebugStyle.Render("Generated RSS feed"))
6095

6196
return enc

0 commit comments

Comments
 (0)
Please sign in to comment.