diff --git a/lib/src/utils/markdown.dart b/lib/src/utils/markdown.dart index 4b1cec4b..7f1f599f 100644 --- a/lib/src/utils/markdown.dart +++ b/lib/src/utils/markdown.dart @@ -212,7 +212,7 @@ String markdown( bool convertLinebreaks = true, }) { var ret = markdownToHtml( - text, + text.replaceNewlines(), extensionSet: ExtensionSet.commonMark, blockSyntaxes: [ BlockLatexSyntax(), @@ -270,6 +270,18 @@ String markdown( } extension on String { + String replaceNewlines() { + // RegEx for at least 3 following \n + final regExp = RegExp(r'(\n{3,})'); + + return replaceAllMapped(regExp, (match) { + final newLineGroup = match.group(0)!; + return newLineGroup + .replaceAll('\n', '
') + .replaceFirst('

', '\n\n'); + }); + } + String convertLinebreaksToBr( String tagName, { bool exclude = false, diff --git a/test/markdown_test.dart b/test/markdown_test.dart index d685f62d..8b56f012 100644 --- a/test/markdown_test.dart +++ b/test/markdown_test.dart @@ -70,14 +70,29 @@ void main() { 'Snape killed Dumbledoor bold', ); }); - test('multiple paragraphs', () { + test('linebreaks', () { + expect(markdown('Heya!\nBeep'), 'Heya!
Beep'); expect(markdown('Heya!\n\nBeep'), '

Heya!

Beep

'); + expect(markdown('Heya!\n\n\nBeep'), '

Heya!


Beep

'); + expect( + markdown('Heya!\n\n\n\nBeep'), + '

Heya!



Beep

', + ); + expect( + markdown('Heya!\n\n\n\nBeep\n\n'), + '

Heya!



Beep

', + ); + expect( + markdown('\n\nHeya!\n\n\n\nBeep'), + '

Heya!



Beep

', + ); + expect( + markdown('\n\nHeya!\n\n\n\nBeep\n '), + '

Heya!



Beep

', + ); }); test('Other block elements', () { - expect(markdown('# blah\n\nblubb'), '

blah

blubb

'); - }); - test('linebreaks', () { - expect(markdown('foxies\ncute'), 'foxies
cute'); + expect(markdown('# blah\n\nblubb'), '

blah


blubb

'); }); test('lists', () { expect(