fix getFirstImageOf(..)

This commit is contained in:
Duong Thai 2024-03-11 17:45:45 +07:00
parent abda51ec58
commit d63436b1d9
2 changed files with 13 additions and 4 deletions

View file

@ -63,7 +63,7 @@ extension ParsedItem {
(url: imageURL, size: image.size)
} else if
let contentHTML = self.contentHTML,
let imageURL = RSSTools.getFirstImageOf(html: contentHTML),
let imageURL = RSSTools.getFirstImageOf(html: contentHTML, baseURL: self.getRSSURL()),
let imageData = try? Data(contentsOf: imageURL),
let image = UIImage(data: imageData)
{

View file

@ -103,9 +103,18 @@ public enum RSSTools {
return NonEmptyString(String(match.1))
}
public static func getFirstImageOf(html: String) -> URL? {
guard let match = html.firstMatch(of: /<img[\s\S]+?src="(.+?)"/) else { return nil }
return URL(string: String(match.1))
public static func getFirstImageOf(html: String, baseURL: URL?) -> URL? {
guard let match = html.firstMatch(of: /<img[\s\S]+?src="(.+?)"/),
let url = URL(string: String(match.1))
else { return nil }
return if url.host != nil {
url
} else if let host = baseURL?.host() {
URL(string: "https://\(host)\(url.absoluteString)")
} else {
nil
}
}
public static func getFeedData(from feedURL: URL) async -> RSSFeed.SendableData? {