Fixing MD Parsing for multi space

pull/1828/head
dbrezack 1 year ago
parent b547e281c9
commit 1e36914bf3
  1. 32
      lib/src/packages/quill_markdown/markdown_to_delta.dart

@ -21,8 +21,7 @@ typedef ElementToEmbeddableConvertor = Embeddable Function(
); );
/// Convertor from Markdown string to quill [Delta]. /// Convertor from Markdown string to quill [Delta].
class MarkdownToDelta extends Converter<String, Delta> class MarkdownToDelta extends Converter<String, Delta> implements md.NodeVisitor {
implements md.NodeVisitor {
/// ///
MarkdownToDelta({ MarkdownToDelta({
required this.markdownDocument, required this.markdownDocument,
@ -117,9 +116,7 @@ class MarkdownToDelta extends Converter<String, Delta>
_justPreviousBlockExit = false; _justPreviousBlockExit = false;
_listItemIndent = -1; _listItemIndent = -1;
final lines = const LineSplitter().convert(input); final mdNodes = markdownDocument.parseInline(input);
final mdNodes = markdownDocument.parseLines(lines);
_topLevelNodes.addAll(mdNodes); _topLevelNodes.addAll(mdNodes);
for (final node in mdNodes) { for (final node in mdNodes) {
@ -146,9 +143,7 @@ class MarkdownToDelta extends Converter<String, Delta>
if (_isInBlockQuote) { if (_isInBlockQuote) {
renderedText = text.text; renderedText = text.text;
} else if (_isInCodeblock) { } else if (_isInCodeblock) {
renderedText = text.text.endsWith('\n') renderedText = text.text.endsWith('\n') ? text.text.substring(0, text.text.length - 1) : text.text;
? text.text.substring(0, text.text.length - 1)
: text.text;
} else { } else {
renderedText = _trimTextToMdSpec(text.text); renderedText = _trimTextToMdSpec(text.text);
} }
@ -250,9 +245,7 @@ class MarkdownToDelta extends Converter<String, Delta>
} }
void _insertNewLineBeforeElementIfNeeded(md.Element element) { void _insertNewLineBeforeElementIfNeeded(md.Element element) {
if (!_isInBlockQuote && if (!_isInBlockQuote && _lastTag == 'blockquote' && element.tag == 'blockquote') {
_lastTag == 'blockquote' &&
element.tag == 'blockquote') {
_insertNewLine(); _insertNewLine();
return; return;
} }
@ -290,10 +283,7 @@ class MarkdownToDelta extends Converter<String, Delta>
return; return;
} }
if (!_justPreviousBlockExit && if (!_justPreviousBlockExit && (_isTopLevelNode(element) || _haveBlockAttrs(element) || element.tag == 'li')) {
(_isTopLevelNode(element) ||
_haveBlockAttrs(element) ||
element.tag == 'li')) {
_justPreviousBlockExit = true; _justPreviousBlockExit = true;
_insertNewLine(); _insertNewLine();
return; return;
@ -378,8 +368,7 @@ class MarkdownToDelta extends Converter<String, Delta>
result = _effectiveElementToInlineAttr()[element.tag]?.call(element); result = _effectiveElementToInlineAttr()[element.tag]?.call(element);
} }
if (result == null) { if (result == null) {
throw Exception( throw Exception('Element $element cannot be converted to inline attribute');
'Element $element cannot be converted to inline attribute');
} }
return result; return result;
} }
@ -398,8 +387,7 @@ class MarkdownToDelta extends Converter<String, Delta>
List<Attribute<dynamic>> _toBlockAttributes(md.Element element) { List<Attribute<dynamic>> _toBlockAttributes(md.Element element) {
final result = _effectiveElementToBlockAttr()[element.tag]?.call(element); final result = _effectiveElementToBlockAttr()[element.tag]?.call(element);
if (result == null) { if (result == null) {
throw Exception( throw Exception('Element $element cannot be converted to block attribute');
'Element $element cannot be converted to block attribute');
} }
return result; return result;
} }
@ -411,12 +399,10 @@ class MarkdownToDelta extends Converter<String, Delta>
}; };
} }
bool _isEmbedElement(md.Element element) => bool _isEmbedElement(md.Element element) => _effectiveElementToEmbed().containsKey(element.tag);
_effectiveElementToEmbed().containsKey(element.tag);
Embeddable _toEmbeddable(md.Element element) { Embeddable _toEmbeddable(md.Element element) {
final result = final result = _effectiveElementToEmbed()[element.tag]?.call(element.attributes);
_effectiveElementToEmbed()[element.tag]?.call(element.attributes);
if (result == null) { if (result == null) {
throw Exception('Element $element cannot be converted to Embeddable'); throw Exception('Element $element cannot be converted to Embeddable');
} }

Loading…
Cancel
Save