From 69acad4d06a48812aae6bbb57da2bddf07847efb Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Wed, 2 Dec 2026 21:56:19 -0800 Subject: [PATCH] =?UTF-8?q?Add=20special=20case=20system=20for=20placehold?= =?UTF-8?q?er=20images,=20so=20they=E2=80=99re=20not=20treated=20as=20thum?= =?UTF-8?q?bnails=20or=20as=20image=20to=20show=20in=20the=20article.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Sources/Images/ArticleThumbnailFinder.swift | 12 ++++++++++-- .../RSParser/HTML/SignificantImageFilter.swift | 15 ++++++++++++++- .../RSParserTests/HTML/HTMLImageParserTests.swift | 1 + Shared/Article Rendering/ArticleRenderer.swift | 14 +++++++++----- 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/Modules/Images/Sources/Images/ArticleThumbnailFinder.swift b/Modules/Images/Sources/Images/ArticleThumbnailFinder.swift index f918ed6c7..9314bc9e7 100644 --- a/Modules/Images/Sources/Images/ArticleThumbnailFinder.swift +++ b/Modules/Images/Sources/Images/ArticleThumbnailFinder.swift @@ -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 diff --git a/Modules/RSParser/Sources/RSParser/HTML/SignificantImageFilter.swift b/Modules/RSParser/Sources/RSParser/HTML/SignificantImageFilter.swift index 2b9fc79ba..d2230de93 100644 --- a/Modules/RSParser/Sources/RSParser/HTML/SignificantImageFilter.swift +++ b/Modules/RSParser/Sources/RSParser/HTML/SignificantImageFilter.swift @@ -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) diff --git a/Modules/RSParser/Tests/RSParserTests/HTML/HTMLImageParserTests.swift b/Modules/RSParser/Tests/RSParserTests/HTML/HTMLImageParserTests.swift index 4be7d6d4c..d04c14277 100644 --- a/Modules/RSParser/Tests/RSParserTests/HTML/HTMLImageParserTests.swift +++ b/Modules/RSParser/Tests/RSParserTests/HTML/HTMLImageParserTests.swift @@ -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)) diff --git a/Shared/Article Rendering/ArticleRenderer.swift b/Shared/Article Rendering/ArticleRenderer.swift index 3247b9887..0b96daaa7 100644 --- a/Shared/Article Rendering/ArticleRenderer.swift +++ b/Shared/Article Rendering/ArticleRenderer.swift @@ -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() 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)