Use link replacement length from instance config if available

This commit is contained in:
Shadowfacts 2021-11-11 13:44:24 -05:00
parent 9f8b14d180
commit f5e9f71586
2 changed files with 3 additions and 3 deletions

View File

@ -13,12 +13,12 @@ public struct CharacterCounter {
static let linkDetector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue) static let linkDetector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
static let mention = try! NSRegularExpression(pattern: "(@[a-z0-9_]+)(?:@[a-z0-9\\-\\.]+[a-z0-9]+)?", options: .caseInsensitive) static let mention = try! NSRegularExpression(pattern: "(@[a-z0-9_]+)(?:@[a-z0-9\\-\\.]+[a-z0-9]+)?", options: .caseInsensitive)
public static func count(text: String) -> Int { public static func count(text: String, for instance: Instance? = nil) -> Int {
let mentionsRemoved = removeMentions(in: text) let mentionsRemoved = removeMentions(in: text)
var count = mentionsRemoved.count var count = mentionsRemoved.count
for match in linkDetector.matches(in: mentionsRemoved, options: [], range: NSRange(location: 0, length: mentionsRemoved.utf16.count)) { for match in linkDetector.matches(in: mentionsRemoved, options: [], range: NSRange(location: 0, length: mentionsRemoved.utf16.count)) {
count -= match.range.length count -= match.range.length
count += 23 // Mastodon link length count += instance?.configuration?.statuses.charactersReservedPerURL ?? 23 // default Mastodon link length
} }
return count return count
} }

View File

@ -30,7 +30,7 @@ struct ComposeView: View {
var charactersRemaining: Int { var charactersRemaining: Int {
let limit = mastodonController.instance?.maxStatusCharacters ?? 500 let limit = mastodonController.instance?.maxStatusCharacters ?? 500
let cwCount = draft.contentWarningEnabled ? draft.contentWarning.count : 0 let cwCount = draft.contentWarningEnabled ? draft.contentWarning.count : 0
return limit - (cwCount + CharacterCounter.count(text: draft.text)) return limit - (cwCount + CharacterCounter.count(text: draft.text, for: mastodonController.instance))
} }
var requiresAttachmentDescriptions: Bool { var requiresAttachmentDescriptions: Bool {