Skip to content

Commit

Permalink
Merge pull request #35 from vishna/issue_34
Browse files Browse the repository at this point in the history
fix softline break getting removed before a newline link
  • Loading branch information
TarekkMA authored Oct 22, 2024
2 parents 40b3350 + 9e08669 commit f94b4d4
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 12 additions & 0 deletions lib/src/markdown_to_delta.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class MarkdownToDelta extends Converter<String, Delta>
bool _isInCodeblock = false;
bool _justPreviousBlockExit = false;
String? _lastTag;
bool _didRemoveTrailingSoftLineBreak = false;
String? _currentBlockTag;
int _listItemIndent = -1;

Expand Down Expand Up @@ -166,6 +167,9 @@ class MarkdownToDelta extends Converter<String, Delta>
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;
Expand All @@ -186,6 +190,7 @@ class MarkdownToDelta extends Converter<String, Delta>
bool visitElementBefore(md.Element element) {
_insertNewLineBeforeElementIfNeeded(element);

_didRemoveTrailingSoftLineBreak = false;
final tag = element.tag;
_currentBlockTag ??= tag;
_lastTag = tag;
Expand Down Expand Up @@ -275,6 +280,13 @@ class MarkdownToDelta extends Converter<String, Delta>
_insertNewLine();
return;
}

if (softLineBreak &&
_didRemoveTrailingSoftLineBreak &&
element.tag == 'a') {
_insertNewLine();
return;
}
}

void _insertNewLineAfterElementIfNeeded(md.Element element) {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
53 changes: 53 additions & 0 deletions test/issue_34_test.dart
Original file line number Diff line number Diff line change
@@ -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'),
]);
});
}

0 comments on commit f94b4d4

Please sign in to comment.