Merge branch 'henri/event-getLocalizedBody-remove-markdown' into 'main'

feat: allow removing markdown formating from localized body

See merge request famedly/company/frontend/famedlysdk!979
This commit is contained in:
Krille Fear 2022-03-24 08:09:43 +00:00
commit b5336cf589
2 changed files with 55 additions and 7 deletions

View File

@ -19,11 +19,13 @@
import 'dart:convert'; import 'dart:convert';
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:html/parser.dart';
import 'package:http/http.dart' as http; import 'package:http/http.dart' as http;
import '../matrix.dart'; import '../matrix.dart';
import 'utils/event_localizations.dart'; import 'utils/event_localizations.dart';
import 'utils/html_to_text.dart'; import 'utils/html_to_text.dart';
import 'utils/markdown.dart';
abstract class RelationshipTypes { abstract class RelationshipTypes {
static const String reply = 'm.in_reply_to'; static const String reply = 'm.in_reply_to';
@ -581,13 +583,14 @@ class Event extends MatrixEvent {
/// room list you may find [withSenderNamePrefix] useful. Set [hideReply] to /// room list you may find [withSenderNamePrefix] useful. Set [hideReply] to
/// crop all lines starting with '>'. With [plaintextBody] it'll use the /// crop all lines starting with '>'. With [plaintextBody] it'll use the
/// plaintextBody instead of the normal body. /// plaintextBody instead of the normal body.
String getLocalizedBody( /// [removeMarkdown] allow to remove the markdown formating from the event body.
MatrixLocalizations i18n, { /// Usefull form message preview or notifications text.
bool withSenderNamePrefix = false, String getLocalizedBody(MatrixLocalizations i18n,
bool hideReply = false, {bool withSenderNamePrefix = false,
bool hideEdit = false, bool hideReply = false,
bool plaintextBody = false, bool hideEdit = false,
}) { bool plaintextBody = false,
bool removeMarkdown = false}) {
if (redacted) { if (redacted) {
return i18n.removedBy(redactedBecause?.sender.calcDisplayname() ?? ''); return i18n.removedBy(redactedBecause?.sender.calcDisplayname() ?? '');
} }
@ -621,6 +624,16 @@ class Event extends MatrixEvent {
body = body.replaceFirst( body = body.replaceFirst(
RegExp(r'^>( \*)? <[^>]+>[^\n\r]+\r?\n(> [^\n]*\r?\n)*\r?\n'), ''); 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]; final callback = EventLocalizations.localizationsMap[type];
var localizedBody = i18n.unknownEvent(type); var localizedBody = i18n.unknownEvent(type);
if (callback != null) { if (callback != null) {

View File

@ -1034,6 +1034,41 @@ void main() {
event.getLocalizedBody(MatrixDefaultLocalizations(), event.getLocalizedBody(MatrixDefaultLocalizations(),
hideReply: true, plaintextBody: true), hideReply: true, plaintextBody: true),
'hmm, *fox*'); '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':
'<h1>Title</h1>\n<p>some text and <a href=\"https://example.com\">link</a><br>okay and this is <strong>important</strong></p>\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');
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', () { test('aggregations', () {