From b396b8c6b9b1491654c3431c5469218c83938e62 Mon Sep 17 00:00:00 2001 From: Jack Sleight Date: Fri, 30 Aug 2024 17:11:33 +0100 Subject: [PATCH] WIP --- src/Plugins/RemoveInnerParagraphs.php | 32 ++++++++ src/Plugins/RemoveOuterParagraphs.php | 30 +++++++ tests/Plugins/RemoveInnerParagraphsTest.php | 87 +++++++++++++++++++++ tests/Plugins/RemoveOuterParagraphsTest.php | 78 ++++++++++++++++++ 4 files changed, 227 insertions(+) create mode 100644 src/Plugins/RemoveInnerParagraphs.php create mode 100644 src/Plugins/RemoveOuterParagraphs.php create mode 100644 tests/Plugins/RemoveInnerParagraphsTest.php create mode 100644 tests/Plugins/RemoveOuterParagraphsTest.php diff --git a/src/Plugins/RemoveInnerParagraphs.php b/src/Plugins/RemoveInnerParagraphs.php new file mode 100644 index 0000000..cb7d265 --- /dev/null +++ b/src/Plugins/RemoveInnerParagraphs.php @@ -0,0 +1,32 @@ + ['listItem', 'tableCell'], + ]; + + public function types(): array + { + return $this->options['types']; + } + + public function process(object $item, object $info): void + { + $content = $item->content ?? []; + + if (count($content) !== 1) { + return; + } + + $inner = $content[0]; + + if ($inner->type !== 'paragraph') { + return; + } + + $item->content = $inner->content; + } +} diff --git a/src/Plugins/RemoveOuterParagraphs.php b/src/Plugins/RemoveOuterParagraphs.php new file mode 100644 index 0000000..cf5b029 --- /dev/null +++ b/src/Plugins/RemoveOuterParagraphs.php @@ -0,0 +1,30 @@ + ['image'], + ]; + + public function types(): array + { + return $this->options['types']; + } + + public function process(object $item, object $info): void + { + $outer = $info->parent; + + if ($outer->type !== 'paragraph') { + return; + } + + if (count($outer->content) !== 1) { + return; + } + + $outer->parent->item->content[$outer->index] = $item; + } +} diff --git a/tests/Plugins/RemoveInnerParagraphsTest.php b/tests/Plugins/RemoveInnerParagraphsTest.php new file mode 100644 index 0000000..d428b39 --- /dev/null +++ b/tests/Plugins/RemoveInnerParagraphsTest.php @@ -0,0 +1,87 @@ +getTestValue([[ + 'type' => 'listItem', + 'content' => [[ + 'type' => 'paragraph', + 'content' => [[ + 'type' => 'text', + 'text' => 'Test', + ]], + ]], + ]]); + expect($this->renderTestValue($value))->toEqual('
  • Test
  • '); +}); + +it('only removes inner paragraphs', function () { + Mutator::plugin(RemoveInnerParagraphs::class); + $value = $this->getTestValue([[ + 'type' => 'listItem', + 'content' => [[ + 'type' => 'heading', + 'attrs' => [ + 'level' => 1, + ], + 'content' => [[ + 'type' => 'text', + 'text' => 'Test', + ]], + ]], + ]]); + expect($this->renderTestValue($value))->toEqual('
  • Test

  • '); +}); + +it('only removes inner paragraphs when there are no other siblings', function () { + Mutator::plugin(RemoveInnerParagraphs::class); + $value = $this->getTestValue([[ + 'type' => 'listItem', + 'content' => [[ + 'type' => 'paragraph', + 'content' => [[ + 'type' => 'text', + 'text' => 'Test', + ]], + ], [ + 'type' => 'paragraph', + 'content' => [[ + 'type' => 'text', + 'text' => 'Test', + ]], + ]], + ]]); + expect($this->renderTestValue($value))->toEqual('
  • Test

    Test

  • '); +}); + +it('removes inner paragraphs from configured types', function () { + Mutator::plugin(RemoveInnerParagraphs::class) + ->options([ + 'types' => ['tableCell'], + ]); + $value = $this->getTestValue([[ + 'type' => 'listItem', + 'content' => [[ + 'type' => 'paragraph', + 'content' => [[ + 'type' => 'text', + 'text' => 'Test', + ]], + ]], + ], [ + 'type' => 'tableCell', + 'content' => [[ + 'type' => 'paragraph', + 'content' => [[ + 'type' => 'text', + 'text' => 'Test', + ]], + ]], + ]]); + expect($this->renderTestValue($value))->toEqual('
  • Test

  • Test'); +}); diff --git a/tests/Plugins/RemoveOuterParagraphsTest.php b/tests/Plugins/RemoveOuterParagraphsTest.php new file mode 100644 index 0000000..c401273 --- /dev/null +++ b/tests/Plugins/RemoveOuterParagraphsTest.php @@ -0,0 +1,78 @@ +getTestValue([[ + 'type' => 'paragraph', + 'content' => [[ + 'type' => 'image', + 'attrs' => [ + 'src' => 'example.jpg', + ], + ]], + ]]); + expect($this->renderTestValue($value))->toEqual(''); +}); + +it('only removes outer paragraphs', function () { + Mutator::plugin(RemoveOuterParagraphs::class); + $value = $this->getTestValue([[ + 'type' => 'heading', + 'attrs' => [ + 'level' => 1, + ], + 'content' => [[ + 'type' => 'image', + 'attrs' => [ + 'src' => 'example.jpg', + ], + ]], + ]]); + expect($this->renderTestValue($value))->toEqual('

    '); +}); + +it('only removes outer paragraphs when there are no other siblings', function () { + Mutator::plugin(RemoveOuterParagraphs::class); + $value = $this->getTestValue([[ + 'type' => 'paragraph', + 'content' => [[ + 'type' => 'image', + 'attrs' => [ + 'src' => 'example.jpg', + ], + ], [ + 'type' => 'image', + 'attrs' => [ + 'src' => 'example.jpg', + ], + ]], + ]]); + expect($this->renderTestValue($value))->toEqual('

    '); +}); + +it('removes outer paragraphs from configured types', function () { + Mutator::plugin(RemoveOuterParagraphs::class) + ->options([ + 'types' => ['table'], + ]); + $value = $this->getTestValue([[ + 'type' => 'paragraph', + 'content' => [[ + 'type' => 'image', + 'attrs' => [ + 'src' => 'example.jpg', + ], + ]], + ], [ + 'type' => 'paragraph', + 'content' => [[ + 'type' => 'table', + ]], + ]]); + expect($this->renderTestValue($value))->toEqual('

    '); +});