Add Hashtag-Regex (IOS-141)

This commit is contained in:
Nathan Mattes 2023-09-20 16:29:30 +02:00
parent d9243c25ce
commit e041a7e086
2 changed files with 12 additions and 1 deletions

View File

@ -103,10 +103,12 @@ class SearchResultsOverviewTableViewController: UIViewController, NeedsDependenc
}
}
//TODO: Check for Hashtag-Regex!
if searchText.starts(with: "#") && searchText.length > 1 {
snapshot.appendItems([.default(.showHashtag(hashtag: searchText.replacingOccurrences(of: "#", with: "")))],
toSection: .default)
} else if searchText.length > 1, let hashtagRegex = try? NSRegularExpression(pattern: MastodonRegex.Search.hashtag, options: .caseInsensitive), hashtagRegex.firstMatch(in: searchText, range: NSRange(location: 0, length: searchText.length-1)) != nil {
snapshot.appendItems([.default(.showHashtag(hashtag: searchText.replacingOccurrences(of: "#", with: "")))],
toSection: .default)
}
if searchText.length > 1,

View File

@ -25,6 +25,15 @@ public enum MastodonRegex {
public enum Search {
public static let username = "^@?[a-z0-9_-]+(@[\\S]+)?$"
/// See: https://github.com/mastodon/mastodon/blob/main/app/javascript/mastodon/utils/hashtags.ts
public static var hashtag: String {
let word = "\\p{L}\\p{M}\\p{N}\\p{Pc}"
let alpha = "\\p{L}\\p{M}"
let hashtag_separators = "_\\u00b7\\u200c"
return "^(([\(word)_][\(word)\(hashtag_separators)]*[\(alpha)\(hashtag_separators)][\(word)\(hashtag_separators)]*[\(word)_])|([\(word)_]*[\(alpha)][\(word)_]*))$"
}
}
}