Skip to content

Commit

Permalink
Add checkbox support to markdown encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
Amir-P committed May 8, 2024
1 parent 8ab66b1 commit 1a69dff
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/parchment/lib/src/codecs/markdown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,16 @@ class _ParchmentMarkdownEncoder extends Converter<ParchmentDocument, String> {
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) {
Expand Down Expand Up @@ -454,6 +464,8 @@ class _ParchmentMarkdownEncoder extends Converter<ParchmentDocument, String> {
} else if (attribute?.key == ParchmentAttribute.block.key) {
_writeBlockTag(buffer, attribute as ParchmentAttribute<String>,
close: close);
} else if (attribute?.key == ParchmentAttribute.checked.key) {
// no-op
} else {
throw ArgumentError('Cannot handle $attribute');
}
Expand Down Expand Up @@ -501,7 +513,9 @@ class _ParchmentMarkdownEncoder extends Converter<ParchmentDocument, String> {
if (close) return; // no close tag needed for simple blocks.

final tag = simpleBlocks[block];
buffer.write(tag);
if (tag != null) {
buffer.write(tag);
}
}
}
}
19 changes: 19 additions & 0 deletions packages/parchment/test/codecs/markdown_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> attribute, String source,
String expected) {
Expand Down

0 comments on commit 1a69dff

Please sign in to comment.