diff --git a/packages/parchment/lib/src/codecs/markdown.dart b/packages/parchment/lib/src/codecs/markdown.dart index b3eca6aa..fd890af8 100644 --- a/packages/parchment/lib/src/codecs/markdown.dart +++ b/packages/parchment/lib/src/codecs/markdown.dart @@ -411,6 +411,16 @@ class _ParchmentMarkdownEncoder extends Converter { for (final lineNode in node.children) { if (node.style.containsSame(ParchmentAttribute.ol)) { lineBuffer.write(currentItemOrder); + } else if (node.style.containsSame(ParchmentAttribute.cl)) { + lineBuffer.write('- ['); + if ((lineNode as LineNode) + .style + .contains(ParchmentAttribute.checked)) { + lineBuffer.write('X'); + } else { + lineBuffer.write(' '); + } + lineBuffer.write('] '); } handleLine(lineNode as LineNode); if (!lineNode.isLast) { @@ -454,6 +464,8 @@ class _ParchmentMarkdownEncoder extends Converter { } else if (attribute?.key == ParchmentAttribute.block.key) { _writeBlockTag(buffer, attribute as ParchmentAttribute, close: close); + } else if (attribute?.key == ParchmentAttribute.checked.key) { + // no-op } else { throw ArgumentError('Cannot handle $attribute'); } @@ -501,7 +513,9 @@ class _ParchmentMarkdownEncoder extends Converter { if (close) return; // no close tag needed for simple blocks. final tag = simpleBlocks[block]; - buffer.write(tag); + if (tag != null) { + buffer.write(tag); + } } } } diff --git a/packages/parchment/test/codecs/markdown_test.dart b/packages/parchment/test/codecs/markdown_test.dart index 97c09f82..22a3bf4b 100644 --- a/packages/parchment/test/codecs/markdown_test.dart +++ b/packages/parchment/test/codecs/markdown_test.dart @@ -543,6 +543,25 @@ void main() { expect(result, expected); }); + test('cl', () { + final delta = Delta() + ..insert('Hello') + ..insert('\n', ParchmentAttribute.cl.toJson()) + ..insert( + 'This is a', + ) + ..insert('\n', { + ...ParchmentAttribute.cl.toJson(), + ...ParchmentAttribute.checked.toJson(), + }) + ..insert('Checklist') + ..insert('\n', ParchmentAttribute.cl.toJson()); + final result = + parchmentMarkdown.encode(ParchmentDocument.fromDelta(delta)); + final expected = '- [ ] Hello\n- [X] This is a\n- [ ] Checklist\n\n'; + expect(result, expected); + }); + test('multiline blocks', () { void runFor(ParchmentAttribute attribute, String source, String expected) {