瀏覽代碼

Merge pull request #1540 from LucasXu0/missing_code_block_markdown

fix: add missing markdown converter for code block and divider
Lucas.Xu 2 年之前
父節點
當前提交
52d65c4c4f

+ 2 - 0
frontend/app_flowy/packages/appflowy_editor/lib/src/plugins/markdown/decoder/document_markdown_decoder.dart

@@ -80,6 +80,8 @@ class DocumentMarkdownDecoder extends Converter<String, Document> {
           BuiltInAttributeKey.subtype: BuiltInAttributeKey.quote,
           BuiltInAttributeKey.subtype: BuiltInAttributeKey.quote,
         },
         },
       );
       );
+    } else if (text.isNotEmpty && RegExp(r'^-*').stringMatch(text) == text) {
+      return Node(type: 'divider');
     }
     }
 
 
     if (text.isNotEmpty) {
     if (text.isNotEmpty) {

+ 2 - 0
frontend/app_flowy/packages/appflowy_editor/lib/src/plugins/markdown/encoder/document_markdown_encoder.dart

@@ -1,6 +1,7 @@
 import 'dart:convert';
 import 'dart:convert';
 
 
 import 'package:appflowy_editor/src/core/document/document.dart';
 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/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/node_parser.dart';
 import 'package:appflowy_editor/src/plugins/markdown/encoder/parser/text_node_parser.dart';
 import 'package:appflowy_editor/src/plugins/markdown/encoder/parser/text_node_parser.dart';
@@ -10,6 +11,7 @@ class DocumentMarkdownEncoder extends Converter<Document, String> {
     this.parsers = const [
     this.parsers = const [
       TextNodeParser(),
       TextNodeParser(),
       ImageNodeParser(),
       ImageNodeParser(),
+      DividerNodeParser(),
     ],
     ],
   });
   });
 
 

+ 14 - 0
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';
+  }
+}

+ 1 - 1
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') {
       } else if (subtype == 'quote') {
         result = '> $markdown';
         result = '> $markdown';
-      } else if (subtype == 'code-block') {
+      } else if (subtype == 'code_block') {
         result = '```\n$markdown\n```';
         result = '```\n$markdown\n```';
       } else if (subtype == 'bulleted-list') {
       } else if (subtype == 'bulleted-list') {
         result = '* $markdown';
         result = '* $markdown';

+ 15 - 0
frontend/app_flowy/packages/appflowy_editor/test/plugins/markdown/encoder/parser/divider_node_parser_test.dart

@@ -0,0 +1,15 @@
+import 'package:appflowy_editor/appflowy_editor.dart';
+import 'package:appflowy_editor/src/plugins/markdown/encoder/parser/divider_node_parser.dart';
+import 'package:flutter_test/flutter_test.dart';
+
+void main() async {
+  group('divider_node_parser.dart', () {
+    test('parser divider node', () {
+      final node = Node(
+        type: 'divider',
+      );
+      final result = const DividerNodeParser().transform(node);
+      expect(result, '---\n');
+    });
+  });
+}

+ 1 - 1
frontend/app_flowy/packages/appflowy_editor/test/plugins/markdown/encoder/parser/text_node_parser_test.dart

@@ -86,7 +86,7 @@ void main() async {
       final node = TextNode(
       final node = TextNode(
         delta: Delta(operations: [TextInsert(text)]),
         delta: Delta(operations: [TextInsert(text)]),
         attributes: {
         attributes: {
-          BuiltInAttributeKey.subtype: 'code-block',
+          BuiltInAttributeKey.subtype: 'code_block',
         },
         },
       );
       );
       expect(const TextNodeParser().transform(node), '```\n$text\n```');
       expect(const TextNodeParser().transform(node), '```\n$text\n```');