Skip to content

Commit

Permalink
Merge pull request #485 from awcodes/fix/nullable-converter
Browse files Browse the repository at this point in the history
Fix: Make converter nullable
  • Loading branch information
awcodes authored Oct 15, 2024
2 parents d49f179 + a9269ed commit 2fac924
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Extensions/Extensions/TextAlign.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function addGlobalAttributes(): array
'default' => $this->options['defaultAlignment'],
'parseHTML' => fn ($DOMNode) => InlineStyle::getAttribute($DOMNode, 'text-align') ?? $this->options['defaultAlignment'],
'renderHTML' => function ($attributes) {
if (property_exists($attributes, 'style') && str_contains($attributes->style, 'text-align')) {
if (property_exists($attributes, 'style') && str_contains($attributes->style ?? '', 'text-align')) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Extensions/Marks/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function addAttributes(): array
return null;
},
'renderHTML' => function ($attributes) {
if (! property_exists($attributes, 'button_theme') || strlen($attributes->button_theme) === 0) {
if (! property_exists($attributes, 'button_theme') || strlen($attributes->button_theme ?? '') === 0) {
return null;
}

Expand Down
24 changes: 20 additions & 4 deletions src/TiptapConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,12 @@ public function mergeTagsMap(array $mergeTagsMap): static
return $this;
}

public function asHTML(string | array $content, bool $toc = false, int $maxDepth = 3): string
public function asHTML(string | array | null $content, bool $toc = false, int $maxDepth = 3): string
{
if (! $content) {
return '';
}

$editor = $this->getEditor()->setContent($content);

if ($toc) {
Expand All @@ -118,8 +122,12 @@ public function asHTML(string | array $content, bool $toc = false, int $maxDepth
return str_replace('</code></pre></code></pre>', '</code></pre>', $editor->getHTML());
}

public function asJSON(string | array $content, bool $decoded = false, bool $toc = false, int $maxDepth = 3): string | array
public function asJSON(string | array | null $content, bool $decoded = false, bool $toc = false, int $maxDepth = 3): string | array
{
if (! $content) {
return '';
}

$editor = $this->getEditor()->setContent($content);

if ($toc) {
Expand All @@ -133,8 +141,12 @@ public function asJSON(string | array $content, bool $decoded = false, bool $toc
return $decoded ? json_decode($editor->getJSON(), true) : $editor->getJSON();
}

public function asText(string | array $content): string
public function asText(string | array | null $content): string
{
if (! $content) {
return '';
}

$editor = $this->getEditor()->setContent($content);

if (filled($this->mergeTagsMap)) {
Expand All @@ -144,8 +156,12 @@ public function asText(string | array $content): string
return $editor->getText();
}

public function asTOC(string | array $content, int $maxDepth = 3, bool $array = false): string | array
public function asTOC(string | array | null $content, int $maxDepth = 3, bool $array = false): string | array
{
if (! $content) {
return '';
}

if (is_string($content)) {
$content = $this->asJSON($content, decoded: true);
}
Expand Down

0 comments on commit 2fac924

Please sign in to comment.