mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2026-12-03 10:47:21 +00:00
Add special case system for placeholder images, so they’re not treated as thumbnails or as image to show in the article.
This commit is contained in:
1 parent
f3eb190e78
commit
69acad4d06
4 files changed
+34
-8
No files matched your search
@@ -71,7 +71,7 @@ extension Notification.Name {
|
||||
return chosenThumbnailURL.thumbnailURL
|
||||
}
|
||||
|
||||
let thumbnailURL = providedThumbnailURL(for: article, mode: mode) ?? cachedOrParsedHTMLThumbnailURLs(for: article).first { !hasFailed($0) } ?? usableProvidedURL(Self.normalizedURLString(article.rawThumbnailLink))
|
||||
let thumbnailURL = providedThumbnailURL(for: article, mode: mode) ?? cachedOrParsedHTMLThumbnailURLs(for: article).first { !hasFailed($0) } ?? usableProvidedURL(Self.thumbnailLink(article))
|
||||
if chosenThumbnailURLs[mode, default: [:]].count >= Self.maxChosenThumbnailURLCount {
|
||||
chosenThumbnailURLs[mode] = nil
|
||||
}
|
||||
@@ -208,7 +208,7 @@ private extension ArticleThumbnailFinder {
|
||||
func providedThumbnailURL(for article: Article, mode: ThumbnailMode) -> String? {
|
||||
switch mode {
|
||||
case .small:
|
||||
usableProvidedURL(Self.normalizedURLString(article.rawThumbnailLink)) ?? usableProvidedURL(Self.normalizedURLString(article.rawImageLink)) ?? usableProvidedURL(Self.firstSignificantImageAttachmentURL(article))
|
||||
usableProvidedURL(Self.thumbnailLink(article)) ?? usableProvidedURL(Self.normalizedURLString(article.rawImageLink)) ?? usableProvidedURL(Self.firstSignificantImageAttachmentURL(article))
|
||||
case .large:
|
||||
usableProvidedURL(Self.normalizedURLString(article.rawImageLink)) ?? usableProvidedURL(Self.firstSignificantImageAttachmentURL(article))
|
||||
}
|
||||
@@ -252,6 +252,14 @@ private extension ArticleThumbnailFinder {
|
||||
return thumbnailURLs
|
||||
}
|
||||
|
||||
/// Nil when the article’s image is a placeholder, since a sync service’s thumbnail is its crop of that image.
|
||||
static func thumbnailLink(_ article: Article) -> String? {
|
||||
if let rawImageLink = article.rawImageLink, SignificantImageFilter.isPlaceholder(urlString: rawImageLink) {
|
||||
return nil
|
||||
}
|
||||
return normalizedURLString(article.rawThumbnailLink)
|
||||
}
|
||||
|
||||
static func firstSignificantImageAttachmentURL(_ article: Article) -> String? {
|
||||
guard let attachments = article.attachments else {
|
||||
return nil
|
||||
|
||||
@@ -24,6 +24,14 @@ public enum SignificantImageFilter {
|
||||
hasSignificantDimensions(width: width, height: height) && !isBlockedURL(urlString) && !hasDecorativeClass(htmlClass) && !isSharingLink(enclosingLinkURLString)
|
||||
}
|
||||
|
||||
/// A site’s stand-in for an article with no image of its own.
|
||||
public static func isPlaceholder(urlString: String) -> Bool {
|
||||
guard let url = URL(string: urlString), let host = url.host()?.lowercased() else {
|
||||
return false
|
||||
}
|
||||
return matches(host: host, path: url.path().lowercased(), placeholderImagePrefixes)
|
||||
}
|
||||
|
||||
public static func hasSignificantDimensions(width: Int?, height: Int?) -> Bool {
|
||||
if let width, width < minimumDimension {
|
||||
return false
|
||||
@@ -67,6 +75,11 @@ private extension SignificantImageFilter {
|
||||
(host: "stats.wordpress.com", pathPrefix: "/")
|
||||
]
|
||||
|
||||
/// Hosts include their subdomains. Path prefixes are lowercase.
|
||||
static let placeholderImagePrefixes: [HostAndPathPrefix] = [
|
||||
(host: "boston.com", pathPrefix: "/wp-content/themes/bdc-2020/assets/images/feed-placeholder.png")
|
||||
]
|
||||
|
||||
/// Share endpoints. Hosts include their subdomains. Path prefixes are lowercase.
|
||||
static let sharingLinkPrefixes: [HostAndPathPrefix] = [
|
||||
(host: "addtoany.com", pathPrefix: "/"),
|
||||
@@ -111,7 +124,7 @@ private extension SignificantImageFilter {
|
||||
if !photoFileExtensions.contains(url.pathExtension.lowercased()), url.pathComponents.contains(where: { blockedPathComponents.contains($0.lowercased()) }) {
|
||||
return true
|
||||
}
|
||||
if matches(host: host, path: url.path().lowercased(), blockedImagePrefixes) {
|
||||
if matches(host: host, path: url.path().lowercased(), blockedImagePrefixes) || isPlaceholder(urlString: urlString) {
|
||||
return true
|
||||
}
|
||||
return MediaRSS.isGravatar(url)
|
||||
|
||||
@@ -163,6 +163,7 @@ import RSParser
|
||||
#expect(!SignificantImageFilter.isSignificant(urlString: "https://example.com/logo.svg", width: nil, height: nil))
|
||||
#expect(!SignificantImageFilter.isSignificant(urlString: "https://s0.wp.com/i/blank.jpg", width: nil, height: nil))
|
||||
#expect(!SignificantImageFilter.isSignificant(urlString: "https://pixel.wp.com/b.gif?v=1", width: nil, height: nil))
|
||||
#expect(!SignificantImageFilter.isSignificant(urlString: "https://www.boston.com/wp-content/themes/bdc-2020/assets/images/feed-placeholder.png", width: nil, height: nil))
|
||||
#expect(!SignificantImageFilter.isSignificant(urlString: "ftp://example.com/a.jpg", width: nil, height: nil))
|
||||
#expect(!SignificantImageFilter.isSignificant(urlString: "https://example.com/banner.jpg", width: 1200, height: 150))
|
||||
#expect(!SignificantImageFilter.isSignificant(urlString: "https://example.com/images/spacer.gif", width: nil, height: nil))
|
||||
|
||||
@@ -288,26 +288,30 @@ private extension ArticleRenderer {
|
||||
// Templates without [[primary_media]], and short untitled posts, get the article image in the enclosures.
|
||||
// The thumbnail is used only for [[primary_media]].
|
||||
let bodyImageIdentities = Self.bodyImageIdentities(body, baseURL: baseURL)
|
||||
var attachments = article.attachments ?? []
|
||||
var attachments = (article.attachments ?? []).filter { !SignificantImageFilter.isPlaceholder(urlString: $0.url) }
|
||||
// A sync service’s thumbnail is its crop of the article image, so it’s a placeholder too.
|
||||
let hasPlaceholderImage = SignificantImageFilter.isPlaceholder(urlString: article.imageLink ?? "")
|
||||
let imageLink = hasPlaceholderImage ? nil : article.imageLink
|
||||
let thumbnailLink = hasPlaceholderImage ? nil : article.thumbnailLink
|
||||
// Feedbin’s image is also the timeline thumbnail, so it’s shown instead of the feed’s image enclosure.
|
||||
if article.account?.type == .feedbin, let imageLink = article.imageLink, !imageLink.isEmpty {
|
||||
if article.account?.type == .feedbin, let imageLink, !imageLink.isEmpty {
|
||||
attachments.removeAll { $0.displayMedium == .image }
|
||||
}
|
||||
var primaryMediaHTML = ""
|
||||
var primaryMediaURLs = Set<String>()
|
||||
let hasPrimaryMedia = template().contains(Self.primaryMediaMacro)
|
||||
let showsPrimaryImage = hasPrimaryMedia && !Self.isShortUntitledPost(title: article.title, body: body)
|
||||
if hasPrimaryMedia, let primaryMedia = Self.primaryMedia(imageLink: article.imageLink, thumbnailLink: article.thumbnailLink, attachments: attachments, articleTitle: article.title, body: body, bodyImageIdentities: bodyImageIdentities, includesImage: showsPrimaryImage) {
|
||||
if hasPrimaryMedia, let primaryMedia = Self.primaryMedia(imageLink: imageLink, thumbnailLink: thumbnailLink, attachments: attachments, articleTitle: article.title, body: body, bodyImageIdentities: bodyImageIdentities, includesImage: showsPrimaryImage) {
|
||||
primaryMediaHTML = primaryMedia.html
|
||||
primaryMediaURLs = primaryMedia.attachmentURLs
|
||||
attachments.removeAll { primaryMediaURLs.contains($0.url) }
|
||||
}
|
||||
if !showsPrimaryImage, let primaryImage = Self.primaryImage(imageLink: article.imageLink, thumbnailLink: nil, attachments: attachments, body: body, bodyImageIdentities: bodyImageIdentities), !primaryMediaURLs.contains(primaryImage.url), !attachments.contains(primaryImage) {
|
||||
if !showsPrimaryImage, let primaryImage = Self.primaryImage(imageLink: imageLink, thumbnailLink: nil, attachments: attachments, body: body, bodyImageIdentities: bodyImageIdentities), !primaryMediaURLs.contains(primaryImage.url), !attachments.contains(primaryImage) {
|
||||
attachments.insert(primaryImage, at: 0)
|
||||
}
|
||||
|
||||
// Templates without [[enclosures]] get them at the end of the body.
|
||||
var enclosures = Self.enclosuresHTML(attachments: attachments, body: body, bodyImageIdentities: bodyImageIdentities, posterURL: article.imageLink ?? article.thumbnailLink)
|
||||
var enclosures = Self.enclosuresHTML(attachments: attachments, body: body, bodyImageIdentities: bodyImageIdentities, posterURL: imageLink ?? thumbnailLink)
|
||||
var body = body
|
||||
if routesThumbnailImages, let thumbnailMode = AppDefaults.timelineThumbnailsDisplay.thumbnailMode, let thumbnailURL = ArticleThumbnailFinder.shared.thumbnailURL(for: article, mode: thumbnailMode) {
|
||||
body = Self.routingThumbnailImages(body, thumbnailURL: thumbnailURL, baseURL: baseURL)
|
||||
|
||||
Reference in new issue
Block a user