Fix links and image sources in rss content

master
Bel LaPointe 2018-10-10 14:02:36 -06:00
parent b7cd1745b1
commit 6c40901465
2 changed files with 17 additions and 3 deletions

View File

@ -60,7 +60,6 @@ func fromGofeedItem(gfitem *gofeed.Item, filter string) *Item {
if content == "" { if content == "" {
content = contentFromLink(item.Link) content = contentFromLink(item.Link)
} }
content = strings.Replace(content, "\n", "", -1)
if filter != "" { if filter != "" {
r := regexp.MustCompile(filter) r := regexp.MustCompile(filter)
matches := r.FindAllString(content, -1) matches := r.FindAllString(content, -1)
@ -81,7 +80,22 @@ func contentFromLink(link string) string {
if err != nil { if err != nil {
return "" 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 { func cleanImgTags(s string) string {

View File

@ -19,7 +19,7 @@ func Test_Server(t *testing.T) {
for _, _ = range cases { for _, _ = range cases {
var err error 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 { if err != nil {
t.Errorf("failed to create server: %v", err) t.Errorf("failed to create server: %v", err)
} }