Skip to content

Commit

Permalink
[flutter_markdown] Soft wrapping in blockquotes (#7848)
Browse files Browse the repository at this point in the history
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 flutter/flutter#156554
  • Loading branch information
andrechalella authored Oct 13, 2024
1 parent 53a0563 commit 17b714a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/flutter_markdown/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.7.4

* Makes paragraphs in blockquotes soft-wrap like a normal `<blockquote>` instead of hard-wrapping like a `<pre>` block.

## 0.7.3+2

* Resolves an issue where code blocks in markdown were not highlighted during selection.
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter_markdown/lib/src/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter_markdown/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions packages/flutter_markdown/test/blockquote_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Widget> widgets = tester.allWidgets;
expectTextStrings(widgets, <String>['soft wrap']);
},
);

testWidgets(
'should work with styling',
(WidgetTester tester) async {
Expand Down

0 comments on commit 17b714a

Please sign in to comment.