Skip to content

Commit

Permalink
Merge pull request #686 from hydephp/code-cleanup
Browse files Browse the repository at this point in the history
Clean up and refactor code
  • Loading branch information
caendesilva authored Dec 21, 2024
2 parents 15d1cd9 + 285aee1 commit de86876
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
8 changes: 6 additions & 2 deletions src/Framework/Features/Metadata/PageMetadataBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, '/'));
}
}
68 changes: 34 additions & 34 deletions tests/Feature/MetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -209,7 +209,7 @@ public function testDoesNotAddCanonicalLinkWhenBaseUrlIsNotSet()
{
$this->withoutSiteUrl();

$page = MarkdownPage::make('bar');
$page = new MarkdownPage('bar');

$this->assertStringNotContainsString('<link rel="canonical"', $page->metadata->render());
}
Expand All @@ -218,7 +218,7 @@ public function testDoesNotAddCanonicalLinkWhenIdentifierIsNotSet()
{
config(['hyde.url' => 'foo']);

$page = MarkdownPage::make();
$page = new MarkdownPage();

$this->assertStringNotContainsString('<link rel="canonical"', $page->metadata->render());
}
Expand All @@ -227,7 +227,7 @@ public function testAddsCanonicalLinkWhenBaseUrlAndIdentifierIsSet()
{
config(['hyde.url' => 'foo']);

$page = MarkdownPage::make('bar');
$page = new MarkdownPage('bar');

$this->assertStringContainsString('<link rel="canonical" href="foo/bar.html">', $page->metadata->render());
}
Expand All @@ -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('<link rel="canonical" href="foo/bar">', $page->metadata->render());
}
Expand All @@ -246,7 +246,7 @@ public function testCanOverrideCanonicalLinkWithFrontMatter()
{
config(['hyde.url' => 'foo']);

$page = MarkdownPage::make('bar', [
$page = new MarkdownPage('bar', [
'canonicalUrl' => 'canonical',
]);

Expand All @@ -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(
'<meta name="twitter:title" content="HydePHP - Foo Bar">'."\n".
Expand All @@ -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());
}
Expand Down Expand Up @@ -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, '<meta name="author" content="My Author">');
}
Expand All @@ -346,12 +346,12 @@ public function testDoesNotAddAuthorWhenAuthorIsNotSetInPost()
{
$page = new MarkdownPost();

$this->assertPageDoesNotHaveMetadata($page, '<meta name="author" content="My Author">');
$this->assertPageDoesNotHaveMetadata($page, '<meta name="author"');
}

public function testAddsKeywordsWhenCategoryIsSetInPost()
{
$page = MarkdownPost::make(matter: ['category' => 'My Category']);
$page = new MarkdownPost(matter: ['category' => 'My Category']);

$this->assertPageHasMetadata($page, '<meta name="keywords" content="My Category">');
}
Expand All @@ -360,12 +360,12 @@ public function testDoesNotAddKeywordsWhenCategoryIsNotSetInPost()
{
$page = new MarkdownPost();

$this->assertPageDoesNotHaveMetadata($page, '<meta name="keywords" content="My Category">');
$this->assertPageDoesNotHaveMetadata($page, '<meta name="keywords"');
}

public function testAddsUrlPropertyWhenCanonicalUrlIsSetInPost()
{
$page = MarkdownPost::make(matter: ['canonicalUrl' => 'example.html']);
$page = new MarkdownPost(matter: ['canonicalUrl' => 'example.html']);

$this->assertPageHasMetadata($page, '<meta property="og:url" content="example.html">');
}
Expand All @@ -374,19 +374,19 @@ public function testDoesNotAddUrlPropertyWhenCanonicalUrlIsNotSetInPost()
{
$page = new MarkdownPost();

$this->assertPageDoesNotHaveMetadata($page, '<meta property="og:url" content="example.html">');
$this->assertPageDoesNotHaveMetadata($page, '<meta property="og:url"');
}

public function testDoesNotAddUrlPropertyWhenCanonicalUrlIsNull()
{
$page = MarkdownPost::make(matter: ['canonicalUrl' => null]);
$page = new MarkdownPost(matter: ['canonicalUrl' => null]);

$this->assertPageDoesNotHaveMetadata($page, '<meta property="og:url" content="example.html">');
$this->assertPageDoesNotHaveMetadata($page, '<meta property="og:url"');
}

public function testAddsTitlePropertyWhenTitleIsSetInPost()
{
$page = MarkdownPost::make(matter: ['title' => 'My Title']);
$page = new MarkdownPost(matter: ['title' => 'My Title']);

$this->assertPageHasMetadata($page, '<meta property="og:title" content="HydePHP - My Title">');
}
Expand All @@ -400,47 +400,47 @@ 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, '<meta property="og:article:published_time" content="2022-01-01T00:00:00+00:00">');
}

public function testDoesNotAddPublishedTimePropertyWhenDateIsNotSetInPost()
{
$page = new MarkdownPost();
$this->assertPageDoesNotHaveMetadata($page, '<meta property="og:article:published_time" content="2022-01-01T00:00:00+00:00">');
$this->assertPageDoesNotHaveMetadata($page, '<meta property="og:article:published_time"');
}

public function testAddsImagePropertyWhenImageIsSetInPost()
{
$page = MarkdownPost::make(matter: ['image' => 'image.jpg']);
$page = new MarkdownPost(matter: ['image' => 'image.jpg']);

$this->assertPageHasMetadata($page, '<meta property="og:image" content="../media/image.jpg">');
}

public function testDoesNotAddImagePropertyWhenImageIsNotSetInPost()
{
$page = new MarkdownPost();
$this->assertPageDoesNotHaveMetadata($page, '<meta property="og:image" content="media/image.jpg">');
$this->assertPageDoesNotHaveMetadata($page, '<meta property="og:image"');
}

public function testAddsTypePropertyAutomatically()
{
$page = MarkdownPost::make();
$page = new MarkdownPost();

$this->assertPageHasMetadata($page, '<meta property="og:type" content="article">');
}

public function testDynamicPostMetaPropertiesReturnsBaseArrayWhenInitializedWithEmptyFrontMatter()
{
$page = MarkdownPost::make();
$page = new MarkdownPost();

$this->assertSame('<meta property="og:type" content="article">', $page->metadata->render());
}

public function testDynamicPostMetaPropertiesContainsImageMetadataWhenFeaturedImageSetToString()
{
$page = MarkdownPost::make(matter: [
$page = new MarkdownPost(matter: [
'image' => 'foo.jpg',
]);

Expand All @@ -449,7 +449,7 @@ public function testDynamicPostMetaPropertiesContainsImageMetadataWhenFeaturedIm

public function testDynamicPostMetaPropertiesContainsImageLinkThatIsAlwaysRelative()
{
$page = MarkdownPost::make(matter: [
$page = new MarkdownPost(matter: [
'image' => 'foo.jpg',
]);

Expand All @@ -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',
]);

Expand All @@ -469,7 +469,7 @@ public function testDynamicPostMetaPropertiesContainsImageLinkThatIsAlwaysRelati
{
MarkdownPost::setOutputDirectory('_posts/foo');

$page = MarkdownPost::make(matter: [
$page = new MarkdownPost(matter: [
'image' => 'foo.jpg',
]);

Expand All @@ -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',
]);

Expand All @@ -491,7 +491,7 @@ public function testDynamicPostMetaPropertiesContainsImageLinkThatUsesTheConfigu
{
Hyde::setMediaDirectory('assets');

$page = MarkdownPost::make(matter: [
$page = new MarkdownPost(matter: [
'image' => 'foo.jpg',
]);

Expand All @@ -500,7 +500,7 @@ public function testDynamicPostMetaPropertiesContainsImageLinkThatUsesTheConfigu

public function testDynamicPostMetaPropertiesContainsImageMetadataWhenFeaturedImageSetToArrayWithPath()
{
$page = MarkdownPost::make(matter: [
$page = new MarkdownPost(matter: [
'image' => [
'source' => 'foo.jpg',
],
Expand All @@ -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',
],
Expand All @@ -522,7 +522,7 @@ public function testDynamicPostMetaPropertiesContainsImageMetadataWhenFeaturedIm

public function testDynamicPostAuthorReturnsAuthorNameWhenAuthorSetToArrayUsingUsername()
{
$page = MarkdownPost::make(matter: [
$page = new MarkdownPost(matter: [
'author' => [
'username' => 'username',
],
Expand All @@ -533,7 +533,7 @@ public function testDynamicPostAuthorReturnsAuthorNameWhenAuthorSetToArrayUsingU

public function testDynamicPostAuthorReturnsAuthorNameWhenAuthorSetToArrayUsingName()
{
$page = MarkdownPost::make(matter: [
$page = new MarkdownPost(matter: [
'author' => [
'name' => 'Name',
],
Expand All @@ -544,7 +544,7 @@ public function testDynamicPostAuthorReturnsAuthorNameWhenAuthorSetToArrayUsingN

public function testNoAuthorIsSetWhenAuthorSetToArrayWithoutNameOrUsername()
{
$page = MarkdownPost::make(matter: [
$page = new MarkdownPost(matter: [
'author' => [],
]);

Expand Down

0 comments on commit de86876

Please sign in to comment.