From 53b99e03c8914386dcec0457e994a7d7579c958d Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 23 Jul 2024 17:29:53 +0200 Subject: [PATCH 1/5] Add todo --- .../src/Framework/Features/Metadata/PageMetadataBag.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/framework/src/Framework/Features/Metadata/PageMetadataBag.php b/packages/framework/src/Framework/Features/Metadata/PageMetadataBag.php index efada5ec3a3..3860ee69eb3 100644 --- a/packages/framework/src/Framework/Features/Metadata/PageMetadataBag.php +++ b/packages/framework/src/Framework/Features/Metadata/PageMetadataBag.php @@ -77,6 +77,8 @@ protected function resolveImageLink(string $image): string { // Since this is run before the page is rendered, we don't have the currentPage property. // So we need to run some of the same calculations here to resolve the image path link. + + // Todo: Cleanup return Hyperlinks::isRemote($image) ? $image : str_repeat('../', substr_count(MarkdownPost::outputDirectory().'/'.$this->page->identifier, '/')).$image; } From eca98b1ac308d9e369a57917552ebe9d09234b45 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 23 Jul 2024 17:54:43 +0200 Subject: [PATCH 2/5] Use more specific assertions --- packages/framework/tests/Feature/MetadataTest.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/framework/tests/Feature/MetadataTest.php b/packages/framework/tests/Feature/MetadataTest.php index c5b31be52c3..0f4694f8436 100644 --- a/packages/framework/tests/Feature/MetadataTest.php +++ b/packages/framework/tests/Feature/MetadataTest.php @@ -280,7 +280,7 @@ public function testDoesNotAddDescriptionWhenDescriptionIsNotSetInPost() { $page = new MarkdownPost(); - $this->assertPageDoesNotHaveMetadata($page, ''); + $this->assertPageDoesNotHaveMetadata($page, 'assertPageDoesNotHaveMetadata($page, ''); + $this->assertPageDoesNotHaveMetadata($page, 'assertPageDoesNotHaveMetadata($page, ''); + $this->assertPageDoesNotHaveMetadata($page, 'assertPageDoesNotHaveMetadata($page, ''); + $this->assertPageDoesNotHaveMetadata($page, ' null]); - $this->assertPageDoesNotHaveMetadata($page, ''); + $this->assertPageDoesNotHaveMetadata($page, 'assertPageDoesNotHaveMetadata($page, ''); + $this->assertPageDoesNotHaveMetadata($page, 'assertPageDoesNotHaveMetadata($page, ''); + $this->assertPageDoesNotHaveMetadata($page, ' Date: Tue, 23 Jul 2024 17:55:24 +0200 Subject: [PATCH 3/5] Normalize constructor syntax --- .../framework/tests/Feature/MetadataTest.php | 58 +++++++++---------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/packages/framework/tests/Feature/MetadataTest.php b/packages/framework/tests/Feature/MetadataTest.php index 0f4694f8436..369a5ca3dcb 100644 --- a/packages/framework/tests/Feature/MetadataTest.php +++ b/packages/framework/tests/Feature/MetadataTest.php @@ -195,7 +195,7 @@ public function testDynamicMetadataOverridesConfigDefinedMetadata() Meta::name('twitter:title', 'bar'), ]]); - $page = MarkdownPage::make(matter: ['title' => 'baz']); + $page = new MarkdownPage(matter: ['title' => 'baz']); $this->assertEquals([ 'metadata:twitter:title' => Meta::name('twitter:title', 'HydePHP - baz'), @@ -207,7 +207,7 @@ public function testDoesNotAddCanonicalLinkWhenBaseUrlIsNotSet() { $this->withoutSiteUrl(); - $page = MarkdownPage::make('bar'); + $page = new MarkdownPage('bar'); $this->assertStringNotContainsString('metadata->render()); } @@ -216,7 +216,7 @@ public function testDoesNotAddCanonicalLinkWhenIdentifierIsNotSet() { config(['hyde.url' => 'foo']); - $page = MarkdownPage::make(); + $page = new MarkdownPage(); $this->assertStringNotContainsString('metadata->render()); } @@ -225,7 +225,7 @@ public function testAddsCanonicalLinkWhenBaseUrlAndIdentifierIsSet() { config(['hyde.url' => 'foo']); - $page = MarkdownPage::make('bar'); + $page = new MarkdownPage('bar'); $this->assertStringContainsString('', $page->metadata->render()); } @@ -235,7 +235,7 @@ public function testCanonicalLinkUsesCleanUrlSetting() config(['hyde.url' => 'foo']); config(['hyde.pretty_urls' => true]); - $page = MarkdownPage::make('bar'); + $page = new MarkdownPage('bar'); $this->assertStringContainsString('', $page->metadata->render()); } @@ -244,7 +244,7 @@ public function testCanOverrideCanonicalLinkWithFrontMatter() { config(['hyde.url' => 'foo']); - $page = MarkdownPage::make('bar', [ + $page = new MarkdownPage('bar', [ 'canonicalUrl' => 'canonical', ]); @@ -253,7 +253,7 @@ public function testCanOverrideCanonicalLinkWithFrontMatter() public function testAddsTwitterAndOpenGraphTitleWhenTitleIsSet() { - $page = MarkdownPage::make(matter: ['title' => 'Foo Bar']); + $page = new MarkdownPage(matter: ['title' => 'Foo Bar']); $this->assertSame( ''."\n". @@ -264,14 +264,14 @@ public function testAddsTwitterAndOpenGraphTitleWhenTitleIsSet() public function testDoesNotAddTwitterAndOpenGraphTitleWhenNoTitleIsSet() { - $page = MarkdownPage::make(matter: ['title' => null]); + $page = new MarkdownPage(matter: ['title' => null]); $this->assertSame('', $page->metadata->render()); } public function testAddsDescriptionWhenDescriptionIsSetInPost() { - $page = MarkdownPost::make(matter: ['description' => 'My Description']); + $page = new MarkdownPost(matter: ['description' => 'My Description']); $this->assertPageHasMetadata($page, ''); } @@ -285,7 +285,7 @@ public function testDoesNotAddDescriptionWhenDescriptionIsNotSetInPost() public function testAddsAuthorWhenAuthorIsSetInPost() { - $page = MarkdownPost::make(matter: ['author' => 'My Author']); + $page = new MarkdownPost(matter: ['author' => 'My Author']); $this->assertPageHasMetadata($page, ''); } @@ -299,7 +299,7 @@ public function testDoesNotAddAuthorWhenAuthorIsNotSetInPost() public function testAddsKeywordsWhenCategoryIsSetInPost() { - $page = MarkdownPost::make(matter: ['category' => 'My Category']); + $page = new MarkdownPost(matter: ['category' => 'My Category']); $this->assertPageHasMetadata($page, ''); } @@ -313,7 +313,7 @@ public function testDoesNotAddKeywordsWhenCategoryIsNotSetInPost() public function testAddsUrlPropertyWhenCanonicalUrlIsSetInPost() { - $page = MarkdownPost::make(matter: ['canonicalUrl' => 'example.html']); + $page = new MarkdownPost(matter: ['canonicalUrl' => 'example.html']); $this->assertPageHasMetadata($page, ''); } @@ -327,14 +327,14 @@ public function testDoesNotAddUrlPropertyWhenCanonicalUrlIsNotSetInPost() public function testDoesNotAddUrlPropertyWhenCanonicalUrlIsNull() { - $page = MarkdownPost::make(matter: ['canonicalUrl' => null]); + $page = new MarkdownPost(matter: ['canonicalUrl' => null]); $this->assertPageDoesNotHaveMetadata($page, ' 'My Title']); + $page = new MarkdownPost(matter: ['title' => 'My Title']); $this->assertPageHasMetadata($page, ''); } @@ -348,7 +348,7 @@ public function testDoesNotAddTitlePropertyWhenTitleIsNotSetInPost() public function testAddsPublishedTimePropertyWhenDateIsSetInPost() { - $page = MarkdownPost::make(matter: ['date' => '2022-01-01']); + $page = new MarkdownPost(matter: ['date' => '2022-01-01']); $this->assertPageHasMetadata($page, ''); } @@ -361,7 +361,7 @@ public function testDoesNotAddPublishedTimePropertyWhenDateIsNotSetInPost() public function testAddsImagePropertyWhenImageIsSetInPost() { - $page = MarkdownPost::make(matter: ['image' => 'image.jpg']); + $page = new MarkdownPost(matter: ['image' => 'image.jpg']); $this->assertPageHasMetadata($page, ''); } @@ -374,21 +374,21 @@ public function testDoesNotAddImagePropertyWhenImageIsNotSetInPost() public function testAddsTypePropertyAutomatically() { - $page = MarkdownPost::make(); + $page = new MarkdownPost(); $this->assertPageHasMetadata($page, ''); } public function testDynamicPostMetaPropertiesReturnsBaseArrayWhenInitializedWithEmptyFrontMatter() { - $page = MarkdownPost::make(); + $page = new MarkdownPost(); $this->assertSame('', $page->metadata->render()); } public function testDynamicPostMetaPropertiesContainsImageMetadataWhenFeaturedImageSetToString() { - $page = MarkdownPost::make(matter: [ + $page = new MarkdownPost(matter: [ 'image' => 'foo.jpg', ]); @@ -397,7 +397,7 @@ public function testDynamicPostMetaPropertiesContainsImageMetadataWhenFeaturedIm public function testDynamicPostMetaPropertiesContainsImageLinkThatIsAlwaysRelative() { - $page = MarkdownPost::make(matter: [ + $page = new MarkdownPost(matter: [ 'image' => 'foo.jpg', ]); @@ -406,7 +406,7 @@ public function testDynamicPostMetaPropertiesContainsImageLinkThatIsAlwaysRelati public function testDynamicPostMetaPropertiesContainsImageLinkThatIsAlwaysRelativeForNestedPosts() { - $page = MarkdownPost::make('foo/bar', matter: [ + $page = new MarkdownPost('foo/bar', matter: [ 'image' => 'foo.jpg', ]); @@ -417,7 +417,7 @@ public function testDynamicPostMetaPropertiesContainsImageLinkThatIsAlwaysRelati { MarkdownPost::setOutputDirectory('_posts/foo'); - $page = MarkdownPost::make(matter: [ + $page = new MarkdownPost(matter: [ 'image' => 'foo.jpg', ]); @@ -428,7 +428,7 @@ public function testDynamicPostMetaPropertiesContainsImageLinkThatIsAlwaysRelati { MarkdownPost::setOutputDirectory('_posts/foo'); - $page = MarkdownPost::make('bar/baz', matter: [ + $page = new MarkdownPost('bar/baz', matter: [ 'image' => 'foo.jpg', ]); @@ -439,7 +439,7 @@ public function testDynamicPostMetaPropertiesContainsImageLinkThatUsesTheConfigu { Hyde::setMediaDirectory('assets'); - $page = MarkdownPost::make(matter: [ + $page = new MarkdownPost(matter: [ 'image' => 'foo.jpg', ]); @@ -448,7 +448,7 @@ public function testDynamicPostMetaPropertiesContainsImageLinkThatUsesTheConfigu public function testDynamicPostMetaPropertiesContainsImageMetadataWhenFeaturedImageSetToArrayWithPath() { - $page = MarkdownPost::make(matter: [ + $page = new MarkdownPost(matter: [ 'image' => [ 'source' => 'foo.jpg', ], @@ -459,7 +459,7 @@ public function testDynamicPostMetaPropertiesContainsImageMetadataWhenFeaturedIm public function testDynamicPostMetaPropertiesContainsImageMetadataWhenFeaturedImageSetToArrayWithUrl() { - $page = MarkdownPost::make(matter: [ + $page = new MarkdownPost(matter: [ 'image' => [ 'source' => 'https://example.com/foo.jpg', ], @@ -470,7 +470,7 @@ public function testDynamicPostMetaPropertiesContainsImageMetadataWhenFeaturedIm public function testDynamicPostAuthorReturnsAuthorNameWhenAuthorSetToArrayUsingUsername() { - $page = MarkdownPost::make(matter: [ + $page = new MarkdownPost(matter: [ 'author' => [ 'username' => 'username', ], @@ -481,7 +481,7 @@ public function testDynamicPostAuthorReturnsAuthorNameWhenAuthorSetToArrayUsingU public function testDynamicPostAuthorReturnsAuthorNameWhenAuthorSetToArrayUsingName() { - $page = MarkdownPost::make(matter: [ + $page = new MarkdownPost(matter: [ 'author' => [ 'name' => 'Name', ], @@ -492,7 +492,7 @@ public function testDynamicPostAuthorReturnsAuthorNameWhenAuthorSetToArrayUsingN public function testNoAuthorIsSetWhenAuthorSetToArrayWithoutNameOrUsername() { - $page = MarkdownPost::make(matter: [ + $page = new MarkdownPost(matter: [ 'author' => [], ]); From a1a16da7805a282bcb4b04ccc0782a187b3cef08 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 23 Jul 2024 17:58:25 +0200 Subject: [PATCH 4/5] Revert "Add todo" This reverts commit 53b99e03c8914386dcec0457e994a7d7579c958d. --- .../src/Framework/Features/Metadata/PageMetadataBag.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/framework/src/Framework/Features/Metadata/PageMetadataBag.php b/packages/framework/src/Framework/Features/Metadata/PageMetadataBag.php index e9d63862462..f150adcbc0f 100644 --- a/packages/framework/src/Framework/Features/Metadata/PageMetadataBag.php +++ b/packages/framework/src/Framework/Features/Metadata/PageMetadataBag.php @@ -81,8 +81,6 @@ protected function resolveImageLink(string $image): string { // Since this is run before the page is rendered, we don't have the currentPage property. // So we need to run some of the same calculations here to resolve the image path link. - - // Todo: Cleanup return Hyperlinks::isRemote($image) ? $image : str_repeat('../', substr_count(MarkdownPost::outputDirectory().'/'.$this->page->identifier, '/')).$image; } From 7637ac413995755173eec6d5ed23e8d882e337cb Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 23 Jul 2024 17:59:31 +0200 Subject: [PATCH 5/5] Extract helper method --- .../src/Framework/Features/Metadata/PageMetadataBag.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Framework/Features/Metadata/PageMetadataBag.php b/packages/framework/src/Framework/Features/Metadata/PageMetadataBag.php index f150adcbc0f..23a764b3d7f 100644 --- a/packages/framework/src/Framework/Features/Metadata/PageMetadataBag.php +++ b/packages/framework/src/Framework/Features/Metadata/PageMetadataBag.php @@ -81,7 +81,11 @@ protected function resolveImageLink(string $image): string { // Since this is run before the page is rendered, we don't have the currentPage property. // So we need to run some of the same calculations here to resolve the image path link. - return Hyperlinks::isRemote($image) ? $image - : str_repeat('../', substr_count(MarkdownPost::outputDirectory().'/'.$this->page->identifier, '/')).$image; + return Hyperlinks::isRemote($image) ? $image : $this->calculatePathTraversal().$image; + } + + private function calculatePathTraversal(): string + { + return str_repeat('../', substr_count(MarkdownPost::outputDirectory().'/'.$this->page->identifier, '/')); } }