From 6aea60b6445bc2dd7fc4652a3efa901e889c70a5 Mon Sep 17 00:00:00 2001 From: Sean Goldin Date: Mon, 23 Jan 2023 13:51:16 -0600 Subject: [PATCH] Fix twitter links (#316) --- .../Sources/Models/Alias/HTMLString.swift | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Packages/Models/Sources/Models/Alias/HTMLString.swift b/Packages/Models/Sources/Models/Alias/HTMLString.swift index 54dc33ba..b4c09289 100644 --- a/Packages/Models/Sources/Models/Alias/HTMLString.swift +++ b/Packages/Models/Sources/Models/Alias/HTMLString.swift @@ -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 = "\(unparsedHandle)" + + tempHtmlValue = tempHtmlValue.replacingOccurrences(of: unparsedHandle, with: twitterUrlString, options: .caseInsensitive, + range: twitterRange.lowerBound..