-
Notifications
You must be signed in to change notification settings - Fork 28.9k
Closed as not planned
Labels
P2Important issues not at the top of the work listImportant issues not at the top of the work listfound in release: 3.27Found to occur in 3.27Found to occur in 3.27found in release: 3.28Found to occur in 3.28Found to occur in 3.28has reproducible stepsThe issue has been confirmed reproducible and is ready to work onThe issue has been confirmed reproducible and is ready to work onp: flutter_markdownflutter/packages flutter_markdownflutter/packages flutter_markdownpackageflutter/packages repository. See also p: labels.flutter/packages repository. See also p: labels.r: invalidIssue is closed as not validIssue is closed as not validteam-ecosystemOwned by Ecosystem teamOwned by Ecosystem teamtriaged-ecosystemTriaged by Ecosystem teamTriaged by Ecosystem team
Description
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
Actual results
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();
}
Bharathh-Raj, Imgkl, alickwong, MrAlek, jusuf97 and 2 more
Metadata
Metadata
Assignees
Labels
P2Important issues not at the top of the work listImportant issues not at the top of the work listfound in release: 3.27Found to occur in 3.27Found to occur in 3.27found in release: 3.28Found to occur in 3.28Found to occur in 3.28has reproducible stepsThe issue has been confirmed reproducible and is ready to work onThe issue has been confirmed reproducible and is ready to work onp: flutter_markdownflutter/packages flutter_markdownflutter/packages flutter_markdownpackageflutter/packages repository. See also p: labels.flutter/packages repository. See also p: labels.r: invalidIssue is closed as not validIssue is closed as not validteam-ecosystemOwned by Ecosystem teamOwned by Ecosystem teamtriaged-ecosystemTriaged by Ecosystem teamTriaged by Ecosystem team