[flutter_markdown] Ignore leading space for p and li (#517)
diff --git a/packages/flutter_markdown/CHANGELOG.md b/packages/flutter_markdown/CHANGELOG.md
index 175d7fa..5683081 100644
--- a/packages/flutter_markdown/CHANGELOG.md
+++ b/packages/flutter_markdown/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 0.6.9
+
+ * Leading spaces in a paragraph and in list items are now ignored according to [GFM #192](https://github.github.com/gfm/#example-192) and [GFM #236](https://github.github.com/gfm/#example-236).
+
## 0.6.8
* Added option paddingBuilders
diff --git a/packages/flutter_markdown/lib/src/builder.dart b/packages/flutter_markdown/lib/src/builder.dart
index c94123f..0f6b20e 100644
--- a/packages/flutter_markdown/lib/src/builder.dart
+++ b/packages/flutter_markdown/lib/src/builder.dart
@@ -163,7 +163,7 @@
final List<_InlineElement> _inlines = <_InlineElement>[];
final List<GestureRecognizer> _linkHandlers = <GestureRecognizer>[];
String? _currentBlockTag;
- String? _lastTag;
+ String? _lastVisitedTag;
bool _isInBlockquote = false;
/// Returns widgets that display the given Markdown nodes.
@@ -194,6 +194,7 @@
bool visitElementBefore(md.Element element) {
final String tag = element.tag;
_currentBlockTag ??= tag;
+ _lastVisitedTag = tag;
if (builders.containsKey(tag)) {
builders[tag]!.visitElementBefore(element);
@@ -307,7 +308,10 @@
// Leading spaces following a hard line break are ignored.
// https://github.github.com/gfm/#example-657
- if (_lastTag == 'br') {
+ // Leading spaces in paragraph or list item are ignored
+ // https://github.github.com/gfm/#example-192
+ // https://github.github.com/gfm/#example-236
+ if (const <String>['ul', 'ol', 'p', 'br'].contains(_lastVisitedTag)) {
text = text.replaceAll(_leadingSpacesPattern, '');
}
@@ -344,6 +348,8 @@
if (child != null) {
_inlines.last.children.add(child);
}
+
+ _lastVisitedTag = null;
}
@override
@@ -492,7 +498,7 @@
if (_currentBlockTag == tag) {
_currentBlockTag = null;
}
- _lastTag = tag;
+ _lastVisitedTag = tag;
}
Widget _buildImage(String src, String? title, String? alt) {
diff --git a/packages/flutter_markdown/pubspec.yaml b/packages/flutter_markdown/pubspec.yaml
index 1dbb23b..49d89ab 100644
--- a/packages/flutter_markdown/pubspec.yaml
+++ b/packages/flutter_markdown/pubspec.yaml
@@ -4,7 +4,7 @@
formatted with simple Markdown tags.
repository: https://github.com/flutter/packages/tree/master/packages/flutter_markdown
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+flutter_markdown%22
-version: 0.6.8
+version: 0.6.9
environment:
sdk: ">=2.12.0 <3.0.0"
diff --git a/packages/flutter_markdown/test/list_test.dart b/packages/flutter_markdown/test/list_test.dart
index 276aa3a..4aca6f4 100644
--- a/packages/flutter_markdown/test/list_test.dart
+++ b/packages/flutter_markdown/test/list_test.dart
@@ -53,6 +53,24 @@
]);
},
);
+
+ testWidgets(
+ // Example 236 from the GitHub Flavored Markdown specification.
+ 'leading space are ignored', (WidgetTester tester) async {
+ const String data = ' - one\n\n two';
+ await tester.pumpWidget(
+ boilerplate(
+ const MarkdownBody(data: data),
+ ),
+ );
+
+ final Iterable<Widget> widgets = tester.allWidgets;
+ expectTextStrings(widgets, <String>[
+ '•',
+ 'one',
+ 'two',
+ ]);
+ });
});
group('Ordered List', () {
@@ -82,6 +100,18 @@
]);
},
);
+
+ testWidgets('leading space are ignored', (WidgetTester tester) async {
+ const String data = ' 1. one\n\n two';
+ await tester.pumpWidget(
+ boilerplate(
+ const MarkdownBody(data: data),
+ ),
+ );
+
+ final Iterable<Widget> widgets = tester.allWidgets;
+ expectTextStrings(widgets, <String>['1.', 'one', 'two']);
+ });
});
group('Task List', () {
diff --git a/packages/flutter_markdown/test/text_test.dart b/packages/flutter_markdown/test/text_test.dart
index eca1b6f..0cb1324 100644
--- a/packages/flutter_markdown/test/text_test.dart
+++ b/packages/flutter_markdown/test/text_test.dart
@@ -66,6 +66,24 @@
);
});
+ group('Leading spaces', () {
+ testWidgets(
+ // Example 192 from the GitHub Flavored Markdown specification.
+ 'leading space are ignored', (WidgetTester tester) async {
+ const String data = ' aaa\n bbb';
+ await tester.pumpWidget(
+ boilerplate(
+ const MarkdownBody(data: data),
+ ),
+ );
+
+ final Iterable<Widget> widgets = tester.allWidgets;
+ expectWidgetTypes(widgets,
+ <Type>[Directionality, MarkdownBody, Column, Wrap, RichText]);
+ expectTextStrings(widgets, <String>['aaa bbb']);
+ });
+ });
+
group('Line Break', () {
testWidgets(
// Example 654 from the GitHub Flavored Markdown specification.