mirror of
https://github.com/meshtastic/Meshtastic-Apple.git
synced 2026-04-20 22:13:56 +00:00
parse links in messages so they can be clicked by recipients
This commit is contained in:
parent
82c1d20a21
commit
41f4eb510d
3 changed files with 20 additions and 4 deletions
|
|
@ -1301,8 +1301,20 @@ func textMessageAppPacket(packet: MeshPacket, connectedNode: Int64, context: NSM
|
|||
if fetchedUsers.first(where: { $0.num == packet.from }) != nil {
|
||||
newMessage.fromUser = fetchedUsers.first(where: { $0.num == packet.from })
|
||||
}
|
||||
|
||||
newMessage.messagePayload = messageText
|
||||
let detector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
|
||||
let matches = detector.matches(in: messageText, options: [], range: NSRange(location: 0, length: messageText.utf16.count))
|
||||
if matches.count > 0 {
|
||||
var messageWithLink = ""
|
||||
for match in matches {
|
||||
guard let range = Range(match.range, in: messageText) else { continue }
|
||||
let url = messageText[range]
|
||||
print(match.url?.baseURL)
|
||||
messageWithLink = messageText.replacingOccurrences(of: url, with: "[\(String(match.url?.host ?? "Link"))](\(url))")
|
||||
}
|
||||
newMessage.messagePayload = messageWithLink
|
||||
} else {
|
||||
newMessage.messagePayload = messageText
|
||||
}
|
||||
newMessage.fromUser?.objectWillChange.send()
|
||||
newMessage.toUser?.objectWillChange.send()
|
||||
|
||||
|
|
|
|||
|
|
@ -60,7 +60,9 @@ struct ChannelMessageList: View {
|
|||
.offset(y: -5)
|
||||
}
|
||||
VStack(alignment: currentUser ? .trailing : .leading) {
|
||||
Text(message.messagePayload ?? "EMPTY MESSAGE")
|
||||
let markdownText: LocalizedStringKey = LocalizedStringKey.init(message.messagePayload ?? "EMPTY MESSAGE")
|
||||
|
||||
Text(markdownText)
|
||||
.padding(10)
|
||||
.foregroundColor(.white)
|
||||
.background(currentUser ? .accentColor : Color(.gray))
|
||||
|
|
|
|||
|
|
@ -61,7 +61,9 @@ struct UserMessageList: View {
|
|||
.offset(y: -5)
|
||||
}
|
||||
VStack(alignment: currentUser ? .trailing : .leading) {
|
||||
Text(message.messagePayload ?? "EMPTY MESSAGE")
|
||||
let markdownText: LocalizedStringKey = LocalizedStringKey.init(message.messagePayload ?? "EMPTY MESSAGE")
|
||||
|
||||
Text(markdownText)
|
||||
.padding(10)
|
||||
.foregroundColor(.white)
|
||||
.background(currentUser ? .accentColor : Color(.gray))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue