Skip to content

Commit

Permalink
Fix markdown decoder not recognizing all ul tokens
Browse files Browse the repository at this point in the history
Fixes #442
  • Loading branch information
Amir-P committed Jan 1, 2025
1 parent c391628 commit 49c3c02
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
16 changes: 11 additions & 5 deletions packages/parchment/lib/src/codecs/markdown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ import '../document/leaf.dart';
import '../document/line.dart';

class ParchmentMarkdownCodec extends Codec<ParchmentDocument, String> {
const ParchmentMarkdownCodec();
final String unorderedListToken;

const ParchmentMarkdownCodec({this.unorderedListToken = '*'});

@override
Converter<String, ParchmentDocument> get decoder =>
_ParchmentMarkdownDecoder();

@override
Converter<ParchmentDocument, String> get encoder =>
_ParchmentMarkdownEncoder();
_ParchmentMarkdownEncoder(unorderedListToken: unorderedListToken);
}

class _ParchmentMarkdownDecoder extends Converter<String, ParchmentDocument> {
Expand All @@ -36,7 +38,7 @@ class _ParchmentMarkdownDecoder extends Converter<String, ParchmentDocument> {
);

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'^> *(.*)');
Expand Down Expand Up @@ -354,9 +356,13 @@ class _ParchmentMarkdownDecoder extends Converter<String, ParchmentDocument> {
}

class _ParchmentMarkdownEncoder extends Converter<ParchmentDocument, String> {
static final simpleBlocks = <ParchmentAttribute, String>{
final String unorderedListToken;

_ParchmentMarkdownEncoder({required this.unorderedListToken});

late final simpleBlocks = <ParchmentAttribute, String>{
ParchmentAttribute.bq: '> ',
ParchmentAttribute.ul: '* ',
ParchmentAttribute.ul: '$unorderedListToken ',
ParchmentAttribute.ol: '. ',
};

Expand Down
23 changes: 22 additions & 1 deletion packages/parchment/test/codecs/markdown_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 49c3c02

Please sign in to comment.