Skip to content

Commit

Permalink
Support strike through in markdown codec
Browse files Browse the repository at this point in the history
  • Loading branch information
amantoux committed Nov 23, 2023
1 parent 56ee00f commit 3b15d19
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
15 changes: 15 additions & 0 deletions packages/parchment/lib/src/codecs/markdown.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class _ParchmentMarkdownDecoder extends Converter<String, ParchmentDocument> {
r'((\*{2}|_{2})([*_])(?<bold_italic_text>.*?[^ \7\6])\7\6)|'
// italic or bold
r'(((\*{1,2})|(_{1,2}))(?<bold_or_italic_text>.*?[^ \10])\10)|'
// strike through
r'(~~(?<strike_through_text>.+?)~~)|'
// inline code
r'(`(?<inline_code_text>.+?)`)',
);
Expand Down Expand Up @@ -247,6 +249,9 @@ class _ParchmentMarkdownDecoder extends Converter<String, ParchmentDocument> {
} else if (match.namedGroup('bold_or_italic_text') != null) {
text = match.namedGroup('bold_or_italic_text')!;
styleTag = match.group(10)!;
} else if (match.namedGroup('strike_through_text') != null) {
text = match.namedGroup('strike_through_text')!;
styleTag = '~~';
} else {
assert(match.namedGroup('inline_code_text') != null);
text = match.namedGroup('inline_code_text')!;
Expand All @@ -268,6 +273,7 @@ class _ParchmentMarkdownDecoder extends Converter<String, ParchmentDocument> {
ParchmentStyle _fromStyleTag(String styleTag) {
assert(
(styleTag == '`') |
(styleTag == '~~') |
(styleTag == '_') |
(styleTag == '*') |
(styleTag == '__') |
Expand All @@ -283,6 +289,9 @@ class _ParchmentMarkdownDecoder extends Converter<String, ParchmentDocument> {
if (styleTag == '`') {
return ParchmentStyle().put(ParchmentAttribute.inlineCode);
}
if (styleTag == '~~') {
return ParchmentStyle().put(ParchmentAttribute.strikethrough);
}
if (styleTag.length == 3) {
return ParchmentStyle()
.putAll([ParchmentAttribute.bold, ParchmentAttribute.italic]);
Expand Down Expand Up @@ -439,6 +448,8 @@ class _ParchmentMarkdownEncoder extends Converter<ParchmentDocument, String> {
_writeItalicTag(buffer);
} else if (attribute == ParchmentAttribute.inlineCode) {
_writeInlineCodeTag(buffer);
} else if (attribute == ParchmentAttribute.strikethrough) {
_writeStrikeThoughTag(buffer);
} else if (attribute?.key == ParchmentAttribute.link.key) {
_writeLinkTag(buffer, attribute as ParchmentAttribute<String>,
close: close);
Expand All @@ -464,6 +475,10 @@ class _ParchmentMarkdownEncoder extends Converter<ParchmentDocument, String> {
buffer.write('`');
}

void _writeStrikeThoughTag(StringBuffer buffer) {
buffer.write('~~');
}

void _writeLinkTag(StringBuffer buffer, ParchmentAttribute<String> link,
{bool close = false}) {
if (close) {
Expand Down
23 changes: 20 additions & 3 deletions packages/parchment/test/codecs/markdown_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import 'package:quill_delta/quill_delta.dart';
import 'package:test/test.dart';

void main() {
group('$ParchmentMarkdownCodec.decode', () {
group('ParchmentMarkdownCodec.decode', () {
test('should convert empty markdown to valid empty document', () {
final markdown = '';
final newParchment = ParchmentDocument();
Expand Down Expand Up @@ -133,6 +133,21 @@ void main() {
false);
});

test('strike through', () {
void runFor(String markdown, bool testEncode) {
final document = parchmentMarkdown.decode(markdown);
final delta = document.toDelta();
expect(delta.elementAt(0).data, 'strike through');
expect(delta.elementAt(0).attributes?['s'], true);
if (testEncode) {
final andBack = parchmentMarkdown.encode(document);
expect(andBack, markdown);
}
}

runFor('~~strike through~~\n\n', true);
});

test('intersecting inline styles', () {
final markdown = 'This **house _is a_ circus**\n\n';
final document = parchmentMarkdown.decode(markdown);
Expand Down Expand Up @@ -390,15 +405,15 @@ void main() {
});
});

group('$ParchmentMarkdownCodec.encode', () {
group('ParchmentMarkdownCodec.encode', () {
test('split adjacent paragraphs', () {
final delta = Delta()..insert('First line\nSecond line\n');
final result =
parchmentMarkdown.encode(ParchmentDocument.fromDelta(delta));
expect(result, 'First line\n\nSecond line\n\n');
});

test('bold italic', () {
test('bold italic strike though', () {
void runFor(ParchmentAttribute<bool> attribute, String expected) {
final delta = Delta()
..insert('This ')
Expand All @@ -414,6 +429,8 @@ void main() {

runFor(ParchmentAttribute.bold, 'This **house** is a **circus**\n\n');
runFor(ParchmentAttribute.italic, 'This _house_ is a _circus_\n\n');
runFor(ParchmentAttribute.strikethrough,
'This ~~house~~ is a ~~circus~~\n\n');
});

test('intersecting inline styles', () {
Expand Down

0 comments on commit 3b15d19

Please sign in to comment.