Fix twitter links (#316)
This commit is contained in:
parent
55c462f7e9
commit
6aea60b644
|
@ -13,7 +13,22 @@ public struct HTMLString: Decodable, Equatable, Hashable {
|
||||||
public init(from decoder: Decoder) {
|
public init(from decoder: Decoder) {
|
||||||
do {
|
do {
|
||||||
let container = try decoder.singleValueContainer()
|
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 {
|
} catch {
|
||||||
htmlValue = ""
|
htmlValue = ""
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue