Reformat code

pull/1609/head
agu 1 year ago
parent 33e7c46288
commit 78abceadf1
  1. 31
      lib/src/packages/quill_markdown/delta_to_markdown.dart

@ -2,6 +2,7 @@ import 'dart:convert';
import 'dart:ui'; import 'dart:ui';
import 'package:collection/collection.dart'; import 'package:collection/collection.dart';
import '../../../flutter_quill.dart'; import '../../../flutter_quill.dart';
import '../../../quill_delta.dart'; import '../../../quill_delta.dart';
import './custom_quill_attributes.dart'; import './custom_quill_attributes.dart';
@ -37,8 +38,7 @@ extension on Object? {
} }
/// Convertor from [Delta] to quill Markdown string. /// Convertor from [Delta] to quill Markdown string.
class DeltaToMarkdown extends Converter<Delta, String> class DeltaToMarkdown extends Converter<Delta, String> implements _NodeVisitor<StringSink> {
implements _NodeVisitor<StringSink> {
/// ///
DeltaToMarkdown({ DeltaToMarkdown({
Map<String, EmbedToMarkdown>? customEmbedHandlers, Map<String, EmbedToMarkdown>? customEmbedHandlers,
@ -70,8 +70,9 @@ class DeltaToMarkdown extends Converter<Delta, String>
); );
} }
if (infoString.isEmpty) { if (infoString.isEmpty) {
final linesWithLang = (node as Block).children.where((child) => final linesWithLang = (node as Block)
child.containsAttr(CodeBlockLanguageAttribute.attrKey)); .children
.where((child) => child.containsAttr(CodeBlockLanguageAttribute.attrKey));
if (linesWithLang.isNotEmpty) { if (linesWithLang.isNotEmpty) {
infoString = linesWithLang.first.getAttrValueOr( infoString = linesWithLang.first.getAttrValueOr(
CodeBlockLanguageAttribute.attrKey, CodeBlockLanguageAttribute.attrKey,
@ -159,8 +160,7 @@ class DeltaToMarkdown extends Converter<Delta, String>
), ),
Attribute.link.key: _AttributeHandler( Attribute.link.key: _AttributeHandler(
beforeContent: (attribute, node, output) { beforeContent: (attribute, node, output) {
if (node.previous?.containsAttr(attribute.key, attribute.value) != if (node.previous?.containsAttr(attribute.key, attribute.value) != true) {
true) {
output.write('['); output.write('[');
} }
}, },
@ -210,8 +210,7 @@ class DeltaToMarkdown extends Converter<Delta, String>
leaf.accept(this, out); leaf.accept(this, out);
} }
}); });
if (style.isEmpty || if (style.isEmpty || style.values.every((item) => item.scope != AttributeScope.block)) {
style.values.every((item) => item.scope != AttributeScope.block)) {
out.writeln(); out.writeln();
} }
if (style.containsKey(Attribute.list.key) && if (style.containsKey(Attribute.list.key) &&
@ -234,10 +233,9 @@ class DeltaToMarkdown extends Converter<Delta, String>
var content = text.value; var content = text.value;
if (!(style.containsKey(Attribute.codeBlock.key) || if (!(style.containsKey(Attribute.codeBlock.key) ||
style.containsKey(Attribute.inlineCode.key) || style.containsKey(Attribute.inlineCode.key) ||
(text.parent?.style.containsKey(Attribute.codeBlock.key) ?? (text.parent?.style.containsKey(Attribute.codeBlock.key) ?? false))) {
false))) { content =
content = content.replaceAllMapped( content.replaceAllMapped(RegExp(r'[\\\`\*\_\{\}\[\]\(\)\#\+\-\.\!\>\<]'), (match) {
RegExp(r'[\\\`\*\_\{\}\[\]\(\)\#\+\-\.\!\>\<]'), (match) {
return '\\${match[0]}'; return '\\${match[0]}';
}); });
} }
@ -266,9 +264,8 @@ class DeltaToMarkdown extends Converter<Delta, String>
VoidCallback contentHandler, { VoidCallback contentHandler, {
bool sortedAttrsBySpan = false, bool sortedAttrsBySpan = false,
}) { }) {
final attrs = sortedAttrsBySpan final attrs =
? node.attrsSortedByLongestSpan() sortedAttrsBySpan ? node.attrsSortedByLongestSpan() : node.style.attributes.values.toList();
: node.style.attributes.values.toList();
final handlersToUse = attrs final handlersToUse = attrs
.where((attr) => handlers.containsKey(attr.key)) .where((attr) => handlers.containsKey(attr.key))
.map((attr) => MapEntry(attr.key, handlers[attr.key]!)) .map((attr) => MapEntry(attr.key, handlers[attr.key]!))
@ -352,8 +349,8 @@ extension _NodeX on Node {
node = node.next!; node = node.next!;
} }
final attrs = style.attributes.values.sorted( final attrs = style.attributes.values
(attr1, attr2) => attrCount[attr2]!.compareTo(attrCount[attr1]!)); .sorted((attr1, attr2) => attrCount[attr2]!.compareTo(attrCount[attr1]!));
return attrs; return attrs;
} }

Loading…
Cancel
Save