Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TW-2145: Update condition for mapping formatted_body #67

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions doc/adr/0003-improve-get-event-content-from-message-text.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 3. 0003-improve-get-event-content-from-message-text.md

Date: 2024-12-08

## Status

Accepted

## Context

- The current implementation includes parsing the message for markdown, converting line breaks, and
formatting the message if necessary.

## Decision

- We will refactor the getEventContentFromMsgText function to improve its readability and structure.
The changes include:
1. Extracting the markdown conversion logic into a separate function.
2. Simplifying the line break conversion and formatting check.

## Consequences

- The function will be easier to read and maintain.
- The logic for markdown conversion and line break handling will be more modular and reusable
15 changes: 11 additions & 4 deletions lib/src/room.dart
Original file line number Diff line number Diff line change
Expand Up @@ -646,14 +646,13 @@ class Room {
threadRootEventId: threadRootEventId,
threadLastEventId: threadLastEventId);
}

final eventContent = getEventContentFromMsgText(
message: message,
parseMarkdown: parseMarkdown,
msgtype: msgtype,
);
return sendEvent(
eventContent,
return sendEvent(eventContent,
txid: txid,
inReplyTo: inReplyTo,
editEventId: editEventId,
Expand All @@ -674,9 +673,17 @@ class Room {
final html = markdown(event['body'],
getEmotePacks: () => getImagePacksFlat(ImagePackUsage.emoticon),
getMention: getMention);

final formatText = event['body']
.toString()
.trim()
.replaceAll(RegExp(r'(<br />)+$'), '')
.convertLinebreaksToBr()
.replaceAll(RegExp(r'<br />\n?'), '\n');

// if the decoded html is the same as the body, there is no need in sending a formatted message
if (HtmlUnescape().convert(html.replaceAll(RegExp(r'<br />\n?'), '\n')) !=
event['body']) {
formatText) {
event['format'] = 'org.matrix.custom.html';
event['formatted_body'] = html;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/utils/markdown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ String markdown(
return ret;
}

extension on String {
extension StringExtension on String {
String convertLinebreaksToBr() {
final parts = split('pre>');
var convertLinebreaks = true;
Expand Down
Loading