Skip to content

Commit

Permalink
Merge branch 'master' into 2.x-dev
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Apr 14, 2024
2 parents dbd5317 + f669a1a commit 0cd701e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Hyde\Framework\Factories;

use Hyde\Framework\Factories\Concerns\CoreDataObject;
use Hyde\Framework\Actions\ConvertsMarkdownToPlainText;
use Hyde\Framework\Features\Blogging\Models\FeaturedImage;
use Hyde\Framework\Features\Blogging\Models\PostAuthor;
use Hyde\Markdown\Contracts\FrontMatter\BlogPostSchema;
Expand Down Expand Up @@ -72,7 +73,7 @@ public function toArray(): array

protected function makeDescription(): string
{
return $this->getMatter('description') ?? $this->getTruncatedMarkdown($this->markdown->body());
return $this->getMatter('description') ?? $this->makeDescriptionFromMarkdownBody();
}

protected function makeCategory(): ?string
Expand Down Expand Up @@ -107,6 +108,11 @@ protected function makeImage(): ?FeaturedImage
return null;
}

private function makeDescriptionFromMarkdownBody(): string
{
return $this->getTruncatedMarkdown($this->stripMarkdownFromBody($this->markdown->body()));
}

private function getTruncatedMarkdown(string $markdown): string
{
if (strlen($markdown) >= 128) {
Expand All @@ -116,6 +122,11 @@ private function getTruncatedMarkdown(string $markdown): string
return $markdown;
}

private function stripMarkdownFromBody(string $body): string
{
return (new ConvertsMarkdownToPlainText($body))->execute();
}

protected function getMatter(string $key): string|null|array
{
return $this->matter->get($key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,10 @@ public function testDynamicDescriptionIsTruncatedWhenLongerThan128Characters()
$post = MarkdownPost::make('foo-bar', [], str_repeat('a', 128));
$this->assertEquals(str_repeat('a', 125).'...', $post->description);
}

public function testDynamicDescriptionStripsMarkdown()
{
$post = MarkdownPost::make('foo-bar', [], '## This is a **bold** description with [a link](https://example.com) and <code>more</code>');
$this->assertEquals('This is a bold description with a link and more', $post->description);
}
}

0 comments on commit 0cd701e

Please sign in to comment.