From 77347a44fff8cdeffe06ce49f3f689ec22bca295 Mon Sep 17 00:00:00 2001 From: "h.carnot" Date: Fri, 18 Mar 2022 11:40:10 +0100 Subject: [PATCH 1/3] feat: allow removing markdown formating --- lib/src/event.dart | 25 ++++++++++++++++++------- test/event_test.dart | 25 +++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/lib/src/event.dart b/lib/src/event.dart index b8979b46..52e59f02 100644 --- a/lib/src/event.dart +++ b/lib/src/event.dart @@ -19,11 +19,13 @@ import 'dart:convert'; import 'dart:typed_data'; +import 'package:html/parser.dart'; import 'package:http/http.dart' as http; import '../matrix.dart'; import 'utils/event_localizations.dart'; import 'utils/html_to_text.dart'; +import 'utils/markdown.dart'; abstract class RelationshipTypes { static const String reply = 'm.in_reply_to'; @@ -581,13 +583,12 @@ class Event extends MatrixEvent { /// room list you may find [withSenderNamePrefix] useful. Set [hideReply] to /// crop all lines starting with '>'. With [plaintextBody] it'll use the /// plaintextBody instead of the normal body. - String getLocalizedBody( - MatrixLocalizations i18n, { - bool withSenderNamePrefix = false, - bool hideReply = false, - bool hideEdit = false, - bool plaintextBody = false, - }) { + String getLocalizedBody(MatrixLocalizations i18n, + {bool withSenderNamePrefix = false, + bool hideReply = false, + bool hideEdit = false, + bool plaintextBody = false, + bool removeMarkdown = false}) { if (redacted) { return i18n.removedBy(redactedBecause?.sender.calcDisplayname() ?? ''); } @@ -621,6 +622,16 @@ class Event extends MatrixEvent { body = body.replaceFirst( RegExp(r'^>( \*)? <[^>]+>[^\n\r]+\r?\n(> [^\n]*\r?\n)*\r?\n'), ''); } + + // return the html tags free body + if (removeMarkdown == true) { + final html = markdown(body); + final document = parse( + html, + ); + body = document.documentElement?.text ?? body; + } + final callback = EventLocalizations.localizationsMap[type]; var localizedBody = i18n.unknownEvent(type); if (callback != null) { diff --git a/test/event_test.dart b/test/event_test.dart index 2a7aa804..0117d58c 100644 --- a/test/event_test.dart +++ b/test/event_test.dart @@ -1034,6 +1034,31 @@ void main() { event.getLocalizedBody(MatrixDefaultLocalizations(), hideReply: true, plaintextBody: true), 'hmm, *fox*'); + + event = Event.fromJson({ + 'content': { + 'body': + '# Title\nsome text and [link](https://example.com)\nokay and this is **important**', + 'format': 'org.matrix.custom.html', + 'formatted_body': + '

Title

\n

some text and link
okay and this is important

\n', + 'msgtype': 'm.text' + }, + 'event_id': '\$143273582443PhrSn:example.org', + 'origin_server_ts': 1432735824653, + 'room_id': '!jEsUZKDJdhlrceRyVU:example.org', + 'sender': '@example:example.org', + 'type': 'm.room.message', + 'unsigned': {'age': 1234} + }, room); + expect( + event.getLocalizedBody(MatrixDefaultLocalizations(), + removeMarkdown: true), + 'Title\nsome text and link\nokay and this is important'); + expect( + event.getLocalizedBody(MatrixDefaultLocalizations(), + removeMarkdown: true, plaintextBody: true), + 'Title\nsome text and 🔗link\nokay and this is important'); }); test('aggregations', () { From b47a5f8dff74fe86d50a5a6bab11e1616f53bcf0 Mon Sep 17 00:00:00 2001 From: "h.carnot" Date: Fri, 18 Mar 2022 11:42:38 +0100 Subject: [PATCH 2/3] feat: added doc --- lib/src/event.dart | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/src/event.dart b/lib/src/event.dart index 52e59f02..169a7d36 100644 --- a/lib/src/event.dart +++ b/lib/src/event.dart @@ -583,6 +583,8 @@ class Event extends MatrixEvent { /// room list you may find [withSenderNamePrefix] useful. Set [hideReply] to /// crop all lines starting with '>'. With [plaintextBody] it'll use the /// plaintextBody instead of the normal body. + /// [removeMarkdown] allow to remove the markdown formating from the event body. + /// Usefull form message preview or notifications text. String getLocalizedBody(MatrixLocalizations i18n, {bool withSenderNamePrefix = false, bool hideReply = false, From f095cbe29d09e48630b129c37646a51c9c21016d Mon Sep 17 00:00:00 2001 From: "h.carnot" Date: Fri, 18 Mar 2022 11:45:08 +0100 Subject: [PATCH 3/3] feat: add some more tests --- test/event_test.dart | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/test/event_test.dart b/test/event_test.dart index 0117d58c..5d0120be 100644 --- a/test/event_test.dart +++ b/test/event_test.dart @@ -1058,7 +1058,17 @@ void main() { expect( event.getLocalizedBody(MatrixDefaultLocalizations(), removeMarkdown: true, plaintextBody: true), - 'Title\nsome text and 🔗link\nokay and this is important'); + 'Title\nsome text and 🔗link\nokay and this is important'); + expect( + event.getLocalizedBody(MatrixDefaultLocalizations(), + removeMarkdown: true, withSenderNamePrefix: true), + 'Example: Title\nsome text and link\nokay and this is important'); + expect( + event.getLocalizedBody(MatrixDefaultLocalizations(), + removeMarkdown: true, + plaintextBody: true, + withSenderNamePrefix: true), + 'Example: Title\nsome text and 🔗link\nokay and this is important'); }); test('aggregations', () {