Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 31 additions & 0 deletions Sources/MessageFormatting/LinkDetector.swift
Original file line number Diff line number Diff line change
@@ -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<String> = ["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
}
}
38 changes: 38 additions & 0 deletions Tests/MessageFormattingTests/LinkDetectorTests.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
Loading