From 1f54338ab95b3fd39d56450a082c7c049fb9c7a6 Mon Sep 17 00:00:00 2001 From: Garth Vander Houwen Date: Fri, 23 Dec 2022 23:48:35 -0800 Subject: [PATCH] Generate message markdown for incoming and outgoing messages --- Meshtastic/Helpers/BLEManager.swift | 1 + Meshtastic/Helpers/MeshPackets.swift | 54 +++++++++++++++------------- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/Meshtastic/Helpers/BLEManager.swift b/Meshtastic/Helpers/BLEManager.swift index acd11c67..85eb1717 100644 --- a/Meshtastic/Helpers/BLEManager.swift +++ b/Meshtastic/Helpers/BLEManager.swift @@ -620,6 +620,7 @@ class BLEManager: NSObject, CBPeripheralDelegate, ObservableObject { newMessage.replyID = replyID } newMessage.messagePayload = message + newMessage.messagePayloadMarkdown = generateMessageMarkdown(message: message) let dataType = PortNum.textMessageApp let payloadData: Data = message.data(using: String.Encoding.utf8)! diff --git a/Meshtastic/Helpers/MeshPackets.swift b/Meshtastic/Helpers/MeshPackets.swift index 784fb2f3..ed76338a 100644 --- a/Meshtastic/Helpers/MeshPackets.swift +++ b/Meshtastic/Helpers/MeshPackets.swift @@ -9,6 +9,32 @@ import Foundation import CoreData import SwiftUI +func generateMessageMarkdown (message: String) -> String { + + let types: NSTextCheckingResult.CheckingType = [.address, .link, .phoneNumber] + let detector = try! NSDataDetector(types: types.rawValue) + let matches = detector.matches(in: message, options: [], range: NSRange(location: 0, length: message.utf16.count)) + var messageWithMarkdown = message + if matches.count > 0 { + + for match in matches { + guard let range = Range(match.range, in: message) else { continue } + if match.resultType == .address { + let address = message[range] + let urlEncodedAddress = address.addingPercentEncoding(withAllowedCharacters: .alphanumerics) + messageWithMarkdown = messageWithMarkdown.replacingOccurrences(of: address, with: "[\(address)](http://maps.apple.com/?address=\(urlEncodedAddress ?? ""))") + } else if match.resultType == .phoneNumber { + let phone = messageWithMarkdown[range] + messageWithMarkdown = messageWithMarkdown.replacingOccurrences(of: phone, with: "[\(phone)](tel:\(phone))") + } else if match.resultType == .link { + let url = messageWithMarkdown[range] + messageWithMarkdown = messageWithMarkdown.replacingOccurrences(of: url, with: "[\(String(match.url?.host ?? "Link"))\(String(match.url?.path ?? ""))](\(url))") + } + } + } + return messageWithMarkdown +} + func localConfig (config: Config, context:NSManagedObjectContext, nodeNum: Int64, nodeLongName: String) { // We don't care about any of the Power settings, config is available for everyting else @@ -1301,31 +1327,9 @@ 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 }) } - let types: NSTextCheckingResult.CheckingType = [.address, .link, .phoneNumber] - let detector = try! NSDataDetector(types: types.rawValue) - let matches = detector.matches(in: messageText, options: [], range: NSRange(location: 0, length: messageText.utf16.count)) - if matches.count > 0 { - var messageWithLink = messageText - for match in matches { - guard let range = Range(match.range, in: messageText) else { continue } - if match.resultType == .address { - let address = messageText[range] - let urlEncodedAddress = address.addingPercentEncoding(withAllowedCharacters: .alphanumerics) - messageWithLink = messageWithLink.replacingOccurrences(of: address, with: "[\(address)](http://maps.apple.com/?address=\(urlEncodedAddress ?? ""))") - } else if match.resultType == .phoneNumber { - let phone = messageText[range] - messageWithLink = messageWithLink.replacingOccurrences(of: phone, with: "[\(phone)](tel:\(phone))") - } else if match.resultType == .link { - let url = messageText[range] - messageWithLink = messageWithLink.replacingOccurrences(of: url, with: "[\(String(match.url?.host ?? "Link"))](\(url))") - } - } - newMessage.messagePayload = messageText - newMessage.messagePayloadMarkdown = messageWithLink - } else { - newMessage.messagePayload = messageText - newMessage.messagePayloadMarkdown = messageText - } + newMessage.messagePayload = messageText + newMessage.messagePayloadMarkdown = generateMessageMarkdown(message: messageText) + newMessage.fromUser?.objectWillChange.send() newMessage.toUser?.objectWillChange.send()