Skip to content

Commit

Permalink
fix: remove url markdown on messages
Browse files Browse the repository at this point in the history
This Closes #221
  • Loading branch information
Julian KOUNE committed Jul 13, 2023
1 parent 527ab0c commit 790884f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/pages/chat/events/message_content.dart
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ class MessageContent extends StatelessWidget {
if (AppConfig.renderHtml &&
!event.redacted &&
event.isRichMessage) {
var html = event.formattedText;
var html = event.formattedText.unMarkdownLinks(event.text);

if (event.messageType == MessageTypes.Emote) {
html = '* $html';
}
Expand Down
30 changes: 30 additions & 0 deletions lib/utils/string_extension.dart
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,34 @@ extension StringCasingExtension on String {
final String? firstValidLink = matches.firstWhere((link) => AnyLinkPreview.isValidLink(link!));
return firstValidLink;
}

// Removes markdowned links from a string based on the unformatted text
// Workaround for content['formatted_body'] which formats urls in a way that makes them unusable
String unMarkdownLinks(String unformattedText) {
final RegExp regex = RegExp(r'https:\/\/[^\s]+');

final Iterable<Match> formattedLinksMatches = regex.allMatches(this);
final Iterable<Match> unformattedLinksMatches = regex.allMatches(unformattedText);

if (formattedLinksMatches.isEmpty ||
unformattedLinksMatches.isEmpty ||
formattedLinksMatches.length != unformattedLinksMatches.length) {
return this;
}

var unMarkdownedText = this;

// Replace respectively all formatted links with unformatted links
for (int i = 0; i < formattedLinksMatches.length; i++) {
final Match formattedLinkMatch = formattedLinksMatches.elementAt(i);
final Match unformattedLinkMatch = unformattedLinksMatches.elementAt(i);

final String formattedLink = formattedLinkMatch.group(0)!;
final String unformattedLink = unformattedLinkMatch.group(0)!;

unMarkdownedText = unMarkdownedText.replaceFirst(formattedLink, unformattedLink);
}

return unMarkdownedText;
}
}

0 comments on commit 790884f

Please sign in to comment.