浏览代码

Fix RSS item links without base domain

Svilen Markov 1 年之前
父节点
当前提交
4164263639
共有 2 个文件被更改,包括 30 次插入1 次删除
  1. 4 0
      docs/configuration.md
  2. 26 1
      internal/feed/rss.go

+ 4 - 0
docs/configuration.md

@@ -431,6 +431,10 @@ An array of RSS/atom feeds. The title can optionally be changed.
 | ---- | ---- | -------- | ------- |
 | ---- | ---- | -------- | ------- |
 | url | string | yes | |
 | url | string | yes | |
 | title | string | no | the title provided by the feed |
 | 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`
 ##### `limit`
 The maximum number of articles to show.
 The maximum number of articles to show.

+ 26 - 1
internal/feed/rss.go

@@ -5,6 +5,7 @@ import (
 	"fmt"
 	"fmt"
 	"html"
 	"html"
 	"log/slog"
 	"log/slog"
+	"net/url"
 	"regexp"
 	"regexp"
 	"sort"
 	"sort"
 	"strings"
 	"strings"
@@ -47,6 +48,7 @@ type RSSFeedRequest struct {
 	Title           string `yaml:"title"`
 	Title           string `yaml:"title"`
 	HideCategories  bool   `yaml:"hide-categories"`
 	HideCategories  bool   `yaml:"hide-categories"`
 	HideDescription bool   `yaml:"hide-description"`
 	HideDescription bool   `yaml:"hide-description"`
+	ItemLinkPrefix  string `yaml:"item-link-prefix"`
 }
 }
 
 
 type RSSFeedItems []RSSFeedItem
 type RSSFeedItems []RSSFeedItem
@@ -79,7 +81,30 @@ func getItemsFromRSSFeedTask(request RSSFeedRequest) ([]RSSFeedItem, error) {
 		rssItem := RSSFeedItem{
 		rssItem := RSSFeedItem{
 			ChannelURL: feed.Link,
 			ChannelURL: feed.Link,
 			Title:      item.Title,
 			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 != "" {
 		if !request.HideDescription && item.Description != "" {