diff --git a/packages/parchment/lib/src/codecs/markdown.dart b/packages/parchment/lib/src/codecs/markdown.dart index c6e314e9..5b526902 100644 --- a/packages/parchment/lib/src/codecs/markdown.dart +++ b/packages/parchment/lib/src/codecs/markdown.dart @@ -9,7 +9,9 @@ import '../document/leaf.dart'; import '../document/line.dart'; class ParchmentMarkdownCodec extends Codec { - const ParchmentMarkdownCodec(); + final String unorderedListToken; + + const ParchmentMarkdownCodec({this.unorderedListToken = '*'}); @override Converter get decoder => @@ -17,7 +19,7 @@ class ParchmentMarkdownCodec extends Codec { @override Converter get encoder => - _ParchmentMarkdownEncoder(); + _ParchmentMarkdownEncoder(unorderedListToken: unorderedListToken); } class _ParchmentMarkdownDecoder extends Converter { @@ -36,7 +38,7 @@ class _ParchmentMarkdownDecoder extends Converter { ); static final _linkRegExp = RegExp(r'\[(.+?)\]\(([^)]+)\)'); - static final _ulRegExp = RegExp(r'^( *)\* +(.*)'); + static final _ulRegExp = RegExp(r'^( *)[(-|*|+)] +(.*)'); static final _olRegExp = RegExp(r'^( *)\d+[.)] +(.*)'); static final _clRegExp = RegExp(r'^( *)- +\[( |x|X)\] +(.*)'); static final _bqRegExp = RegExp(r'^> *(.*)'); @@ -99,9 +101,9 @@ class _ParchmentMarkdownDecoder extends Converter { return true; } - if (_handleOrderedList(line, delta, style) || - _handleUnorderedList(line, delta, style) || - _handleCheckList(line, delta, style)) { + if (_handleCheckList(line, delta, style) || + _handleOrderedList(line, delta, style) || + _handleUnorderedList(line, delta, style)) { return true; } @@ -354,9 +356,13 @@ class _ParchmentMarkdownDecoder extends Converter { } class _ParchmentMarkdownEncoder extends Converter { - static final simpleBlocks = { + final String unorderedListToken; + + _ParchmentMarkdownEncoder({required this.unorderedListToken}); + + late final simpleBlocks = { ParchmentAttribute.bq: '> ', - ParchmentAttribute.ul: '* ', + ParchmentAttribute.ul: '$unorderedListToken ', ParchmentAttribute.ol: '. ', }; diff --git a/packages/parchment/test/codecs/markdown_test.dart b/packages/parchment/test/codecs/markdown_test.dart index f0f52820..369b72c7 100644 --- a/packages/parchment/test/codecs/markdown_test.dart +++ b/packages/parchment/test/codecs/markdown_test.dart @@ -347,12 +347,17 @@ void main() { }); test('ul', () { - var markdown = '* a bullet point\n* another bullet point\n\n'; + var markdown = + '* a bullet point\n* another bullet point\n- list with -\n + list with +\n'; final act = parchmentMarkdown.decode(markdown).toDelta(); final exp = Delta() ..insert('a bullet point') ..insert('\n', {'block': 'ul'}) ..insert('another bullet point') + ..insert('\n', {'block': 'ul'}) + ..insert('list with -') + ..insert('\n', {'block': 'ul'}) + ..insert('list with +') ..insert('\n', {'block': 'ul'}); expect(act, exp); }); @@ -588,6 +593,22 @@ void main() { runFor(ParchmentAttribute.code, 'List item', '```\nList item\n```\n\n'); }); + test('ul', () { + final delta = Delta() + ..insert('Hello') + ..insert('\n', ParchmentAttribute.ul.toJson()); + expect(parchmentMarkdown.encode(ParchmentDocument.fromDelta(delta)), + '* Hello\n\n'); + expect( + ParchmentMarkdownCodec(unorderedListToken: '-') + .encode(ParchmentDocument.fromDelta(delta)), + '- Hello\n\n'); + expect( + ParchmentMarkdownCodec(unorderedListToken: '+') + .encode(ParchmentDocument.fromDelta(delta)), + '+ Hello\n\n'); + }); + test('ol', () { final delta = Delta() ..insert('Hello')