From cd88559ede26d7f5f1cb1f50dfba551b3463f586 Mon Sep 17 00:00:00 2001 From: Alan Mantoux Date: Sat, 25 Nov 2023 06:35:50 +0100 Subject: [PATCH] Add strike through to Markdown codec (#177) Support strike through in markdown codec --- .../parchment/lib/src/codecs/markdown.dart | 15 ++++++++++++ .../parchment/test/codecs/markdown_test.dart | 23 ++++++++++++++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/packages/parchment/lib/src/codecs/markdown.dart b/packages/parchment/lib/src/codecs/markdown.dart index f15e534e..e651295e 100644 --- a/packages/parchment/lib/src/codecs/markdown.dart +++ b/packages/parchment/lib/src/codecs/markdown.dart @@ -33,6 +33,8 @@ class _ParchmentMarkdownDecoder extends Converter { r'((\*{2}|_{2})([*_])(?.*?[^ \7\6])\7\6)|' // italic or bold r'(((\*{1,2})|(_{1,2}))(?.*?[^ \10])\10)|' + // strike through + r'(~~(?.+?)~~)|' // inline code r'(`(?.+?)`)', ); @@ -247,6 +249,9 @@ class _ParchmentMarkdownDecoder extends Converter { } 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')!; @@ -268,6 +273,7 @@ class _ParchmentMarkdownDecoder extends Converter { ParchmentStyle _fromStyleTag(String styleTag) { assert( (styleTag == '`') | + (styleTag == '~~') | (styleTag == '_') | (styleTag == '*') | (styleTag == '__') | @@ -283,6 +289,9 @@ class _ParchmentMarkdownDecoder extends Converter { 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]); @@ -439,6 +448,8 @@ class _ParchmentMarkdownEncoder extends Converter { _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, close: close); @@ -464,6 +475,10 @@ class _ParchmentMarkdownEncoder extends Converter { buffer.write('`'); } + void _writeStrikeThoughTag(StringBuffer buffer) { + buffer.write('~~'); + } + void _writeLinkTag(StringBuffer buffer, ParchmentAttribute link, {bool close = false}) { if (close) { diff --git a/packages/parchment/test/codecs/markdown_test.dart b/packages/parchment/test/codecs/markdown_test.dart index fa6015de..60a23555 100644 --- a/packages/parchment/test/codecs/markdown_test.dart +++ b/packages/parchment/test/codecs/markdown_test.dart @@ -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(); @@ -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); @@ -390,7 +405,7 @@ void main() { }); }); - group('$ParchmentMarkdownCodec.encode', () { + group('ParchmentMarkdownCodec.encode', () { test('split adjacent paragraphs', () { final delta = Delta()..insert('First line\nSecond line\n'); final result = @@ -398,7 +413,7 @@ void main() { expect(result, 'First line\n\nSecond line\n\n'); }); - test('bold italic', () { + test('bold italic strike though', () { void runFor(ParchmentAttribute attribute, String expected) { final delta = Delta() ..insert('This ') @@ -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', () {