Skip to content

Commit

Permalink
fix: Do only convert linebreaks to br tags in p blocks
Browse files Browse the repository at this point in the history
This changes the behavior
of the markdown method to
only convert linebreaks inside
of p blocks. I found no better
solution yet for the problem
as otherwise also lists
will have linebreaks between
the list items. Unfortunately
the default linebreak syntax
seems not to fulfill our needs.
  • Loading branch information
krille-chan committed Nov 15, 2023
1 parent 7324064 commit f8607c4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
23 changes: 14 additions & 9 deletions lib/src/utils/markdown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -250,29 +250,34 @@ 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

0 comments on commit f8607c4

Please sign in to comment.