From c1694cefbdb0fff1c1cd306264e8e9e7ec12990e Mon Sep 17 00:00:00 2001 From: Berzan Mikaili <17531789+bmikaili@users.noreply.github.com> Date: Wed, 2 Dec 2026 19:34:46 +0100 Subject: [PATCH] Make bare URLs clickable in plain-text messages --- .../MessageTextContentView.swift | 3 +- Sources/MessageFormatting/LinkDetector.swift | 31 +++++++++++++++ .../LinkDetectorTests.swift | 38 +++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 Sources/MessageFormatting/LinkDetector.swift create mode 100644 Tests/MessageFormattingTests/LinkDetectorTests.swift diff --git a/Sources/MactrixApp/Views/ChatView/TimelineView/TimelineRowViews/MessageTextContentView.swift b/Sources/MactrixApp/Views/ChatView/TimelineView/TimelineRowViews/MessageTextContentView.swift index 4c323c94..9d486d0a 100644 --- a/Sources/MactrixApp/Views/ChatView/TimelineView/TimelineRowViews/MessageTextContentView.swift +++ b/Sources/MactrixApp/Views/ChatView/TimelineView/TimelineRowViews/MessageTextContentView.swift @@ -45,7 +45,8 @@ final class MessageTextContentView: NSView, MessageContentRowView { } private func setText(_ text: NSAttributedString, matrixClient: MatrixClient?) { - let text = MentionText.replacingUserLinks(in: text, imageLoader: matrixClient) + let linked = LinkDetector.linkifyingBareURLs(in: text) + let text = MentionText.replacingUserLinks(in: linked, imageLoader: matrixClient) bodyText.textStorage?.setAttributedString(text) if let matrixClient { MentionAvatarLoader.shared(for: matrixClient).restoreAvatars(in: text) diff --git a/Sources/MessageFormatting/LinkDetector.swift b/Sources/MessageFormatting/LinkDetector.swift new file mode 100644 index 00000000..abd044b2 --- /dev/null +++ b/Sources/MessageFormatting/LinkDetector.swift @@ -0,0 +1,31 @@ +import Foundation + +/// Attaches `.link` attributes to bare `http`, `https`, and `mailto` URLs without overriding existing links. +@MainActor +public enum LinkDetector { + private static let detector = try? NSDataDetector( + types: NSTextCheckingResult.CheckingType.link.rawValue) + private static let allowedSchemes: Set = ["http", "https", "mailto"] + + public static func linkifyingBareURLs(in text: NSAttributedString) -> NSAttributedString { + guard let detector, text.length > 0 else { return text } + let result = NSMutableAttributedString(attributedString: text) + let whole = NSRange(location: 0, length: result.length) + for match in detector.matches(in: result.string, range: whole) { + guard let url = match.url, allowedSchemes.contains(url.scheme?.lowercased() ?? "") else { + continue + } + var hasExistingLink = false + result.enumerateAttribute(.link, in: match.range) { value, _, stop in + if value != nil { + hasExistingLink = true + stop.pointee = true + } + } + if !hasExistingLink { + result.addAttribute(.link, value: url, range: match.range) + } + } + return result + } +} diff --git a/Tests/MessageFormattingTests/LinkDetectorTests.swift b/Tests/MessageFormattingTests/LinkDetectorTests.swift new file mode 100644 index 00000000..f2f98da4 --- /dev/null +++ b/Tests/MessageFormattingTests/LinkDetectorTests.swift @@ -0,0 +1,38 @@ +import Foundation +import MessageFormatting +import Testing + +@MainActor +struct LinkDetectorTests { + private func linkRanges(in text: NSAttributedString) -> [String] { + var matches: [String] = [] + text.enumerateAttribute(.link, in: NSRange(location: 0, length: text.length)) { + value, range, _ in + if value != nil { + matches.append((text.string as NSString).substring(with: range)) + } + } + return matches + } + + @Test func detectsBareHTTPAndWWWLinks() { + let input = NSAttributedString(string: "See https://example.org/docs and www.example.com ok") + let out = LinkDetector.linkifyingBareURLs(in: input) + #expect(linkRanges(in: out) == ["https://example.org/docs", "www.example.com"]) + } + + @Test func leavesPlainTextUnchanged() { + let out = LinkDetector.linkifyingBareURLs(in: NSAttributedString(string: "no links here")) + #expect(linkRanges(in: out).isEmpty) + } + + @Test func preservesExistingLinkTarget() { + let input = NSMutableAttributedString(string: "see https://a.example") + let range = NSRange(location: 4, length: 17) + let original = URL(string: "https://b.example")! + input.addAttribute(.link, value: original, range: range) + + let out = LinkDetector.linkifyingBareURLs(in: input) + #expect(out.attribute(.link, at: 5, effectiveRange: nil) as? URL == original) + } +}