Merge pull request #145 from ssrtw/rss-feed-enhance

Rss feed enhance
This commit is contained in:
Svilen Markov 2024-07-03 19:49:32 +01:00 committed by GitHub
commit 9bda88edbd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 28 deletions

View File

@ -44,12 +44,25 @@ func sanitizeFeedDescription(description string) string {
return description
}
func shortenFeedDescriptionLen(description string, maxLen int) string {
description, _ = limitStringLength(description, 1000)
description = sanitizeFeedDescription(description)
description, limited := limitStringLength(description, maxLen)
if limited {
description += "…"
}
return description
}
type RSSFeedRequest struct {
Url string `yaml:"url"`
Title string `yaml:"title"`
HideCategories bool `yaml:"hide-categories"`
HideDescription bool `yaml:"hide-description"`
ItemLinkPrefix string `yaml:"item-link-prefix"`
IsDetailed bool `yaml:"-"`
}
type RSSFeedItems []RSSFeedItem
@ -81,7 +94,6 @@ func getItemsFromRSSFeedTask(request RSSFeedRequest) ([]RSSFeedItem, error) {
rssItem := RSSFeedItem{
ChannelURL: feed.Link,
Title: item.Title,
}
if request.ItemLinkPrefix != "" {
@ -108,34 +120,34 @@ func getItemsFromRSSFeedTask(request RSSFeedRequest) ([]RSSFeedItem, error) {
}
}
if !request.HideDescription && item.Description != "" {
description, _ := limitStringLength(item.Description, 1000)
description = sanitizeFeedDescription(description)
description, limited := limitStringLength(description, 200)
if limited {
description += "…"
}
rssItem.Description = description
if item.Title != "" {
rssItem.Title = item.Title
} else {
rssItem.Title = shortenFeedDescriptionLen(item.Description, 100)
}
if !request.HideCategories {
var categories = make([]string, 0, 6)
for _, category := range item.Categories {
if len(categories) == 6 {
break
}
if len(category) == 0 || len(category) > 30 {
continue
}
categories = append(categories, category)
if request.IsDetailed {
if !request.HideDescription && item.Description != "" && item.Title != "" {
rssItem.Description = shortenFeedDescriptionLen(item.Description, 200)
}
rssItem.Categories = categories
if !request.HideCategories {
var categories = make([]string, 0, 6)
for _, category := range item.Categories {
if len(categories) == 6 {
break
}
if len(category) == 0 || len(category) > 30 {
continue
}
categories = append(categories, category)
}
rssItem.Categories = categories
}
}
if request.Title != "" {

View File

@ -39,10 +39,9 @@ func (widget *RSS) Initialize() error {
widget.CardHeight = 0
}
if widget.Style != "detailed-list" {
if widget.Style == "detailed-list" {
for i := range widget.FeedRequests {
widget.FeedRequests[i].HideCategories = true
widget.FeedRequests[i].HideDescription = true
widget.FeedRequests[i].IsDetailed = true
}
}