Fix handling of Twitter link

This commit is contained in:
Thomas Ricouard 2023-01-23 21:34:45 +01:00
parent 6aea60b644
commit 11a7ade82d
2 changed files with 11 additions and 16 deletions

View File

@ -23,6 +23,16 @@ private struct SafariRouter: ViewModifier {
})
.onAppear {
routerPath.urlHandler = { url in
if url.absoluteString.contains("@twitter.com"), url.absoluteString.hasPrefix("mailto:") {
let username = url.absoluteString
.replacingOccurrences(of: "@twitter.com", with: "")
.replacingOccurrences(of: "mailto:", with: "")
let twitterLink = "https://twitter.com/\(username)"
if let url = URL(string: twitterLink) {
UIApplication.shared.open(url)
return .handled
}
}
guard preferences.preferredBrowser == .inAppSafari, !ProcessInfo.processInfo.isiOSAppOnMac else { return .systemAction }
// SFSafariViewController only supports initial URLs with http:// or https:// schemes.
guard let scheme = url.scheme, ["https", "http"].contains(scheme.lowercased()) else {

View File

@ -13,22 +13,7 @@ public struct HTMLString: Decodable, Equatable, Hashable {
public init(from decoder: Decoder) {
do {
let container = try decoder.singleValueContainer()
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
htmlValue = try container.decode(String.self)
} catch {
htmlValue = ""
}