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

feat: add markdown divider encoder parser #639

Merged
Show file tree
Hide file tree
Changes from 3 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 lib/src/plugins/markdown/document_markdown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:appflowy_editor/src/core/document/document.dart';
import 'package:appflowy_editor/src/plugins/markdown/decoder/document_markdown_decoder.dart';
import 'package:appflowy_editor/src/plugins/markdown/decoder/parser/custom_node_parser.dart';
import 'package:appflowy_editor/src/plugins/markdown/encoder/document_markdown_encoder.dart';
import 'package:appflowy_editor/src/plugins/markdown/encoder/parser/image_node_parser.dart';
import 'package:appflowy_editor/src/plugins/markdown/encoder/parser/parser.dart';
import 'package:markdown/markdown.dart' as md;

Expand Down Expand Up @@ -43,6 +42,7 @@ String documentToMarkdown(
const HeadingNodeParser(),
const ImageNodeParser(),
const TableNodeParser(),
const DividerNodeParser(),
],
).encode(document);
}
Expand Down
18 changes: 18 additions & 0 deletions lib/src/plugins/markdown/encoder/parser/divider_node_parser.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:appflowy_editor/appflowy_editor.dart';

class DividerNodeParser extends NodeParser {
const DividerNodeParser();

@override
String get id => DividerBlockKeys.type;

@override
String transform(Node node, DocumentMarkdownEncoder? encoder) {
final children = encoder?.convertNodes(node.children);
String markdown = '---\n';
if (children != null && children.isNotEmpty) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The children of the divider node must be empty.

markdown += children;

Check warning on line 14 in lib/src/plugins/markdown/encoder/parser/divider_node_parser.dart

View check run for this annotation

Codecov / codecov/patch

lib/src/plugins/markdown/encoder/parser/divider_node_parser.dart#L14

Added line #L14 was not covered by tests
}
return markdown;
}
}
10 changes: 6 additions & 4 deletions lib/src/plugins/markdown/encoder/parser/parser.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export 'bulleted_list_node_parser.dart';
export 'code_block_node_parser.dart';
export 'divider_node_parser.dart';
export 'heading_node_parser.dart';
export 'image_node_parser.dart';
export 'node_parser.dart';
export 'quote_node_parser.dart';
export 'numbered_list_node_parser.dart';
export 'todo_list_node_parser.dart';
export 'text_node_parser.dart';
export 'code_block_node_parser.dart';
export 'quote_node_parser.dart';
export 'table_node_parser.dart';
export 'text_node_parser.dart';
export 'todo_list_node_parser.dart';
4 changes: 1 addition & 3 deletions lib/src/plugins/plugins.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,5 @@ export 'markdown/decoder/delta_markdown_decoder.dart';
export 'markdown/document_markdown.dart';
export 'markdown/encoder/delta_markdown_encoder.dart';
export 'markdown/encoder/document_markdown_encoder.dart';
export 'markdown/encoder/parser/image_node_parser.dart';
export 'markdown/encoder/parser/node_parser.dart';
export 'markdown/encoder/parser/text_node_parser.dart';
export 'markdown/encoder/parser/parser.dart';
export 'quill_delta/quill_delta_encoder.dart';
1 change: 1 addition & 0 deletions test/plugins/markdown/document_markdown_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,5 @@ const markdownDocumentEncoded = """# Heading 1
## Heading 2
### Heading 3

---
""";
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dart:convert';

import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor/src/plugins/markdown/encoder/parser/parser.dart';
import 'package:flutter_test/flutter_test.dart';

void main() async {
Expand Down
20 changes: 20 additions & 0 deletions test/plugins/markdown/encoder/parser/divider_node_parser_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:flutter_test/flutter_test.dart';

void main() async {
group('divider_node_parser.dart', () {
test('parser divider node', () {
final node = Node(
type: DividerBlockKeys.type,
);

final result = const DividerNodeParser().transform(node, null);
expect(result, '---\n');
});

test('DividerNodeParser id getter', () {
const imageNodeParser = DividerNodeParser();
expect(imageNodeParser.id, 'divider');
});
});
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:appflowy_editor/appflowy_editor.dart';
import 'package:appflowy_editor/src/plugins/markdown/encoder/parser/parser.dart';
import 'package:flutter_test/flutter_test.dart';

void main() async {
Expand Down
Loading