Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/Plugins/RemoveInnerParagraphs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace JackSleight\StatamicBardMutator\Plugins;

class RemoveInnerParagraphs extends Plugin
{
protected array $options = [
'types' => ['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;
}
}
30 changes: 30 additions & 0 deletions src/Plugins/RemoveOuterParagraphs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace JackSleight\StatamicBardMutator\Plugins;

class RemoveOuterParagraphs extends Plugin
{
protected array $options = [
'types' => ['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;
}
}
87 changes: 87 additions & 0 deletions tests/Plugins/RemoveInnerParagraphsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

use JackSleight\StatamicBardMutator\Facades\Mutator;
use JackSleight\StatamicBardMutator\Plugins\RemoveInnerParagraphs;

uses(Tests\TestCase::class);

it('removes inner paragraphs', function () {
Mutator::plugin(RemoveInnerParagraphs::class);
$value = $this->getTestValue([[
'type' => 'listItem',
'content' => [[
'type' => 'paragraph',
'content' => [[
'type' => 'text',
'text' => 'Test',
]],
]],
]]);
expect($this->renderTestValue($value))->toEqual('<li>Test</li>');
});

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('<li><h1>Test</h1></li>');
});

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('<li><p>Test</p><p>Test</p></li>');
});

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('<li><p>Test</p></li><td>Test</td>');
});
78 changes: 78 additions & 0 deletions tests/Plugins/RemoveOuterParagraphsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

use JackSleight\StatamicBardMutator\Facades\Mutator;
use JackSleight\StatamicBardMutator\Plugins\RemoveOuterParagraphs;

uses(Tests\TestCase::class);

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

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('<h1><img src="example.jpg"></h1>');
});

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('<p><img src="example.jpg"><img src="example.jpg"></p>');
});

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('<p><img src="example.jpg"></p><table><tbody></tbody></table>');
});