diff --git a/src/Framework/Features/Metadata/PageMetadataBag.php b/src/Framework/Features/Metadata/PageMetadataBag.php index f150adcb..23a764b3 100644 --- a/src/Framework/Features/Metadata/PageMetadataBag.php +++ b/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, '/')); } } diff --git a/tests/Feature/MetadataTest.php b/tests/Feature/MetadataTest.php index a9273baa..944114e1 100644 --- a/tests/Feature/MetadataTest.php +++ b/tests/Feature/MetadataTest.php @@ -197,7 +197,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'), @@ -209,7 +209,7 @@ public function testDoesNotAddCanonicalLinkWhenBaseUrlIsNotSet() { $this->withoutSiteUrl(); - $page = MarkdownPage::make('bar'); + $page = new MarkdownPage('bar'); $this->assertStringNotContainsString('metadata->render()); } @@ -218,7 +218,7 @@ public function testDoesNotAddCanonicalLinkWhenIdentifierIsNotSet() { config(['hyde.url' => 'foo']); - $page = MarkdownPage::make(); + $page = new MarkdownPage(); $this->assertStringNotContainsString('metadata->render()); } @@ -227,7 +227,7 @@ public function testAddsCanonicalLinkWhenBaseUrlAndIdentifierIsSet() { config(['hyde.url' => 'foo']); - $page = MarkdownPage::make('bar'); + $page = new MarkdownPage('bar'); $this->assertStringContainsString('', $page->metadata->render()); } @@ -237,7 +237,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()); } @@ -246,7 +246,7 @@ public function testCanOverrideCanonicalLinkWithFrontMatter() { config(['hyde.url' => 'foo']); - $page = MarkdownPage::make('bar', [ + $page = new MarkdownPage('bar', [ 'canonicalUrl' => 'canonical', ]); @@ -255,7 +255,7 @@ public function testCanOverrideCanonicalLinkWithFrontMatter() public function testAddsTwitterAndOpenGraphTitleWhenTitleIsSet() { - $page = MarkdownPage::make(matter: ['title' => 'Foo Bar']); + $page = new MarkdownPage(matter: ['title' => 'Foo Bar']); $this->assertSame( ''."\n". @@ -266,7 +266,7 @@ public function testAddsTwitterAndOpenGraphTitleWhenTitleIsSet() public function testDoesNotAddTwitterAndOpenGraphTitleWhenNoTitleIsSet() { - $page = MarkdownPage::make(matter: ['title' => null]); + $page = new MarkdownPage(matter: ['title' => null]); $this->assertSame('', $page->metadata->render()); } @@ -337,7 +337,7 @@ public function testDoesNotAddDescriptionWhenDescriptionIsNotSetInDocumentationP public function testAddsAuthorWhenAuthorIsSetInPost() { - $page = MarkdownPost::make(matter: ['author' => 'My Author']); + $page = new MarkdownPost(matter: ['author' => 'My Author']); $this->assertPageHasMetadata($page, ''); } @@ -346,12 +346,12 @@ public function testDoesNotAddAuthorWhenAuthorIsNotSetInPost() { $page = new MarkdownPost(); - $this->assertPageDoesNotHaveMetadata($page, ''); + $this->assertPageDoesNotHaveMetadata($page, ' 'My Category']); + $page = new MarkdownPost(matter: ['category' => 'My Category']); $this->assertPageHasMetadata($page, ''); } @@ -360,12 +360,12 @@ public function testDoesNotAddKeywordsWhenCategoryIsNotSetInPost() { $page = new MarkdownPost(); - $this->assertPageDoesNotHaveMetadata($page, ''); + $this->assertPageDoesNotHaveMetadata($page, ' 'example.html']); + $page = new MarkdownPost(matter: ['canonicalUrl' => 'example.html']); $this->assertPageHasMetadata($page, ''); } @@ -374,19 +374,19 @@ public function testDoesNotAddUrlPropertyWhenCanonicalUrlIsNotSetInPost() { $page = new MarkdownPost(); - $this->assertPageDoesNotHaveMetadata($page, ''); + $this->assertPageDoesNotHaveMetadata($page, ' null]); + $page = new MarkdownPost(matter: ['canonicalUrl' => null]); - $this->assertPageDoesNotHaveMetadata($page, ''); + $this->assertPageDoesNotHaveMetadata($page, ' 'My Title']); + $page = new MarkdownPost(matter: ['title' => 'My Title']); $this->assertPageHasMetadata($page, ''); } @@ -400,7 +400,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, ''); } @@ -408,12 +408,12 @@ public function testAddsPublishedTimePropertyWhenDateIsSetInPost() public function testDoesNotAddPublishedTimePropertyWhenDateIsNotSetInPost() { $page = new MarkdownPost(); - $this->assertPageDoesNotHaveMetadata($page, ''); + $this->assertPageDoesNotHaveMetadata($page, ' 'image.jpg']); + $page = new MarkdownPost(matter: ['image' => 'image.jpg']); $this->assertPageHasMetadata($page, ''); } @@ -421,26 +421,26 @@ public function testAddsImagePropertyWhenImageIsSetInPost() public function testDoesNotAddImagePropertyWhenImageIsNotSetInPost() { $page = new MarkdownPost(); - $this->assertPageDoesNotHaveMetadata($page, ''); + $this->assertPageDoesNotHaveMetadata($page, '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', ]); @@ -449,7 +449,7 @@ public function testDynamicPostMetaPropertiesContainsImageMetadataWhenFeaturedIm public function testDynamicPostMetaPropertiesContainsImageLinkThatIsAlwaysRelative() { - $page = MarkdownPost::make(matter: [ + $page = new MarkdownPost(matter: [ 'image' => 'foo.jpg', ]); @@ -458,7 +458,7 @@ public function testDynamicPostMetaPropertiesContainsImageLinkThatIsAlwaysRelati public function testDynamicPostMetaPropertiesContainsImageLinkThatIsAlwaysRelativeForNestedPosts() { - $page = MarkdownPost::make('foo/bar', matter: [ + $page = new MarkdownPost('foo/bar', matter: [ 'image' => 'foo.jpg', ]); @@ -469,7 +469,7 @@ public function testDynamicPostMetaPropertiesContainsImageLinkThatIsAlwaysRelati { MarkdownPost::setOutputDirectory('_posts/foo'); - $page = MarkdownPost::make(matter: [ + $page = new MarkdownPost(matter: [ 'image' => 'foo.jpg', ]); @@ -480,7 +480,7 @@ public function testDynamicPostMetaPropertiesContainsImageLinkThatIsAlwaysRelati { MarkdownPost::setOutputDirectory('_posts/foo'); - $page = MarkdownPost::make('bar/baz', matter: [ + $page = new MarkdownPost('bar/baz', matter: [ 'image' => 'foo.jpg', ]); @@ -491,7 +491,7 @@ public function testDynamicPostMetaPropertiesContainsImageLinkThatUsesTheConfigu { Hyde::setMediaDirectory('assets'); - $page = MarkdownPost::make(matter: [ + $page = new MarkdownPost(matter: [ 'image' => 'foo.jpg', ]); @@ -500,7 +500,7 @@ public function testDynamicPostMetaPropertiesContainsImageLinkThatUsesTheConfigu public function testDynamicPostMetaPropertiesContainsImageMetadataWhenFeaturedImageSetToArrayWithPath() { - $page = MarkdownPost::make(matter: [ + $page = new MarkdownPost(matter: [ 'image' => [ 'source' => 'foo.jpg', ], @@ -511,7 +511,7 @@ public function testDynamicPostMetaPropertiesContainsImageMetadataWhenFeaturedIm public function testDynamicPostMetaPropertiesContainsImageMetadataWhenFeaturedImageSetToArrayWithUrl() { - $page = MarkdownPost::make(matter: [ + $page = new MarkdownPost(matter: [ 'image' => [ 'source' => 'https://example.com/foo.jpg', ], @@ -522,7 +522,7 @@ public function testDynamicPostMetaPropertiesContainsImageMetadataWhenFeaturedIm public function testDynamicPostAuthorReturnsAuthorNameWhenAuthorSetToArrayUsingUsername() { - $page = MarkdownPost::make(matter: [ + $page = new MarkdownPost(matter: [ 'author' => [ 'username' => 'username', ], @@ -533,7 +533,7 @@ public function testDynamicPostAuthorReturnsAuthorNameWhenAuthorSetToArrayUsingU public function testDynamicPostAuthorReturnsAuthorNameWhenAuthorSetToArrayUsingName() { - $page = MarkdownPost::make(matter: [ + $page = new MarkdownPost(matter: [ 'author' => [ 'name' => 'Name', ], @@ -544,7 +544,7 @@ public function testDynamicPostAuthorReturnsAuthorNameWhenAuthorSetToArrayUsingN public function testNoAuthorIsSetWhenAuthorSetToArrayWithoutNameOrUsername() { - $page = MarkdownPost::make(matter: [ + $page = new MarkdownPost(matter: [ 'author' => [], ]);