diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go index 26d3c21..ab9db11 100644 --- a/pkg/cache/cache.go +++ b/pkg/cache/cache.go @@ -51,6 +51,9 @@ func NewFeed(url, title string) *Feed { func (f *Feed) GetItemsOrderedByDate() []*Item { slices.SortFunc(f.Items, func(a, b *Item) bool { + if a.PubDate == util.NoPubDate || b.PubDate == util.NoPubDate { + return strings.Compare(a.Title, b.Title) <= -1 + } return a.PubDate.After(b.PubDate) }) return f.Items @@ -154,7 +157,12 @@ func (c *Cache) AddFeed(parsedFeed *gofeed.Feed, url string) error { newFeed := NewFeed(url, title) for _, parsedItem := range parsedFeed.Items { - cachedItem := NewItem(parsedItem.Title, parsedItem.Link, *parsedItem.PublishedParsed) + + pubDate := util.NoPubDate + if parsedItem.PublishedParsed != nil { + pubDate = *parsedItem.PublishedParsed + } + cachedItem := NewItem(parsedItem.Title, parsedItem.Link, pubDate) newFeed.Items = append(newFeed.Items, cachedItem) } c.feeds = append(c.feeds, newFeed) diff --git a/pkg/util/strings.go b/pkg/util/strings.go index 7202033..60248b8 100644 --- a/pkg/util/strings.go +++ b/pkg/util/strings.go @@ -7,7 +7,12 @@ import ( "golang.org/x/exp/slices" ) +var NoPubDate time.Time = time.Date(1001, 1, 1, 1, 1, 1, 1, time.UTC) + func RenderArticleRow(pubDate time.Time, title string) string { + if pubDate == NoPubDate { + return fmt.Sprintf("%-20s %s", "N/A", title) + } return fmt.Sprintf("%-20s %s", pubDate.Format("2006-January-02"), title) }