Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Feb 28, 2025
1 parent 9d6752e commit 96864f7
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 33 deletions.
6 changes: 3 additions & 3 deletions src/blocktransformers/Blockquote.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public function render(array $data, Entry $entry): string
$content = $this->command->renderBlocks($data['innerBlocks'], $entry);

// is there a <cite> tag in innerHTML?
$nodes = (new Crawler($data['innerHTML']))->filter('cite');
if ($nodes->count()) {
$content .= $nodes->outerHtml();
$node = (new Crawler($data['innerHTML']))->filter('cite');
if ($node->count()) {
$content .= $node->outerHtml();
}

return Html::tag('blockquote', $content);
Expand Down
8 changes: 4 additions & 4 deletions src/blocktransformers/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ public static function blockName(): string

public function render(array $data, Entry $entry): string
{
$nodes = (new Crawler($data['innerHTML']))->filter('a');
if (!$nodes->count()) {
$node = (new Crawler($data['innerHTML']))->filter('a');
if (!$node->count()) {
return '';
}
$label = $nodes->html();
$url = $nodes->first()->attr('href');
$label = $node->text();
$url = $node->attr('href');

return $this->command->createNestedEntry($entry, function(Entry $nestedEntry) use ($label, $url) {
$nestedEntry->setTypeId(ButtonEntryType::get()->id);
Expand Down
6 changes: 3 additions & 3 deletions src/blocktransformers/Code.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ public static function blockName(): string
public function render(array $data, Entry $entry): string
{
// `<pre class="wp-block-code"><code>` → `<pre><code>`
$nodes = (new Crawler($data['innerHTML']))->filter('code');
if (!$nodes->count()) {
$node = (new Crawler($data['innerHTML']))->filter('code');
if (!$node->count()) {
return '';
}
$code = $nodes->html();
$code = $node->html();
return Html::beginTag('pre') .
Html::tag('code', $code, [
'class' => sprintf('language-%s', $this->detectLanguage($code)),
Expand Down
6 changes: 3 additions & 3 deletions src/blocktransformers/Cover.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public function render(array $data, Entry $entry): string
}

if (isset($data['innerBlocks'][0]) && $data['innerBlocks'][0]['blockName'] === 'core/paragraph') {
$nodes = (new Crawler($data['innerBlocks'][0]['innerHTML']))->filter('p');
if ($nodes->count()) {
$entry->setFieldValue('coverText', $nodes->html());
$node = (new Crawler($data['innerBlocks'][0]['innerHTML']))->filter('p');
if ($node->count()) {
$entry->setFieldValue('coverText', $node->html());
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/blocktransformers/DsbDetails.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ public function render(array $data, Entry $entry): string
return $this->command->createNestedEntry($entry, function(Entry $nestedEntry) use ($data) {
$nestedEntry->setTypeId(DetailsEntryType::get()->id);

$summaryNodes = (new Crawler($data['innerHTML']))->filter('summary');
if ($summaryNodes->count()) {
$nestedEntry->setFieldValue(Summary::get()->handle, $summaryNodes->html());
$node = (new Crawler($data['innerHTML']))->filter('summary');
if ($node->count()) {
$nestedEntry->setFieldValue(Summary::get()->handle, $node->html());
}

// save it so we get an ID, before parsing the nested blocks
Expand Down
2 changes: 1 addition & 1 deletion src/blocktransformers/GBHeadline.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ public function render(array $data, Entry $entry): string
$tag = 'p';
}

return HtmlHelper::tag($tag, $node->text());
return HtmlHelper::tag($tag, $node->html());
}
}
8 changes: 4 additions & 4 deletions src/blocktransformers/GBImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ public function render(array $data, Entry $entry): string
return '';
}

$img = (new Crawler($data['innerHTML']))->filter('img');
$src = $img->attr('src');
$node = (new Crawler($data['innerHTML']))->filter('img');
$src = $node->attr('src');

try {
if ($src) {
$assetId = $this->command->import(Media::SLUG, [
'id' => $id,
'source_url' => $src,
'title' => $img->attr('title'),
'alt_text' => $img->attr('alt'),
'title' => $node->attr('title'),
'alt_text' => $node->attr('alt'),
]);
} else {
$assetId = $this->command->import(Media::SLUG, $id);
Expand Down
6 changes: 3 additions & 3 deletions src/blocktransformers/Heading.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ public function render(array $data, Entry $entry): string
{
// render a new heading tag without `class="wp-block-heading"`
// (can't rely on `$data['attrs']['level']` unfortunately)
$nodes = (new Crawler($data['innerHTML']))->filter('h1,h2,h3,h4,h5,h6');
if (!$nodes->count()) {
$node = (new Crawler($data['innerHTML']))->filter('h1,h2,h3,h4,h5,h6');
if (!$node->count()) {
return '';
}
return Html::tag($nodes->first()->nodeName(), $nodes->html());
return Html::tag($node->nodeName(), $node->html());
}
}
6 changes: 3 additions & 3 deletions src/blocktransformers/Preformatted.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ public static function blockName(): string
public function render(array $data, Entry $entry): string
{
// `<pre class="wp-block-preformatted">` → `<pre><code>`
$nodes = (new Crawler($data['innerHTML']))->filter('pre');
if (!$nodes->count()) {
$node = (new Crawler($data['innerHTML']))->filter('pre');
if (!$node->count()) {
return '';
}
return Html::beginTag('pre') .
Html::tag('code', $nodes->html(), ['class' => 'language-plaintext']) .
Html::tag('code', $node->html(), ['class' => 'language-plaintext']) .
Html::endTag('pre');
}
}
6 changes: 3 additions & 3 deletions src/blocktransformers/PullQuote.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ public static function blockName(): string
public function render(array $data, Entry $entry): string
{
$crawler = new Crawler($data['innerHTML']);
$paragraphNodes = $crawler->filter('p');
$pNodes = $crawler->filter('p');
$citeNodes = $crawler->filter('cite');

if (!$paragraphNodes->count() && !$citeNodes->count()) {
if (!$pNodes->count() && !$citeNodes->count()) {
return '';
}

$html = '';
$paragraphNodes->each(function(Crawler $node) use (&$html) {
$pNodes->each(function(Crawler $node) use (&$html) {
$html .= $node->outerHtml();
});

Expand Down
6 changes: 3 additions & 3 deletions src/blocktransformers/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public static function blockName(): string
public function render(array $data, Entry $entry): string
{
// render a new <figure> with `class="table"`
$nodes = (new Crawler($data['innerHTML']))->filter('table');
if (!$nodes->count()) {
$node = (new Crawler($data['innerHTML']))->filter('table');
if (!$node->count()) {
return '';
}
return Html::tag('figure', $nodes->outerHtml(), ['class' => 'table']);
return Html::tag('figure', $node->outerHtml(), ['class' => 'table']);
}
}

0 comments on commit 96864f7

Please sign in to comment.