Prevent links from showing up in color within code blocks, simplify foreground color stripping

This commit is contained in:
Stefan Ceriu
2024-05-10 10:36:20 +03:00
committed by Stefan Ceriu
parent a401976b2b
commit 099b69d2f2
2 changed files with 14 additions and 23 deletions
@@ -49,9 +49,9 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol {
}
let mutableAttributedString = NSMutableAttributedString(string: string)
removeDefaultForegroundColors(mutableAttributedString)
addLinksAndMentions(mutableAttributedString)
detectPermalinks(mutableAttributedString)
removeLinkColors(mutableAttributedString)
let result = try? AttributedString(mutableAttributedString, including: \.elementX)
Self.cacheValue(result, forKey: string, cacheKey: cacheKey)
@@ -105,12 +105,11 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol {
}
let mutableAttributedString = NSMutableAttributedString(attributedString: attributedString)
removeDefaultForegroundColor(mutableAttributedString)
removeDefaultForegroundColors(mutableAttributedString)
addLinksAndMentions(mutableAttributedString)
replaceMarkedBlockquotes(mutableAttributedString)
replaceMarkedCodeBlocks(mutableAttributedString)
detectPermalinks(mutableAttributedString)
removeLinkColors(mutableAttributedString)
removeDTCoreTextArtifacts(mutableAttributedString)
let result = try? AttributedString(mutableAttributedString, including: \.elementX)
@@ -140,12 +139,8 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol {
return result
}
private func removeDefaultForegroundColor(_ attributedString: NSMutableAttributedString) {
attributedString.enumerateAttribute(.foregroundColor, in: .init(location: 0, length: attributedString.length), options: []) { value, range, _ in
if value as? UIColor == UIColor.black {
attributedString.removeAttribute(.foregroundColor, range: range)
}
}
private func removeDefaultForegroundColors(_ attributedString: NSMutableAttributedString) {
attributedString.removeAttribute(.foregroundColor, range: .init(location: 0, length: attributedString.length))
}
private func addLinksAndMentions(_ attributedString: NSMutableAttributedString) {
@@ -293,14 +288,6 @@ struct AttributedStringBuilder: AttributedStringBuilderProtocol {
}
}
private func removeLinkColors(_ attributedString: NSMutableAttributedString) {
attributedString.enumerateAttribute(.link, in: .init(location: 0, length: attributedString.length), options: []) { value, range, _ in
if value != nil {
attributedString.removeAttribute(.foregroundColor, range: range)
}
}
}
private func removeDTCoreTextArtifacts(_ attributedString: NSMutableAttributedString) {
guard attributedString.length > 0 else {
return