Fix twitter links (#316)

This commit is contained in:
Sean Goldin 2023-01-23 13:51:16 -06:00 committed by GitHub
parent 55c462f7e9
commit 6aea60b644
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -13,7 +13,22 @@ public struct HTMLString: Decodable, Equatable, Hashable {
public init(from decoder: Decoder) {
do {
let container = try decoder.singleValueContainer()
htmlValue = try container.decode(String.self)
var tempHtmlValue = try container.decode(String.self)
let twitterUrl = "@twitter.com"
let twitPattern = #"@+[a-zA-Z0-9(_).-]{1,}@twitter.com"#
let regex = try Regex(twitPattern)
for twitterRange in tempHtmlValue.ranges(of: regex).reversed() {
let unparsedHandle = tempHtmlValue[twitterRange.lowerBound...twitterRange.upperBound]
let twitterHandle = unparsedHandle.dropFirst().dropLast(twitterUrl.count + 1)
let twitterUrlString = "<a href=\"https://twitter.com/\(twitterHandle)\">\(unparsedHandle)</a>"
tempHtmlValue = tempHtmlValue.replacingOccurrences(of: unparsedHandle, with: twitterUrlString, options: .caseInsensitive,
range: twitterRange.lowerBound..<tempHtmlValue.endIndex)
}
htmlValue = tempHtmlValue
} catch {
htmlValue = ""
}