Skip to content

Commit c6e8b16

Browse files
authored
Merge pull request #1609 from famedly/krille/linebreaks-markdown
fix: Do only convert linebreaks to br tags in p blocks
2 parents 966951d + 029b648 commit c6e8b16

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

lib/src/utils/markdown.dart

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -250,29 +250,33 @@ String markdown(
250250
}
251251
}
252252
}
253-
if (stripPTags) {
254-
ret = ret.replaceAll('<p>', '').replaceAll('</p>', '');
255-
}
256253
ret = ret
257254
.trim()
258255
// Remove trailing linebreaks
259256
.replaceAll(RegExp(r'(<br />)+$'), '');
260257
if (convertLinebreaks) {
261258
// Only convert linebreaks which are not in <pre> blocks
262-
ret = ret.convertLinebreaksToBr();
259+
ret = ret.convertLinebreaksToBr('p');
260+
// Delete other linebreaks except for pre blocks:
261+
ret = ret.convertLinebreaksToBr('pre', exclude: true, replaceWith: '');
262+
}
263+
264+
if (stripPTags) {
265+
ret = ret.replaceAll('<p>', '').replaceAll('</p>', '');
263266
}
264267

265268
return ret;
266269
}
267270

268271
extension on String {
269-
String convertLinebreaksToBr() {
270-
final parts = split('pre>');
271-
var convertLinebreaks = true;
272+
String convertLinebreaksToBr(String tagName,
273+
{bool exclude = false, String replaceWith = '<br/>'}) {
274+
final parts = split('$tagName>');
275+
var convertLinebreaks = exclude;
272276
for (var i = 0; i < parts.length; i++) {
273-
if (convertLinebreaks) parts[i] = parts[i].replaceAll('\n', '<br/>');
277+
if (convertLinebreaks) parts[i] = parts[i].replaceAll('\n', replaceWith);
274278
convertLinebreaks = !convertLinebreaks;
275279
}
276-
return parts.join('pre>');
280+
return parts.join('$tagName>');
277281
}
278282
}

test/markdown_test.dart

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,20 @@ void main() {
5959
'Snape killed <span data-mx-spoiler="">Dumbledoor <strong>bold</strong></span>');
6060
});
6161
test('multiple paragraphs', () {
62-
expect(markdown('Heya!\n\nBeep'), '<p>Heya!</p><br/><p>Beep</p>');
62+
expect(markdown('Heya!\n\nBeep'), '<p>Heya!</p><p>Beep</p>');
6363
});
6464
test('Other block elements', () {
65-
expect(markdown('# blah\n\nblubb'), '<h1>blah</h1><br/><p>blubb</p>');
65+
expect(markdown('# blah\n\nblubb'), '<h1>blah</h1><p>blubb</p>');
6666
});
6767
test('linebreaks', () {
6868
expect(markdown('foxies\ncute'), 'foxies<br/>cute');
6969
});
70+
test('lists', () {
71+
expect(
72+
markdown('So we have:\n- foxies\n- cats\n- dogs'),
73+
'<p>So we have:</p><ul><li>foxies</li><li>cats</li><li>dogs</li></ul>',
74+
);
75+
});
7076
test('emotes', () {
7177
expect(markdown(':fox:', getEmotePacks: () => emotePacks),
7278
'<img data-mx-emoticon="" src="mxc://roomfox" alt=":fox:" title=":fox:" height="32" vertical-align="middle" />');
@@ -137,7 +143,7 @@ void main() {
137143
'The first \n codeblock\n```dart\nvoid main(){\nprint(something);\n}\n```\nAnd the second code block\n```js\nmeow\nmeow\n```',
138144
convertLinebreaks: true,
139145
),
140-
'<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>',
146+
'<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>',
141147
);
142148
});
143149
});

0 commit comments

Comments
 (0)