1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-06-27 16:36:00 +00:00

Refactor AddImageTitle rewriter.

* Only processes images with `src` **and** `title` attributes (others are ignored).
* Processes **all** images in the document (not just the first one).
* Wraps the image and its title attribute in a `figure` tag with the title attribute's contents in a `figcaption` tag.

Updated xkcd rewriter unit test.

Added another xkcd rewriter unit test to check rendering of images without title tags.
This commit is contained in:
dzaikos 2018-06-26 17:39:56 -04:00
parent c9131b0e89
commit 45d7105ed1
2 changed files with 22 additions and 4 deletions

View file

@ -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 + `<blockquote cite="` + entryURL + `">` + titleAttr + "</blockquote>"
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(`<figure><img src="` + srcAttr + `" alt="` + altAttr + `"/><figcaption><p>` + titleAttr + `</p></figcaption></figure>`)
})
output, _ := doc.Find("body").First().Html()
return output
}
return entryContent