Skip to content

Commit 56867b3

Browse files
feat: Improve no-blocks handling
1 parent ab1c526 commit 56867b3

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/Exception/NoBlockException.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace SyntaxPhoenix\EJSParserBundle\Exception;
4+
5+
use Exception;
6+
7+
class NoBlockException extends Exception
8+
{
9+
}

src/Parser/EditorjsParser.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Exception;
77
use DOMDocument;
88
use Masterminds\HTML5;
9+
use SyntaxPhoenix\EJSParserBundle\Exception\NoBlockException;
910
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\RawParser;
1011
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\CodeParser;
1112
use SyntaxPhoenix\EJSParserBundle\Parser\Extension\LinkParser;
@@ -69,9 +70,13 @@ public function setData(object $data): void
6970

7071
public function toHtml(): string
7172
{
72-
$this->render();
73-
74-
return $this->dom->saveHTML();
73+
try {
74+
$this->render();
75+
76+
return $this->dom->saveHTML();
77+
} catch (NoBlockException $exception) {
78+
return '';
79+
}
7580
}
7681

7782
private function hasBlocks()
@@ -85,7 +90,7 @@ private function hasBlocks()
8590
private function render(): void
8691
{
8792
if (!$this->hasBlocks()) {
88-
throw new Exception('No Blocks to parse !');
93+
throw new NoBlockException('No Blocks to parse !');
8994
}
9095
foreach ($this->data->blocks as $block) {
9196
if (array_key_exists($block->type, $this->parser)) {

0 commit comments

Comments
 (0)