Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix markdown decoder not recognizing all ul tokens #445

Merged
merged 2 commits into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 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 @@ -99,9 +101,9 @@ class _ParchmentMarkdownDecoder extends Converter<String, ParchmentDocument> {
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;
}

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
Loading