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..12c7b2fa 100644
--- a/test/markdown_test.dart
+++ b/test/markdown_test.dart
@@ -70,15 +70,30 @@ 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
Heya!
Beep
Heya!
Beep
Heya!
Beep
Heya!
Beep
blubb
'); }); - test('linebreaks', () { - expect(markdown('foxies\ncute'), 'foxies