From 7ddce8e51bd2716a774408e5052b6b66a8573641 Mon Sep 17 00:00:00 2001 From: Alan Mantoux Date: Thu, 23 Nov 2023 09:31:24 +0100 Subject: [PATCH] WIP --- .../parchment/lib/src/codecs/markdown.dart | 11 +++++++++++ .../parchment/test/codecs/markdown_test.dart | 18 +++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/packages/parchment/lib/src/codecs/markdown.dart b/packages/parchment/lib/src/codecs/markdown.dart index f15e534e..20f6e484 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) { + _writeInlineCodeTag(buffer); } else if (attribute?.key == ParchmentAttribute.link.key) { _writeLinkTag(buffer, attribute as ParchmentAttribute, close: close); diff --git a/packages/parchment/test/codecs/markdown_test.dart b/packages/parchment/test/codecs/markdown_test.dart index fa6015de..dd56b751 100644 --- a/packages/parchment/test/codecs/markdown_test.dart +++ b/packages/parchment/test/codecs/markdown_test.dart @@ -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); @@ -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,7 @@ 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', () {