diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 9d251312db1..3529f658206 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -29,6 +29,7 @@ This serves two purposes: ### Fixed - Fixed a bug where the sitemap and RSS feed generator commands did not work when the `_site/` directory was not present in https://github.com/hydephp/develop/pull/1654 - Fixed extra newlines being written to console for failing build tasks in https://github.com/hydephp/develop/pull/1661 +- Markdown formatting will now be stripped when generating an automatic blog post description when none is set in https://github.com/hydephp/develop/pull/1662 (fixes https://github.com/hydephp/develop/issues/1634) - Realtime Compiler: Fixed responsive dashboard table issue in https://github.com/hydephp/develop/pull/1595 ### Security diff --git a/packages/framework/src/Framework/Factories/BlogPostDataFactory.php b/packages/framework/src/Framework/Factories/BlogPostDataFactory.php index 8eb80a3d401..667800b0718 100644 --- a/packages/framework/src/Framework/Factories/BlogPostDataFactory.php +++ b/packages/framework/src/Framework/Factories/BlogPostDataFactory.php @@ -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; @@ -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 @@ -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) { @@ -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); diff --git a/packages/framework/tests/Unit/Pages/MarkdownPostHelpersTest.php b/packages/framework/tests/Unit/Pages/MarkdownPostHelpersTest.php index 7e93b577afa..721c575eb86 100644 --- a/packages/framework/tests/Unit/Pages/MarkdownPostHelpersTest.php +++ b/packages/framework/tests/Unit/Pages/MarkdownPostHelpersTest.php @@ -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 more'); + $this->assertEquals('This is a bold description with a link and more', $post->description); + } }