From 6c40901465f323d25e51e586e2825aca310964a9 Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Wed, 10 Oct 2018 14:02:36 -0600 Subject: [PATCH] Fix links and image sources in rss content --- rss/item.go | 18 ++++++++++++++++-- server/server_test.go | 2 +- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/rss/item.go b/rss/item.go index a60a978..8d40dbe 100644 --- a/rss/item.go +++ b/rss/item.go @@ -60,7 +60,6 @@ func fromGofeedItem(gfitem *gofeed.Item, filter string) *Item { if content == "" { content = contentFromLink(item.Link) } - content = strings.Replace(content, "\n", "", -1) if filter != "" { r := regexp.MustCompile(filter) matches := r.FindAllString(content, -1) @@ -81,7 +80,22 @@ func contentFromLink(link string) string { if err != nil { return "" } - return string(b) + protocol := strings.Split(link, ":")[0] + "://" + if !strings.HasPrefix(protocol, "http") { + protocol = "" + } + content := strings.Replace(string(b), "\n", "", -1) + // fix all //img.link/something.jpg + badSrc := regexp.MustCompile("\"\\/\\/") + content = badSrc.ReplaceAllString(content, "\""+protocol) + // fix all href="/path/to" + host := protocol + strings.Split(link[len(protocol):], "/")[0] + "/" + badHref := regexp.MustCompile("href=\"\\/") + content = badHref.ReplaceAllString(content, "href=\""+host) + // fix all src="/path/to" + badPathSrc := regexp.MustCompile("src=\"\\/") + content = badPathSrc.ReplaceAllString(content, "src=\""+host) + return content } func cleanImgTags(s string) string { diff --git a/server/server_test.go b/server/server_test.go index 8574d24..55ecc4e 100644 --- a/server/server_test.go +++ b/server/server_test.go @@ -19,7 +19,7 @@ func Test_Server(t *testing.T) { for _, _ = range cases { var err error - s, err := New(testPort, func(string, string, string, time.Duration) {}, func(string, int) (string, error) { return "", nil }) + s, err := New(testPort, func(string, string, string, time.Duration) {}, func(string, int) (string, error) { return "", nil }, func(string) (string, error) { return "", nil }) if err != nil { t.Errorf("failed to create server: %v", err) }