From 4164263639f5df4ce4d67b85f17ca3e541a3a2be Mon Sep 17 00:00:00 2001 From: Svilen Markov <7613769+svilenmarkov@users.noreply.github.com> Date: Tue, 28 May 2024 18:29:14 +0100 Subject: [PATCH] Fix RSS item links without base domain --- docs/configuration.md | 4 ++++ internal/feed/rss.go | 27 ++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/docs/configuration.md b/docs/configuration.md index f57c0ad..f1126fc 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -431,6 +431,10 @@ An array of RSS/atom feeds. The title can optionally be changed. | ---- | ---- | -------- | ------- | | url | string | yes | | | title | string | no | the title provided by the feed | +| item-link-prefix | string | no | | + +###### `item-link-prefix` +If an RSS feed isn't returning item links with a base domain and Glance has failed to automatically detect the correct domain you can manually add a prefix to each link with this property. ##### `limit` The maximum number of articles to show. diff --git a/internal/feed/rss.go b/internal/feed/rss.go index 6cadc31..4d22af6 100644 --- a/internal/feed/rss.go +++ b/internal/feed/rss.go @@ -5,6 +5,7 @@ import ( "fmt" "html" "log/slog" + "net/url" "regexp" "sort" "strings" @@ -47,6 +48,7 @@ type RSSFeedRequest struct { Title string `yaml:"title"` HideCategories bool `yaml:"hide-categories"` HideDescription bool `yaml:"hide-description"` + ItemLinkPrefix string `yaml:"item-link-prefix"` } type RSSFeedItems []RSSFeedItem @@ -79,7 +81,30 @@ func getItemsFromRSSFeedTask(request RSSFeedRequest) ([]RSSFeedItem, error) { rssItem := RSSFeedItem{ ChannelURL: feed.Link, Title: item.Title, - Link: item.Link, + } + + if request.ItemLinkPrefix != "" { + rssItem.Link = request.ItemLinkPrefix + item.Link + } else if strings.HasPrefix(item.Link, "http://") || strings.HasPrefix(item.Link, "https://") { + rssItem.Link = item.Link + } else { + parsedUrl, err := url.Parse(feed.Link) + + if err != nil { + parsedUrl, err = url.Parse(request.Url) + } + + if err == nil { + var link string + + if item.Link[0] == '/' { + link = item.Link + } else { + link = "/" + item.Link + } + + rssItem.Link = parsedUrl.Scheme + "://" + parsedUrl.Host + link + } } if !request.HideDescription && item.Description != "" {