diff --git a/lib/pages/chat/events/message_content.dart b/lib/pages/chat/events/message_content.dart index 3375da7097..c876e0d16d 100644 --- a/lib/pages/chat/events/message_content.dart +++ b/lib/pages/chat/events/message_content.dart @@ -90,17 +90,33 @@ class MessageContent extends StatelessWidget { ); } + bool _isSearchResult() { + return searchTerm != null && searchTerm!.isNotEmpty; + } + String _getEventTextFormatted() { var text = event.formattedText; - if (searchTerm != null && searchTerm!.isNotEmpty) { - text = text.replaceAll(searchTerm!, - "$searchTerm"); - return text; + if (_isSearchResult()) { + // search result messages may not be formatted-messages originally + // -> take normal text in this case + if (text.isEmpty) { + text = event.text; + } + + return _replaceSearchResults(text); } else { return text; } } + String _replaceSearchResults(String text) { + final searchExp = RegExp(searchTerm!, caseSensitive: false); + text = text.replaceAllMapped( + searchExp, (match) => "${match[0]}"); + + return text; + } + @override Widget build(BuildContext context) { final fontSize = AppConfig.messageFontSize * AppConfig.fontSizeFactor; @@ -144,8 +160,8 @@ class MessageContent extends StatelessWidget { case MessageTypes.Emote: if (AppConfig.renderHtml && !event.redacted && - event.isRichMessage) { - var html = _getEventTextFormatted(); + (event.isRichMessage || _isSearchResult())) { + String html = _getEventTextFormatted(); if (event.messageType == MessageTypes.Emote) { html = '* $html'; } diff --git a/lib/pages/chat_search/chat_search_view.dart b/lib/pages/chat_search/chat_search_view.dart index b870bec467..4672a4cd55 100644 --- a/lib/pages/chat_search/chat_search_view.dart +++ b/lib/pages/chat_search/chat_search_view.dart @@ -145,13 +145,16 @@ class ChatSearchView extends StatelessWidget { crossAxisAlignment: CrossAxisAlignment.stretch, children: [ - Message(snapshot.data![i - 1], - onSwipe: (direction) => {}, - onSelect: - controller.onSelectMessage, - scrollToEventId: - controller.scrollToEventId, - timeline: controller.timeline!), + Message( + snapshot.data![i - 1], + onSwipe: (direction) => {}, + onSelect: + controller.onSelectMessage, + scrollToEventId: + controller.scrollToEventId, + timeline: controller.timeline!, + searchTerm: controller.searchTerm, + ), if (i == snapshot.data?.length && controller.searchState == SearchState.searching)