Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: markdown parser doesn't recognize softline breaks #933

Merged
merged 3 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ dependency_overrides:
git:
url: https://github.com/AppFlowy-IO/AppFlowy-plugins.git
path: "packages/appflowy_editor_plugins"
ref: "b228456"
ref: "2d3d4fb"

dev_dependencies:
flutter_test:
Expand Down
10 changes: 4 additions & 6 deletions lib/src/plugins/markdown/decoder/document_markdown_decoder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ class DocumentMarkdownDecoder extends Converter<String, Document> {
],
encodeHtml: false,
).parse(input);
final document = Document.blank();

final document = Document.blank();
final nodes = mdNodes
.map((mdNode) => _parseNode(mdNode))
.toList()
.map((e) => _parseNode(e))
.whereNotNull()
.toList()
.flattened;

.flattened
.toList(growable: false); // avoid lazy evaluation
if (nodes.isNotEmpty) {
document.insert([0], nodes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,51 @@
}
}

final deltaDecoder = DeltaMarkdownDecoder();
return [
paragraphNode(
delta: deltaDecoder.convertNodes(element.children),
),
];
if (ec == null || ec.isEmpty) {
// return empty paragraph node if there is no children
return [
paragraphNode(),

Check warning on line 34 in lib/src/plugins/markdown/decoder/parser/markdown_paragraph_parser.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/plugins/markdown/decoder/parser/markdown_paragraph_parser.dart#L33-L34

Added lines #L33 - L34 were not covered by tests
];
}

// Split the paragraph node by <br> tag
final splitContent = _splitByBrTag(ec);

// Transform each split content into a paragraph node
return splitContent.map((content) {
final deltaDecoder = DeltaMarkdownDecoder();
final delta = deltaDecoder.convertNodes(content);
return paragraphNode(delta: delta);
}).toList();
}
}

// split the <p> children by <br> tag, mostly it's used for handling the soft line break
// for example:
// ```html
// <p>
// Hello<br>World
// </p>
// ```
// will be split into:
// ```document
// Hello
//
// World
// ```
List<List<md.Node>> _splitByBrTag(List<md.Node> nodes) {
return nodes
.fold<List<List<md.Node>>>(
[[]],
(acc, node) {
if (node is md.Element && node.tag == 'br') {
acc.add([]);
} else {
acc.last.add(node);
}
return acc;
},
)
.where((group) => group.isNotEmpty)
.toList();
}
8 changes: 8 additions & 0 deletions test/plugins/markdown/document_markdown_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ void main() {
expect(document.toJson(), data);
});

test('soft line break with two spaces', () {
const markdown = 'first line \nsecond line';
final document = markdownToDocument(markdown);
expect(document.root.children.length, 2);
expect(document.root.children[0].delta?.toPlainText(), 'first line');
expect(document.root.children[1].delta?.toPlainText(), 'second line');
});

test('documentToMarkdown()', () {
final document = markdownToDocument(markdownDocument);
final markdown = documentToMarkdown(document);
Expand Down
Loading