Rename URL.reparingIfRequired to URL.encodeSpacesIfNeeded, which is more explicit about what it does. Slightly edit the code and comment.

This commit is contained in:
Brent Simmons committed 2025-09-29 14:08:10 -07:00
1 parent ba6c6cd17f
commit ea5da07a2d
2 files changed
+12 -14

No files matched your search

+3 -3
View File
@@ -47,15 +47,15 @@ extension Article {
}
var url: URL? {
return URL.reparingIfRequired(rawLink)
return URL.encodeSpacesIfNeeded(rawLink)
}
var externalURL: URL? {
return URL.reparingIfRequired(rawExternalLink)
return URL.encodeSpacesIfNeeded(rawExternalLink)
}
var imageURL: URL? {
return URL.reparingIfRequired(rawImageLink)
return URL.encodeSpacesIfNeeded(rawImageLink)
}
var link: String? {
+9 -11
View File
@@ -35,16 +35,14 @@ extension URL {
return value
}
static func reparingIfRequired(_ link: String?) -> URL? {
// If required, we replace any space characters to handle malformed links that are otherwise percent
// encoded but contain spaces. For performance reasons, only try this if initial URL init fails.
guard let link = link, !link.isEmpty else { return nil }
if let url = URL(string: link) {
return url
} else {
return URL(string: link.replacingOccurrences(of: " ", with: "%20"))
}
}
/// Percent-encode spaces in links that may contain spaces but are otherwise already percent-encoded.
///
/// For performance reasons, try this only if initial URL init fails.
static func encodeSpacesIfNeeded(_ link: String?) -> URL? {
guard let link, !link.isEmpty else {
return nil
}
return URL(string: link.replacingOccurrences(of: " ", with: "%20"))
}
}