Skip to content

Commit ab9fb42

Browse files
authored
Merge pull request #3340 from SwiftPackageIndex/fix-3339
Replace GitHub protected cached images in README files with their original uncached versions
2 parents 653726d + e84feac commit ab9fb42

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

Sources/App/Views/PackageController/PackageReadme+Model.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ extension PackageReadme {
5656
readmeElement.rewriteRelativeImages(to: repoTriple)
5757
readmeElement.rewriteRelativeLinks(to: repoTriple)
5858
readmeElement.fixInlineAnchors()
59+
readmeElement.fixProtectedCachedImages()
5960
readmeElement.disableTurboOnLinks()
6061
return readmeElement
6162
}
@@ -124,6 +125,21 @@ extension Element {
124125
}
125126
}
126127

128+
func fixProtectedCachedImages() {
129+
do {
130+
let imageElements = try select("img[data-canonical-src]")
131+
for imageElement in imageElements {
132+
let originalUrl = try imageElement.attr("data-canonical-src")
133+
if originalUrl.hasPrefix("http") {
134+
try imageElement.attr("src", originalUrl)
135+
}
136+
}
137+
} catch {
138+
// Errors are being intentionally eaten here. The worst that can happen if the
139+
// HTML selection/parsing fails is that cached images don't get corrected.
140+
}
141+
}
142+
127143
func disableTurboOnLinks() {
128144
do {
129145
let linkElements = try select("a")

Tests/AppTests/PackageReadmeModelTests.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,30 @@ class PackageReadmeModelTests: SnapshotTestCase {
130130
assertSnapshot(of: html, as: .lines)
131131
}
132132

133+
func test_Element_fixProtectedCachedImages() throws {
134+
// setup
135+
let element = Element.extractReadme("""
136+
<div id="readme">
137+
<article>
138+
<p>README content.</p>
139+
<img src="https://example.com/standard.png" />
140+
<img src="https://camo.githubusercontent.com/0123456789" data-canonical-src="https://example.com/cached.png" />
141+
<img src="https://camo.githubusercontent.com/0123456789" data-canonical-src="" />
142+
<img src="https://camo.githubusercontent.com/0123456789" />
143+
<img src="https://example.com/0123456789" data-canonical-src="https://example.com/cached.png" />
144+
<img />
145+
</article>
146+
</div>
147+
""")
148+
149+
// MUT
150+
element?.fixProtectedCachedImages()
151+
152+
// validate
153+
let html = try XCTUnwrap(try element?.html())
154+
assertSnapshot(of: html, as: .lines)
155+
}
156+
133157
func test_Element_disableTurboOnLinks() throws {
134158
// setup
135159
let element = Element.extractReadme("""
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<p>README content.</p>
2+
<img src="https://example.com/standard.png" />
3+
<img src="https://example.com/cached.png" data-canonical-src="https://example.com/cached.png" />
4+
<img src="https://camo.githubusercontent.com/0123456789" data-canonical-src="" />
5+
<img src="https://camo.githubusercontent.com/0123456789" />
6+
<img src="https://example.com/cached.png" data-canonical-src="https://example.com/cached.png" />
7+
<img />

0 commit comments

Comments
 (0)