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

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.
This commit is contained in:
Krille 2023-11-15 07:48:34 +01:00
parent 966951dfeb
commit 029b648f26
No known key found for this signature in database
2 changed files with 22 additions and 12 deletions

View File

@ -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>');
}
}

View File

@ -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" />');
@ -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>',
);
});
});