From 17b714a361460ac51b3dd51a1bbf0dec26fdbe3c Mon Sep 17 00:00:00 2001 From: andrechalella Date: Sat, 12 Oct 2024 23:56:24 -0300 Subject: [PATCH] [flutter_markdown] Soft wrapping in blockquotes (#7848) Makes `flutter_markdown` treat paragraphs in blockquotes like a normal paragraph with respect to soft wrapping. Currently, line breaks in blockquotes translate directly to line breaks in the output. This happens regardless of the Markdown Specification chosen. Such behavior is different from most Markdown implementations, including the reference ones cited in [flutter_markdown ](https://pub.dev/packages/flutter_markdown) ([Dart Markdown Live Editor](https://dart-lang.github.io/markdown/) and the [Markdown Live Preview](https://markdownlivepreview.com/)). Example: ``` > my > blockquote ``` Currently renders like: > my > blockquote And this PR fixes it to become: > my blockquote Note: previewing in this GitHub editor, I get the hard-wrap behavior. However, [Dart Markdown Live Editor](https://dart-lang.github.io/markdown/) gives the soft-wrap behavior in all flavors, even when "GitHub Flavored Markdown" is selected. Reading the GFM spec [there's an example](https://github.github.com/gfm/#example-222) that to me implies that soft-wrapping is the correct thing in GFM, plus that's what I gather from reading [6.12](https://github.github.com/gfm/#hard-line-breaks) and [6.13](https://github.github.com/gfm/#soft-line-breaks). However, if for some reason we should hard-wrap in GFM, it should be just a matter of checking the chosen `extensionSet` when deciding whether to call `trimText()`. Seems clear to me hard-wrapping shouldn't ever be the only behavior. Issues: this fixes https://github.com/flutter/flutter/issues/156554 --- packages/flutter_markdown/CHANGELOG.md | 4 ++++ packages/flutter_markdown/lib/src/builder.dart | 2 +- packages/flutter_markdown/pubspec.yaml | 2 +- .../flutter_markdown/test/blockquote_test.dart | 14 ++++++++++++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/packages/flutter_markdown/CHANGELOG.md b/packages/flutter_markdown/CHANGELOG.md index b6df88cad18e..dba9cb958789 100644 --- a/packages/flutter_markdown/CHANGELOG.md +++ b/packages/flutter_markdown/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.7.4 + +* Makes paragraphs in blockquotes soft-wrap like a normal `
` instead of hard-wrapping like a `
` block.
+
 ## 0.7.3+2
 
 * Resolves an issue where code blocks in markdown were not highlighted during selection.
diff --git a/packages/flutter_markdown/lib/src/builder.dart b/packages/flutter_markdown/lib/src/builder.dart
index 0ec8833c42c0..3a0f4b15e993 100644
--- a/packages/flutter_markdown/lib/src/builder.dart
+++ b/packages/flutter_markdown/lib/src/builder.dart
@@ -362,7 +362,7 @@ class MarkdownBuilder implements md.NodeVisitor {
           style: _isInBlockquote
               ? styleSheet.blockquote!.merge(_inlines.last.style)
               : _inlines.last.style,
-          text: _isInBlockquote ? text.text : trimText(text.text),
+          text: trimText(text.text),
           recognizer: _linkHandlers.isNotEmpty ? _linkHandlers.last : null,
         ),
         textAlign: _textAlignForBlockTag(_currentBlockTag),
diff --git a/packages/flutter_markdown/pubspec.yaml b/packages/flutter_markdown/pubspec.yaml
index c6008933453c..f0ac1fd83533 100644
--- a/packages/flutter_markdown/pubspec.yaml
+++ b/packages/flutter_markdown/pubspec.yaml
@@ -4,7 +4,7 @@ description: A Markdown renderer for Flutter. Create rich text output,
   formatted with simple Markdown tags.
 repository: https://github.com/flutter/packages/tree/main/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.7.3+2
+version: 0.7.4
 
 environment:
   sdk: ^3.3.0
diff --git a/packages/flutter_markdown/test/blockquote_test.dart b/packages/flutter_markdown/test/blockquote_test.dart
index 2d1fa3e30a4c..07e88f4c9e09 100644
--- a/packages/flutter_markdown/test/blockquote_test.dart
+++ b/packages/flutter_markdown/test/blockquote_test.dart
@@ -25,6 +25,20 @@ void defineTests() {
       },
     );
 
+    testWidgets(
+      'soft wrapping in blockquote',
+      (WidgetTester tester) async {
+        await tester.pumpWidget(
+          boilerplate(
+            const MarkdownBody(data: '> soft\n> wrap'),
+          ),
+        );
+
+        final Iterable widgets = tester.allWidgets;
+        expectTextStrings(widgets, ['soft wrap']);
+      },
+    );
+
     testWidgets(
       'should work with styling',
       (WidgetTester tester) async {