From 29952bc7fdf0bc067385087a68277a6b1c72f72a Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Tue, 6 Dec 2022 17:24:10 +0800 Subject: [PATCH] fix: add missing markdown converter for code block and divider --- .../decoder/document_markdown_decoder.dart | 2 ++ .../encoder/document_markdown_encoder.dart | 2 ++ .../encoder/parser/divider_node_parser.dart | 14 ++++++++++++++ .../markdown/encoder/parser/text_node_parser.dart | 2 +- 4 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 frontend/app_flowy/packages/appflowy_editor/lib/src/plugins/markdown/encoder/parser/divider_node_parser.dart diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/plugins/markdown/decoder/document_markdown_decoder.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/plugins/markdown/decoder/document_markdown_decoder.dart index 257cfabb28..05b8c4a1ff 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/src/plugins/markdown/decoder/document_markdown_decoder.dart +++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/plugins/markdown/decoder/document_markdown_decoder.dart @@ -80,6 +80,8 @@ class DocumentMarkdownDecoder extends Converter { BuiltInAttributeKey.subtype: BuiltInAttributeKey.quote, }, ); + } else if (RegExp(r'^-*').stringMatch(text) == text) { + return Node(type: 'divider'); } if (text.isNotEmpty) { diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/plugins/markdown/encoder/document_markdown_encoder.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/plugins/markdown/encoder/document_markdown_encoder.dart index 52c2bd3756..5a666bd443 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/src/plugins/markdown/encoder/document_markdown_encoder.dart +++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/plugins/markdown/encoder/document_markdown_encoder.dart @@ -1,6 +1,7 @@ import 'dart:convert'; import 'package:appflowy_editor/src/core/document/document.dart'; +import 'package:appflowy_editor/src/plugins/markdown/encoder/parser/divider_node_parser.dart'; import 'package:appflowy_editor/src/plugins/markdown/encoder/parser/image_node_parser.dart'; import 'package:appflowy_editor/src/plugins/markdown/encoder/parser/node_parser.dart'; import 'package:appflowy_editor/src/plugins/markdown/encoder/parser/text_node_parser.dart'; @@ -10,6 +11,7 @@ class DocumentMarkdownEncoder extends Converter { this.parsers = const [ TextNodeParser(), ImageNodeParser(), + DividerNodeParser(), ], }); diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/plugins/markdown/encoder/parser/divider_node_parser.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/plugins/markdown/encoder/parser/divider_node_parser.dart new file mode 100644 index 0000000000..c9742fbd60 --- /dev/null +++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/plugins/markdown/encoder/parser/divider_node_parser.dart @@ -0,0 +1,14 @@ +import 'package:appflowy_editor/src/core/document/node.dart'; +import 'package:appflowy_editor/src/plugins/markdown/encoder/parser/node_parser.dart'; + +class DividerNodeParser extends NodeParser { + const DividerNodeParser(); + + @override + String get id => 'divider'; + + @override + String transform(Node node) { + return '---\n'; + } +} diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/plugins/markdown/encoder/parser/text_node_parser.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/plugins/markdown/encoder/parser/text_node_parser.dart index 8857310876..f6532c91d1 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/src/plugins/markdown/encoder/parser/text_node_parser.dart +++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/plugins/markdown/encoder/parser/text_node_parser.dart @@ -40,7 +40,7 @@ class TextNodeParser extends NodeParser { } } else if (subtype == 'quote') { result = '> $markdown'; - } else if (subtype == 'code-block') { + } else if (subtype == 'code_block') { result = '```\n$markdown\n```'; } else if (subtype == 'bulleted-list') { result = '* $markdown';