From 33013d186c4c4f7c4edfd62e9d1ae3bfc13482b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wi=C5=9Bniewski?= Date: Mon, 21 Oct 2024 16:49:58 +0200 Subject: [PATCH 1/2] fix softline break getting removed before a newline link closes #34 --- lib/src/markdown_to_delta.dart | 12 ++++++++ test/issue_34_test.dart | 53 ++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 test/issue_34_test.dart diff --git a/lib/src/markdown_to_delta.dart b/lib/src/markdown_to_delta.dart index 7966fb4..bb02179 100644 --- a/lib/src/markdown_to_delta.dart +++ b/lib/src/markdown_to_delta.dart @@ -110,6 +110,7 @@ class MarkdownToDelta extends Converter bool _isInCodeblock = false; bool _justPreviousBlockExit = false; String? _lastTag; + bool _didRemoveTrailingSoftLineBreak = false; String? _currentBlockTag; int _listItemIndent = -1; @@ -166,6 +167,9 @@ class MarkdownToDelta extends Converter var lines = renderedText.split('\n'); if (renderedText.endsWith('\n')) { lines = lines.sublist(0, lines.length - 1); + if (softLineBreak) { + _didRemoveTrailingSoftLineBreak = true; + } } for (var i = 0; i < lines.length; i++) { final isLastItem = i == lines.length - 1; @@ -186,6 +190,7 @@ class MarkdownToDelta extends Converter bool visitElementBefore(md.Element element) { _insertNewLineBeforeElementIfNeeded(element); + _didRemoveTrailingSoftLineBreak = false; final tag = element.tag; _currentBlockTag ??= tag; _lastTag = tag; @@ -275,6 +280,13 @@ class MarkdownToDelta extends Converter _insertNewLine(); return; } + + if (softLineBreak && + _didRemoveTrailingSoftLineBreak && + element.tag == 'a') { + _insertNewLine(); + return; + } } void _insertNewLineAfterElementIfNeeded(md.Element element) { diff --git a/test/issue_34_test.dart b/test/issue_34_test.dart new file mode 100644 index 0000000..83cc88e --- /dev/null +++ b/test/issue_34_test.dart @@ -0,0 +1,53 @@ +import 'package:flutter_quill/quill_delta.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:markdown/markdown.dart' as md; +import 'package:markdown_quill/markdown_quill.dart'; + +void main() { + test('softLineBreak + link', () { + // given + const input = ''' +A text +[a link](https://flutter.dev) +'''; + + // when + final delta = MarkdownToDelta( + markdownDocument: md.Document( + encodeHtml: false, + ), + softLineBreak: true, + ).convert(input); + + // then + expect(delta.operations, [ + Operation.insert('A text\n'), + Operation.insert('a link', {'link': 'https://flutter.dev'}), + Operation.insert('\n'), + ]); + }); + + test('softLineBreak + link (an extra line at the end)', () { + // given + const input = ''' +A text +[a link](https://flutter.dev) +test! +'''; + + // when + final delta = MarkdownToDelta( + markdownDocument: md.Document( + encodeHtml: false, + ), + softLineBreak: true, + ).convert(input); + + // then + expect(delta.operations, [ + Operation.insert('A text\n'), + Operation.insert('a link', {'link': 'https://flutter.dev'}), + Operation.insert('\ntest!\n'), + ]); + }); +} From 9e0866924ae6e15579f93b30878bbbefc35f47d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wi=C5=9Bniewski?= Date: Tue, 22 Oct 2024 11:18:20 +0200 Subject: [PATCH 2/2] bump up pubspec + update changelog --- CHANGELOG.md | 4 ++++ pubspec.yaml | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2534dde..ca35f50 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 4.2.0 + +* Fix `softLineBreak` not respected when a new line is starting with a link [#34](https://github.com/TarekkMA/markdown_quill/issues/34) + ## 4.1.0 * Add `DeltaToMarkdown.customContentHandler` that allows you to configure how special characters are escaped diff --git a/pubspec.yaml b/pubspec.yaml index 7b9f8d1..d944569 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: markdown_quill description: Convert between quill (delta) format and markdown -version: 4.1.0 +version: 4.2.0 repository: https://github.com/TarekkMA/markdown_quill environment: