Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This serves two purposes:
- Updated the `HydeKernel` array representation to include the Hyde version in https://github.com/hydephp/develop/pull/1786
- Registered the `cache:clear` command to make it easier to clear the cache in https://github.com/hydephp/develop/pull/1881
- Added a new `Hyperlinks::isRemote()` helper method to check if a URL is remote in https://github.com/hydephp/develop/pull/1882
- All page types now support the `description` front matter field (used in page metadata) in https://github.com/hydephp/develop/pull/1884

### Changed
- Updated the `Serializable` trait to provide a default automatic `toArray` method in https://github.com/hydephp/develop/pull/1791
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand All @@ -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');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 <meta name='description'> 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,
];
}
56 changes: 54 additions & 2 deletions packages/framework/tests/Feature/MetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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, '<meta name="description" content="My Description">');
$this->assertPageHasMetadata($page, '<meta property="og:description" content="My Description">');
}

public function testDoesNotAddDescriptionWhenDescriptionIsNotSetInPost()
{
$page = new MarkdownPost();

$this->assertPageDoesNotHaveMetadata($page, '<meta name="description" content="My Description">');
$this->assertPageDoesNotHaveMetadata($page, '<meta name="description"');
$this->assertPageDoesNotHaveMetadata($page, '<meta property="og:description"');
}

public function testAddsDescriptionWhenDescriptionIsSetInMarkdownPage()
{
$page = new MarkdownPage(matter: ['description' => 'My Page Description']);

$this->assertPageHasMetadata($page, '<meta name="description" content="My Page Description">');
$this->assertPageHasMetadata($page, '<meta property="og:description" content="My Page Description">');
}

public function testDoesNotAddDescriptionWhenDescriptionIsNotSetInMarkdownPage()
{
$page = new MarkdownPage();

$this->assertPageDoesNotHaveMetadata($page, '<meta name="description"');
$this->assertPageDoesNotHaveMetadata($page, '<meta property="og:description"');
}

public function testAddsDescriptionWhenDescriptionIsSetInBladePage()
{
$page = new BladePage(matter: ['description' => 'My Page Description']);

$this->assertPageHasMetadata($page, '<meta name="description" content="My Page Description">');
$this->assertPageHasMetadata($page, '<meta property="og:description" content="My Page Description">');
}

public function testDoesNotAddDescriptionWhenDescriptionIsNotSetInBladePage()
{
$page = new BladePage();

$this->assertPageDoesNotHaveMetadata($page, '<meta name="description"');
$this->assertPageDoesNotHaveMetadata($page, '<meta property="og:description"');
}

public function testAddsDescriptionWhenDescriptionIsSetInDocumentationPage()
{
$page = new DocumentationPage(matter: ['description' => 'My Page Description']);

$this->assertPageHasMetadata($page, '<meta name="description" content="My Page Description">');
$this->assertPageHasMetadata($page, '<meta property="og:description" content="My Page Description">');
}

public function testDoesNotAddDescriptionWhenDescriptionIsNotSetInDocumentationPage()
{
$page = new DocumentationPage();

$this->assertPageDoesNotHaveMetadata($page, '<meta name="description"');
$this->assertPageDoesNotHaveMetadata($page, '<meta property="og:description"');
}

public function testAddsAuthorWhenAuthorIsSetInPost()
Expand Down
27 changes: 27 additions & 0 deletions packages/framework/tests/Feature/Views/MetadataViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ public function testMetadataTagsInMarkdownPostWithFlatFrontMatter()
'<meta name="keywords" content="My category">',
'<meta name="url" content="https://example.com/posts/test.html">',
'<meta property="og:title" content="HydePHP - My title">',
'<meta property="og:description" content="My description">',
'<meta property="og:url" content="https://example.com/posts/test.html">',
'<meta property="og:type" content="article">',
'<meta property="og:article:published_time" content="2022-01-01T00:00:00+00:00">',
Expand Down Expand Up @@ -233,6 +234,7 @@ public function testCanonicalUrlTagsAreNotAddedWhenCanonicalUrlIsNotSet()
'<meta name="author" content="Mr. Hyde">',
'<meta name="keywords" content="My category">',
'<meta property="og:title" content="HydePHP - My title">',
'<meta property="og:description" content="My description">',
'<meta property="og:type" content="article">',
'<meta property="og:article:published_time" content="2022-01-01T00:00:00+00:00">',
'<meta property="og:image" content="../media/image.jpg">',
Expand Down Expand Up @@ -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(), [
'<title>HydePHP - My Page Title</title>',
'<link rel="stylesheet" href="media/app.css">',
'<meta name="twitter:title" content="HydePHP - My Page Title">',
'<meta property="og:title" content="HydePHP - My Page Title">',
'<meta property="og:description" content="My page description">',
'<meta name="description" content="My page description">',
]));
}
}
1 change: 1 addition & 0 deletions packages/framework/tests/Unit/SchemaContractsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function testSchemasAreNotAccidentallyChanged()
{
$this->assertSame([
'title' => 'string',
'description' => 'string',
'canonicalUrl' => 'string',
'navigation' => NavigationSchema::NAVIGATION_SCHEMA,
], PageSchema::PAGE_SCHEMA);
Expand Down