Skip to content

Commit

Permalink
Merge pull request #1886 from hydephp/code-cleanup
Browse files Browse the repository at this point in the history
Clean up and refactor code hydephp/develop@31e3730
  • Loading branch information
github-actions authored and caendesilva committed Dec 21, 2024
1 parent ee9b130 commit 285aee1
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 @@ -77,7 +77,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 @@ -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'),
Expand All @@ -207,7 +207,7 @@ public function testDoesNotAddCanonicalLinkWhenBaseUrlIsNotSet()
{
$this->withoutSiteUrl();

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

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

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

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

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

Expand All @@ -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(
'<meta name="twitter:title" content="HydePHP - Foo Bar">'."\n".
Expand All @@ -264,7 +264,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 All @@ -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, '<meta name="author" content="My Author">');
}
Expand All @@ -294,12 +294,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 @@ -308,12 +308,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 @@ -322,19 +322,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 @@ -348,47 +348,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 @@ -397,7 +397,7 @@ public function testDynamicPostMetaPropertiesContainsImageMetadataWhenFeaturedIm

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

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

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

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

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

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

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

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

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

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

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

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

Expand Down

0 comments on commit 285aee1

Please sign in to comment.