Skip to content

Commit f669a1a

Browse files
authored
Merge pull request #1662 from hydephp/strip-markdown-from-automatic-blog-post-descriptions
Strip Markdown from automatic blog post descriptions
2 parents bf136ff + 42736e5 commit f669a1a

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

RELEASE_NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ This serves two purposes:
2929
### Fixed
3030
- 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
3131
- Fixed extra newlines being written to console for failing build tasks in https://github.com/hydephp/develop/pull/1661
32+
- 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)
3233
- Realtime Compiler: Fixed responsive dashboard table issue in https://github.com/hydephp/develop/pull/1595
3334

3435
### Security

packages/framework/src/Framework/Factories/BlogPostDataFactory.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Hyde\Framework\Factories;
66

77
use Hyde\Framework\Factories\Concerns\CoreDataObject;
8+
use Hyde\Framework\Actions\ConvertsMarkdownToPlainText;
89
use Hyde\Framework\Features\Blogging\Models\FeaturedImage;
910
use Hyde\Framework\Features\Blogging\Models\PostAuthor;
1011
use Hyde\Markdown\Contracts\FrontMatter\BlogPostSchema;
@@ -72,7 +73,7 @@ public function toArray(): array
7273

7374
protected function makeDescription(): string
7475
{
75-
return $this->getMatter('description') ?? $this->getTruncatedMarkdown($this->markdown->body());
76+
return $this->getMatter('description') ?? $this->makeDescriptionFromMarkdownBody();
7677
}
7778

7879
protected function makeCategory(): ?string
@@ -107,6 +108,11 @@ protected function makeImage(): ?FeaturedImage
107108
return null;
108109
}
109110

111+
private function makeDescriptionFromMarkdownBody(): string
112+
{
113+
return $this->getTruncatedMarkdown($this->stripMarkdownFromBody($this->markdown->body()));
114+
}
115+
110116
private function getTruncatedMarkdown(string $markdown): string
111117
{
112118
if (strlen($markdown) >= 128) {
@@ -116,6 +122,11 @@ private function getTruncatedMarkdown(string $markdown): string
116122
return $markdown;
117123
}
118124

125+
private function stripMarkdownFromBody(string $body): string
126+
{
127+
return (new ConvertsMarkdownToPlainText($body))->execute();
128+
}
129+
119130
protected function getMatter(string $key): string|null|array
120131
{
121132
return $this->matter->get($key);

packages/framework/tests/Unit/Pages/MarkdownPostHelpersTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,10 @@ public function testDynamicDescriptionIsTruncatedWhenLongerThan128Characters()
4949
$post = MarkdownPost::make('foo-bar', [], str_repeat('a', 128));
5050
$this->assertEquals(str_repeat('a', 125).'...', $post->description);
5151
}
52+
53+
public function testDynamicDescriptionStripsMarkdown()
54+
{
55+
$post = MarkdownPost::make('foo-bar', [], '## This is a **bold** description with [a link](https://example.com) and <code>more</code>');
56+
$this->assertEquals('This is a bold description with a link and more', $post->description);
57+
}
5258
}

0 commit comments

Comments
 (0)