diff --git a/src/Framework/Features/Metadata/PageMetadataBag.php b/src/Framework/Features/Metadata/PageMetadataBag.php index efada5ec..f150adcb 100644 --- a/src/Framework/Features/Metadata/PageMetadataBag.php +++ b/src/Framework/Features/Metadata/PageMetadataBag.php @@ -34,6 +34,11 @@ protected function addDynamicPageMetadata(HydePage $page): void $this->add(Meta::link('canonical', $page->getCanonicalUrl())); } + if ($page->has('description')) { + $this->add(Meta::name('description', $page->data('description'))); + $this->add(Meta::property('description', $page->data('description'))); + } + if ($page->has('title')) { $this->add(Meta::name('twitter:title', $page->title())); $this->add(Meta::property('title', $page->title())); @@ -46,7 +51,6 @@ protected function addDynamicPageMetadata(HydePage $page): void protected function addMetadataForMarkdownPost(MarkdownPost $page): void { - $this->addPostMetadataIfExists($page, 'description'); $this->addPostMetadataIfExists($page, 'author'); $this->addPostMetadataIfExists($page, 'category', 'keywords'); diff --git a/src/Markdown/Contracts/FrontMatter/PageSchema.php b/src/Markdown/Contracts/FrontMatter/PageSchema.php index 5700b57f..2875b10d 100644 --- a/src/Markdown/Contracts/FrontMatter/PageSchema.php +++ b/src/Markdown/Contracts/FrontMatter/PageSchema.php @@ -13,7 +13,8 @@ interface PageSchema extends FrontMatterSchema { public const PAGE_SCHEMA = [ 'title' => 'string', - 'canonicalUrl' => 'string', // While not present in the page data as a property, it is used for the accessor method, which reads this value from the front matter. + 'description' => 'string', // For values. It is used by the automatic page metadata generator, which reads this value from the front matter. + 'canonicalUrl' => 'string', // While not present in the page data as a property, it is used by the accessor method, which reads this value from the front matter. 'navigation' => NavigationSchema::NAVIGATION_SCHEMA, ]; } diff --git a/tests/Feature/MetadataTest.php b/tests/Feature/MetadataTest.php index c5b31be5..a9273baa 100644 --- a/tests/Feature/MetadataTest.php +++ b/tests/Feature/MetadataTest.php @@ -5,6 +5,8 @@ namespace Hyde\Framework\Testing\Feature; use Hyde\Facades\Meta; +use Hyde\Pages\BladePage; +use Hyde\Pages\DocumentationPage; use Hyde\Framework\Features\Metadata\Elements\LinkElement; use Hyde\Framework\Features\Metadata\Elements\MetadataElement; use Hyde\Framework\Features\Metadata\Elements\OpenGraphElement; @@ -271,16 +273,66 @@ public function testDoesNotAddTwitterAndOpenGraphTitleWhenNoTitleIsSet() public function testAddsDescriptionWhenDescriptionIsSetInPost() { - $page = MarkdownPost::make(matter: ['description' => 'My Description']); + $page = new MarkdownPost(matter: ['description' => 'My Description']); $this->assertPageHasMetadata($page, ''); + $this->assertPageHasMetadata($page, ''); } public function testDoesNotAddDescriptionWhenDescriptionIsNotSetInPost() { $page = new MarkdownPost(); - $this->assertPageDoesNotHaveMetadata($page, ''); + $this->assertPageDoesNotHaveMetadata($page, 'assertPageDoesNotHaveMetadata($page, ' 'My Page Description']); + + $this->assertPageHasMetadata($page, ''); + $this->assertPageHasMetadata($page, ''); + } + + public function testDoesNotAddDescriptionWhenDescriptionIsNotSetInMarkdownPage() + { + $page = new MarkdownPage(); + + $this->assertPageDoesNotHaveMetadata($page, 'assertPageDoesNotHaveMetadata($page, ' 'My Page Description']); + + $this->assertPageHasMetadata($page, ''); + $this->assertPageHasMetadata($page, ''); + } + + public function testDoesNotAddDescriptionWhenDescriptionIsNotSetInBladePage() + { + $page = new BladePage(); + + $this->assertPageDoesNotHaveMetadata($page, 'assertPageDoesNotHaveMetadata($page, ' 'My Page Description']); + + $this->assertPageHasMetadata($page, ''); + $this->assertPageHasMetadata($page, ''); + } + + public function testDoesNotAddDescriptionWhenDescriptionIsNotSetInDocumentationPage() + { + $page = new DocumentationPage(); + + $this->assertPageDoesNotHaveMetadata($page, 'assertPageDoesNotHaveMetadata($page, '', '', '', + '', '', '', '', @@ -233,6 +234,7 @@ public function testCanonicalUrlTagsAreNotAddedWhenCanonicalUrlIsNotSet() '', '', '', + '', '', '', '', @@ -265,4 +267,29 @@ public function testCanonicalUrlTagsAreNotAddedWhenCanonicalUrlIsNotSet() $this->assertStringNotContainsString($text, $contents); } } + + public function testMetadataTagsInMarkdownPageWithDescription() + { + $this->file('_pages/test-page.md', <<<'MARKDOWN' + --- + title: "My Page Title" + description: "My page description" + --- + + ## Welcome to My Page + + This is a test page with a description. + MARKDOWN + ); + $this->build('_pages/test-page.md'); + + $this->assertSee('test-page', array_merge($this->getDefaultTags(), [ + '