diff --git a/reader/rewrite/rewrite_functions.go b/reader/rewrite/rewrite_functions.go index 6717deb9..412266db 100644 --- a/reader/rewrite/rewrite_functions.go +++ b/reader/rewrite/rewrite_functions.go @@ -22,9 +22,19 @@ func addImageTitle(entryURL, entryContent string) string { return entryContent } - imgTag := doc.Find("img").First() - if titleAttr, found := imgTag.Attr("title"); found { - return entryContent + `
` + titleAttr + "
" + matches := doc.Find("img[src][title]") + + if matches.Length() > 0 { + matches.Each(func(i int, img *goquery.Selection) { + altAttr := img.AttrOr("alt", "") + srcAttr, _ := img.Attr("src") + titleAttr, _ := img.Attr("title") + + img.ReplaceWithHtml(`
` + altAttr + `

` + titleAttr + `

`) + }) + + output, _ := doc.Find("body").First().Html() + return output } return entryContent diff --git a/reader/rewrite/rewriter_test.go b/reader/rewrite/rewriter_test.go index 995508b0..7a33b1f8 100644 --- a/reader/rewrite/rewriter_test.go +++ b/reader/rewrite/rewriter_test.go @@ -35,7 +35,15 @@ func TestRewriteWithInexistingCustomRule(t *testing.T) { func TestRewriteWithXkcdLink(t *testing.T) { description := `Your problem is so terrible, I worry that, if I help you, I risk drawing the attention of whatever god of technology inflicted it on you.` output := Rewriter("https://xkcd.com/1912/", description, ``) - expected := description + `
Your problem is so terrible, I worry that, if I help you, I risk drawing the attention of whatever god of technology inflicted it on you.
` + expected := `
Your problem is so terrible, I worry that, if I help you, I risk drawing the attention of whatever god of technology inflicted it on you.

Your problem is so terrible, I worry that, if I help you, I risk drawing the attention of whatever god of technology inflicted it on you.

` + if expected != output { + t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) + } +} +func TestRewriteWithXkcdLinkAndImageNoTitle(t *testing.T) { + description := `Your problem is so terrible, I worry that, if I help you, I risk drawing the attention of whatever god of technology inflicted it on you.` + output := Rewriter("https://xkcd.com/1912/", description, ``) + expected := description if expected != output { t.Errorf(`Not expected output: got "%s" instead of "%s"`, output, expected) }