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

fix: Do only convert linebreaks to br tags in p blocks #1609

Merged
merged 1 commit into from
Nov 22, 2023
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
22 changes: 13 additions & 9 deletions lib/src/utils/markdown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -250,29 +250,33 @@ String markdown(
}
}
}
if (stripPTags) {
ret = ret.replaceAll('<p>', '').replaceAll('</p>', '');
}
ret = ret
.trim()
// Remove trailing linebreaks
.replaceAll(RegExp(r'(<br />)+$'), '');
if (convertLinebreaks) {
// Only convert linebreaks which are not in <pre> blocks
ret = ret.convertLinebreaksToBr();
ret = ret.convertLinebreaksToBr('p');
// Delete other linebreaks except for pre blocks:
ret = ret.convertLinebreaksToBr('pre', exclude: true, replaceWith: '');
}

if (stripPTags) {
ret = ret.replaceAll('<p>', '').replaceAll('</p>', '');
}

return ret;
}

extension on String {
String convertLinebreaksToBr() {
final parts = split('pre>');
var convertLinebreaks = true;
String convertLinebreaksToBr(String tagName,
{bool exclude = false, String replaceWith = '<br/>'}) {
final parts = split('$tagName>');
var convertLinebreaks = exclude;
for (var i = 0; i < parts.length; i++) {
if (convertLinebreaks) parts[i] = parts[i].replaceAll('\n', '<br/>');
if (convertLinebreaks) parts[i] = parts[i].replaceAll('\n', replaceWith);
convertLinebreaks = !convertLinebreaks;
}
return parts.join('pre>');
return parts.join('$tagName>');
}
}
12 changes: 9 additions & 3 deletions test/markdown_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,20 @@ void main() {
'Snape killed <span data-mx-spoiler="">Dumbledoor <strong>bold</strong></span>');
});
test('multiple paragraphs', () {
expect(markdown('Heya!\n\nBeep'), '<p>Heya!</p><br/><p>Beep</p>');
expect(markdown('Heya!\n\nBeep'), '<p>Heya!</p><p>Beep</p>');
});
test('Other block elements', () {
expect(markdown('# blah\n\nblubb'), '<h1>blah</h1><br/><p>blubb</p>');
expect(markdown('# blah\n\nblubb'), '<h1>blah</h1><p>blubb</p>');
});
test('linebreaks', () {
expect(markdown('foxies\ncute'), 'foxies<br/>cute');
});
test('lists', () {
expect(
markdown('So we have:\n- foxies\n- cats\n- dogs'),
'<p>So we have:</p><ul><li>foxies</li><li>cats</li><li>dogs</li></ul>',
);
});
test('emotes', () {
expect(markdown(':fox:', getEmotePacks: () => emotePacks),
'<img data-mx-emoticon="" src="mxc://roomfox" alt=":fox:" title=":fox:" height="32" vertical-align="middle" />');
Expand Down Expand Up @@ -137,7 +143,7 @@ void main() {
'The first \n codeblock\n```dart\nvoid main(){\nprint(something);\n}\n```\nAnd the second code block\n```js\nmeow\nmeow\n```',
convertLinebreaks: true,
),
'<p>The first<br/>codeblock</p><br/><pre><code class="language-dart">void main(){\nprint(something);\n}\n</code></pre><br/><p>And the second code block</p><br/><pre><code class="language-js">meow\nmeow\n</code></pre>',
'<p>The first<br/>codeblock</p><pre><code class="language-dart">void main(){\nprint(something);\n}\n</code></pre><p>And the second code block</p><pre><code class="language-js">meow\nmeow\n</code></pre>',
);
});
});
Expand Down