feat: remove the codec code from main project

This commit is contained in:
Lucas.Xu 2022-11-08 21:36:51 +08:00
parent 888c1b86f0
commit 76d1267aa5
6 changed files with 3 additions and 162 deletions

View File

@ -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<Document, String> {
const AppFlowyEditorMarkdownCodec();
@override
Converter<String, Document> get decoder => throw UnimplementedError();
@override
Converter<Document, String> get encoder {
return AppFlowyEditorMarkdownEncoder();
}
}

View File

@ -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']})';
}
}

View File

@ -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<Document, String> {
AppFlowyEditorMarkdownEncoder({
this.parsers = const [
TextNodeParser(),
ImageNodeParser(),
],
});
final List<NodeParser> 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<T> on Iterable<T> {
T? firstWhereOrNull(bool Function(T element) test) {
for (var element in this) {
if (test(element)) return element;
}
return null;
}
}

View File

@ -1,8 +0,0 @@
import 'package:appflowy_editor/appflowy_editor.dart';
abstract class NodeParser {
const NodeParser();
String get id;
String transform(Node node);
}

View File

@ -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';
}
}

View File

@ -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<Document, String> {
const AppFlowyEditorMarkdownCodec();
@override
Converter<String, Document> get decoder => throw UnimplementedError();
Converter<String, Document> get decoder => DocumentMarkdownDecoder();
@override
Converter<Document, String> get encoder {
return DocumentMarkdownEncoder();
}
Converter<Document, String> get encoder => DocumentMarkdownEncoder();
}