@@ -224,13 +224,12 @@ public struct EmojiTextView<Factory: ViewFactory>: View {
224224
225225struct StreamTextView : View {
226226
227- @Injected ( \. utils) var utils
228227 @Injected ( \. fonts) var fonts
229228
230229 var message : ChatMessage
231230
232231 var body : some View {
233- if #available( iOS 15 , * ) , utils . messageListConfig . localLinkDetectionEnabled {
232+ if #available( iOS 15 , * ) {
234233 LinkDetectionTextView ( message: message)
235234 } else {
236235 Text ( message. adjustedText)
@@ -245,40 +244,85 @@ struct LinkDetectionTextView: View {
245244
246245 @Injected ( \. colors) var colors
247246 @Injected ( \. fonts) var fonts
247+ @Injected ( \. utils) var utils
248248
249249 var message : ChatMessage
250250
251- var text : String {
252- message. adjustedText
251+ var text : LocalizedStringKey {
252+ LocalizedStringKey ( message. adjustedText)
253253 }
254254
255- @State var displayedText : AttributedString
255+ @State var displayedText : AttributedString ?
256256
257257 @State var linkDetector = TextLinkDetector ( )
258258
259+ @State var tintColor = InjectedValues [ \. colors] . tintColor
260+
259261 init ( message: ChatMessage ) {
260262 self . message = message
261- _displayedText = State ( initialValue: AttributedString ( message. adjustedText) )
263+ }
264+
265+ private var markdownEnabled : Bool {
266+ utils. messageListConfig. markdownSupportEnabled
262267 }
263268
264269 var body : some View {
265- Text ( displayedText)
266- . foregroundColor ( textColor ( for: message) )
267- . font ( fonts. body)
268- . onAppear {
269- let attributedText = NSMutableAttributedString (
270- string: text,
271- attributes: [
272- . foregroundColor: textColor ( for: message) ,
273- . font: fonts. body
274- ]
275- )
270+ Group {
271+ if let displayedText {
272+ Text ( displayedText)
273+ } else if markdownEnabled {
274+ Text ( text)
275+ } else {
276+ Text ( message. adjustedText)
277+ }
278+ }
279+ . foregroundColor ( textColor ( for: message) )
280+ . font ( fonts. body)
281+ . tint ( tintColor)
282+ . onAppear {
283+ guard utils. messageListConfig. localLinkDetectionEnabled else { return }
284+ var attributes : [ NSAttributedString . Key : Any ] = [
285+ . foregroundColor: textColor ( for: message) ,
286+ . font: fonts. body
287+ ]
288+
289+ let additional = utils. messageListConfig. messageDisplayOptions. messageLinkDisplayResolver ( message)
290+ for (key, value) in additional {
291+ if key == . foregroundColor, let value = value as? UIColor {
292+ tintColor = Color ( value)
293+ } else {
294+ attributes [ key] = value
295+ }
296+ }
297+
298+ let attributedText = NSMutableAttributedString (
299+ string: message. adjustedText,
300+ attributes: attributes
301+ )
276302
277- linkDetector. links ( in: text) . forEach { textLink in
278- attributedText. addAttribute ( . link, value: textLink. url, range: textLink. range)
303+ var containsLinks = false
304+ let range = NSRange ( location: 0 , length: message. adjustedText. utf16. count)
305+ linkDetector. links ( in: message. adjustedText) . forEach { textLink in
306+ let pattern = " \\ [([^ \\ ]]+) \\ ] \\ ( \( textLink. originalText) \\ ) "
307+ if let regex = try ? NSRegularExpression ( pattern: pattern) {
308+ containsLinks = ( regex. firstMatch (
309+ in: message. adjustedText,
310+ options: [ ] ,
311+ range: range
312+ ) == nil ) || !markdownEnabled
313+ } else {
314+ containsLinks = true
279315 }
280316
317+ if !message. adjustedText. contains ( " ]( \( textLink. originalText) ) " ) {
318+ containsLinks = true
319+ }
320+ attributedText. addAttribute ( . link, value: textLink. url, range: textLink. range)
321+ }
322+
323+ if containsLinks {
281324 self . displayedText = AttributedString ( attributedText)
282325 }
326+ }
283327 }
284328}
0 commit comments