From 76d1267aa5dfe2f67773d6565ba8f837a600020d Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Tue, 8 Nov 2022 21:36:51 +0800 Subject: [PATCH] feat: remove the codec code from main project --- .../markdown/document_markdown.dart | 29 -------- .../src/parser/image_node_parser.dart | 14 ---- .../markdown/src/parser/markdown_encoder.dart | 39 ----------- .../markdown/src/parser/node_parser.dart | 8 --- .../markdown/src/parser/text_node_parser.dart | 68 ------------------- .../plugins/markdown/document_markdown.dart | 7 +- 6 files changed, 3 insertions(+), 162 deletions(-) delete mode 100644 frontend/app_flowy/lib/workspace/application/markdown/document_markdown.dart delete mode 100644 frontend/app_flowy/lib/workspace/application/markdown/src/parser/image_node_parser.dart delete mode 100644 frontend/app_flowy/lib/workspace/application/markdown/src/parser/markdown_encoder.dart delete mode 100644 frontend/app_flowy/lib/workspace/application/markdown/src/parser/node_parser.dart delete mode 100644 frontend/app_flowy/lib/workspace/application/markdown/src/parser/text_node_parser.dart diff --git a/frontend/app_flowy/lib/workspace/application/markdown/document_markdown.dart b/frontend/app_flowy/lib/workspace/application/markdown/document_markdown.dart deleted file mode 100644 index 71d0137280..0000000000 --- a/frontend/app_flowy/lib/workspace/application/markdown/document_markdown.dart +++ /dev/null @@ -1,29 +0,0 @@ -library delta_markdown; - -import 'dart:convert'; - -import 'package:appflowy_editor/appflowy_editor.dart' show Document; -import 'package:app_flowy/workspace/application/markdown/src/parser/markdown_encoder.dart'; - -/// Codec used to convert between Markdown and AppFlowy Editor Document. -const AppFlowyEditorMarkdownCodec _kCodec = AppFlowyEditorMarkdownCodec(); - -Document markdownToDocument(String markdown) { - return _kCodec.decode(markdown); -} - -String documentToMarkdown(Document document) { - return _kCodec.encode(document); -} - -class AppFlowyEditorMarkdownCodec extends Codec { - const AppFlowyEditorMarkdownCodec(); - - @override - Converter get decoder => throw UnimplementedError(); - - @override - Converter get encoder { - return AppFlowyEditorMarkdownEncoder(); - } -} diff --git a/frontend/app_flowy/lib/workspace/application/markdown/src/parser/image_node_parser.dart b/frontend/app_flowy/lib/workspace/application/markdown/src/parser/image_node_parser.dart deleted file mode 100644 index 575cf13216..0000000000 --- a/frontend/app_flowy/lib/workspace/application/markdown/src/parser/image_node_parser.dart +++ /dev/null @@ -1,14 +0,0 @@ -import 'package:app_flowy/workspace/application/markdown/src/parser/node_parser.dart'; -import 'package:appflowy_editor/appflowy_editor.dart'; - -class ImageNodeParser extends NodeParser { - const ImageNodeParser(); - - @override - String get id => 'image'; - - @override - String transform(Node node) { - return '![](${node.attributes['image_src']})'; - } -} diff --git a/frontend/app_flowy/lib/workspace/application/markdown/src/parser/markdown_encoder.dart b/frontend/app_flowy/lib/workspace/application/markdown/src/parser/markdown_encoder.dart deleted file mode 100644 index f6c1e6ea02..0000000000 --- a/frontend/app_flowy/lib/workspace/application/markdown/src/parser/markdown_encoder.dart +++ /dev/null @@ -1,39 +0,0 @@ -import 'dart:convert'; - -import 'package:app_flowy/workspace/application/markdown/src/parser/image_node_parser.dart'; -import 'package:app_flowy/workspace/application/markdown/src/parser/node_parser.dart'; -import 'package:app_flowy/workspace/application/markdown/src/parser/text_node_parser.dart'; -import 'package:appflowy_editor/appflowy_editor.dart'; - -class AppFlowyEditorMarkdownEncoder extends Converter { - AppFlowyEditorMarkdownEncoder({ - this.parsers = const [ - TextNodeParser(), - ImageNodeParser(), - ], - }); - - final List parsers; - - @override - String convert(Document input) { - final buffer = StringBuffer(); - for (final node in input.root.children) { - NodeParser? parser = - parsers.firstWhereOrNull((element) => element.id == node.type); - if (parser != null) { - buffer.write(parser.transform(node)); - } - } - return buffer.toString(); - } -} - -extension IterableExtension on Iterable { - T? firstWhereOrNull(bool Function(T element) test) { - for (var element in this) { - if (test(element)) return element; - } - return null; - } -} diff --git a/frontend/app_flowy/lib/workspace/application/markdown/src/parser/node_parser.dart b/frontend/app_flowy/lib/workspace/application/markdown/src/parser/node_parser.dart deleted file mode 100644 index 649ca7eae7..0000000000 --- a/frontend/app_flowy/lib/workspace/application/markdown/src/parser/node_parser.dart +++ /dev/null @@ -1,8 +0,0 @@ -import 'package:appflowy_editor/appflowy_editor.dart'; - -abstract class NodeParser { - const NodeParser(); - - String get id; - String transform(Node node); -} diff --git a/frontend/app_flowy/lib/workspace/application/markdown/src/parser/text_node_parser.dart b/frontend/app_flowy/lib/workspace/application/markdown/src/parser/text_node_parser.dart deleted file mode 100644 index 0dbf6418aa..0000000000 --- a/frontend/app_flowy/lib/workspace/application/markdown/src/parser/text_node_parser.dart +++ /dev/null @@ -1,68 +0,0 @@ -import 'dart:convert'; - -import 'package:app_flowy/workspace/application/markdown/delta_markdown.dart'; -import 'package:app_flowy/workspace/application/markdown/src/parser/node_parser.dart'; -import 'package:appflowy_editor/appflowy_editor.dart'; - -class TextNodeParser extends NodeParser { - const TextNodeParser(); - - @override - String get id => 'text'; - - @override - String transform(Node node) { - assert(node is TextNode); - final textNode = node as TextNode; - final delta = jsonEncode( - textNode.delta - ..add(TextInsert('\n')) - ..toJson(), - ); - final markdown = deltaToMarkdown(delta); - final attributes = textNode.attributes; - var result = markdown; - var suffix = ''; - if (attributes.isNotEmpty && - attributes.containsKey(BuiltInAttributeKey.subtype)) { - final subtype = attributes[BuiltInAttributeKey.subtype]; - if (node.next?.subtype != subtype) { - suffix = '\n'; - } - if (subtype == 'heading') { - final heading = attributes[BuiltInAttributeKey.heading]; - if (heading == 'h1') { - result = '# $markdown'; - } else if (heading == 'h2') { - result = '## $markdown'; - } else if (heading == 'h3') { - result = '### $markdown'; - } else if (heading == 'h4') { - result = '#### $markdown'; - } else if (heading == 'h5') { - result = '##### $markdown'; - } else if (heading == 'h6') { - result = '###### $markdown'; - } - } else if (subtype == 'quote') { - result = '> $markdown'; - } else if (subtype == 'code') { - result = '`$markdown`'; - } else if (subtype == 'code-block') { - result = '```\n$markdown\n```'; - } else if (subtype == 'bulleted-list') { - result = '- $markdown'; - } else if (subtype == 'number-list') { - final number = attributes['number']; - result = '$number. $markdown'; - } else if (subtype == 'checkbox') { - if (attributes[BuiltInAttributeKey.checkbox] == true) { - result = '- [x] $markdown'; - } else { - result = '- [ ] $markdown'; - } - } - } - return '$result$suffix'; - } -} diff --git a/frontend/app_flowy/packages/appflowy_editor/lib/src/plugins/markdown/document_markdown.dart b/frontend/app_flowy/packages/appflowy_editor/lib/src/plugins/markdown/document_markdown.dart index 48714a6a85..224f9b6ccd 100644 --- a/frontend/app_flowy/packages/appflowy_editor/lib/src/plugins/markdown/document_markdown.dart +++ b/frontend/app_flowy/packages/appflowy_editor/lib/src/plugins/markdown/document_markdown.dart @@ -3,6 +3,7 @@ library delta_markdown; import 'dart:convert'; 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/encoder/document_markdown_encoder.dart'; /// Codec used to convert between Markdown and AppFlowy Editor Document. @@ -20,10 +21,8 @@ class AppFlowyEditorMarkdownCodec extends Codec { const AppFlowyEditorMarkdownCodec(); @override - Converter get decoder => throw UnimplementedError(); + Converter get decoder => DocumentMarkdownDecoder(); @override - Converter get encoder { - return DocumentMarkdownEncoder(); - } + Converter get encoder => DocumentMarkdownEncoder(); }