Skip to content

[flutter_markdown] New lines (\n) do not behave as expected. #160292

@bryantwilliam

Description

@bryantwilliam

The problems

  • \n by itself doesn't work inside the markdown widget unless there are 2 spaces before it like \n.
  • \n\n\n and \n\n\n\n (3, 4, or more) show the same as \n\n

Steps to reproduce

MarkdownBody(
  data: """
Testing123 test123\nTesting123 test123  \nTesting123 test123\n\nTesting123 test123  \n\nTesting123 test123\n\n\nTesting123  \n\n\nTesting123 test123
```
code snippet here
asdasdad
```
asdasd
# Hi
asdasd\n\n\n\ntest
""",
);
Note (click to expand)

Sorry, in the example images below, the colours might be different to yours because I also set the Markdown widget's style parameter to:

final style = MarkdownStyleSheet.fromTheme(Theme.of(context)).copyWith(
  p: Theme.of(context).textTheme.bodyMedium?.apply(
        color: Theme.of(context).colorScheme.primary,
      ),
);

Also, I haven't tested on mobile, this is Flutter web.

Expected results

image

Actual results

image

My temporary fix

MarkdownBody(
  data: """
Testing123 test123\nTesting123 test123  \nTesting123 test123\n\nTesting123 test123  \n\nTesting123 test123\n\n\nTesting123  \n\n\nTesting123 test123
```
code snippet here
asdasdad
```
asdasd
# Hi
asdasd\n\n\n\ntest
""",
);

String _fixMarkdownNewLines(String data) {
  final codeBlockPattern =
      RegExp(r'```.*?```', dotAll: true); // Matches code blocks
  final nonCodeBlocks = <String>[];
  final codeBlocks = <String>[];

  // Split data into segments of code blocks and non-code blocks
  final matches = codeBlockPattern.allMatches(data);
  int lastMatchEnd = 0;

  for (final match in matches) {
    // Add text before the code block as a non-code block
    if (match.start > lastMatchEnd) {
      nonCodeBlocks.add(data.substring(lastMatchEnd, match.start));
    }
    // Add the code block itself
    codeBlocks.add(data.substring(match.start, match.end));
    lastMatchEnd = match.end;
  }

  // Add any remaining non-code block text
  if (lastMatchEnd < data.length) {
    nonCodeBlocks.add(data.substring(lastMatchEnd));
  }

  // Process non-code blocks (using \u200B as a temporary hack to show new lines):
  final processedNonCodeBlocks = nonCodeBlocks.map((block) {
    return block
        .replaceAll(
            '\n\n\n\n', '\n\n\n\u200B\n') // First replace quadruple newlines
        .replaceAll('\n\n\n', '\n\n\u200B\n') // Then replace triple newlines
        .replaceAll('\n', '  \n'); // Then replace single newlines
  }).toList();

  // Combine processed non-code blocks and code blocks back together
  final buffer = StringBuffer();
  int codeIndex = 0, nonCodeIndex = 0;

  for (final _ in matches) {
    if (nonCodeIndex < processedNonCodeBlocks.length) {
      buffer.write(processedNonCodeBlocks[nonCodeIndex++]);
    }
    if (codeIndex < codeBlocks.length) {
      buffer.write(codeBlocks[codeIndex++]);
    }
  }

  // Add any remaining non-code block
  if (nonCodeIndex < processedNonCodeBlocks.length) {
    buffer.write(processedNonCodeBlocks[nonCodeIndex]);
  }

  return buffer.toString();
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listfound in release: 3.27Found to occur in 3.27found in release: 3.28Found to occur in 3.28has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: flutter_markdownflutter/packages flutter_markdownpackageflutter/packages repository. See also p: labels.r: invalidIssue is closed as not validteam-ecosystemOwned by Ecosystem teamtriaged-ecosystemTriaged by Ecosystem team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy