Skip to content

Commit

Permalink
Merge pull request #684 from hydephp/cleanup-canonical-page-helper-me…
Browse files Browse the repository at this point in the history
…thod

Cleanup the canonical URL helper method
  • Loading branch information
caendesilva authored Dec 21, 2024
2 parents ee9b130 + d54ea72 commit bc31435
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/Pages/Concerns/HydePage.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,13 @@ public function navigationMenuGroup(): ?string
return $this->navigation->group;
}

/**
* Get the canonical URL for the page to use in the `<link rel="canonical">` tag.
*
* It can be explicitly set in the front matter using the `canonicalUrl` key,
* otherwise it will be generated based on the site URL and the output path,
* unless there is no configured base URL, leading to this returning null.
*/
public function getCanonicalUrl(): ?string
{
/** @var ?string $value */
Expand Down
14 changes: 13 additions & 1 deletion tests/Unit/Pages/BladePageUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,18 @@ public function testMatter()

public function testGetCanonicalUrl()
{
$this->markTestSkipped('Not yet implemented');
$page = new BladePage('foo');
$this->assertNull($page->getCanonicalUrl());

self::mockConfig(['hyde.url' => 'https://example.com']);

$this->assertSame('https://example.com/foo.html', $page->getCanonicalUrl());

self::mockConfig(['hyde.url' => 'https://example.com', 'hyde.pretty_urls' => true]);

$this->assertSame('https://example.com/foo', $page->getCanonicalUrl());

$page = new BladePage('foo', ['canonicalUrl' => 'foo']);
$this->assertSame('foo', $page->getCanonicalUrl());
}
}
14 changes: 13 additions & 1 deletion tests/Unit/Pages/DocumentationPageUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,18 @@ public function testSave()

public function testGetCanonicalUrl()
{
$this->markTestSkipped('Not yet implemented');
$page = new DocumentationPage('foo');
$this->assertNull($page->getCanonicalUrl());

self::mockConfig(['hyde.url' => 'https://example.com']);

$this->assertSame('https://example.com/docs/foo.html', $page->getCanonicalUrl());

self::mockConfig(['hyde.url' => 'https://example.com', 'hyde.pretty_urls' => true]);

$this->assertSame('https://example.com/docs/foo', $page->getCanonicalUrl());

$page = new DocumentationPage('foo', ['canonicalUrl' => 'foo']);
$this->assertSame('foo', $page->getCanonicalUrl());
}
}
14 changes: 13 additions & 1 deletion tests/Unit/Pages/HtmlPageUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,18 @@ public function testMatter()

public function testGetCanonicalUrl()
{
$this->markTestSkipped('Not yet implemented');
$page = new HtmlPage('foo');
$this->assertNull($page->getCanonicalUrl());

self::mockConfig(['hyde.url' => 'https://example.com']);

$this->assertSame('https://example.com/foo.html', $page->getCanonicalUrl());

self::mockConfig(['hyde.url' => 'https://example.com', 'hyde.pretty_urls' => true]);

$this->assertSame('https://example.com/foo', $page->getCanonicalUrl());

$page = new HtmlPage('foo', ['canonicalUrl' => 'foo']);
$this->assertSame('foo', $page->getCanonicalUrl());
}
}
14 changes: 13 additions & 1 deletion tests/Unit/Pages/InMemoryPageUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,18 @@ public function testMatter()

public function testGetCanonicalUrl()
{
$this->markTestSkipped('Not yet implemented');
$page = new InMemoryPage('foo');
$this->assertNull($page->getCanonicalUrl());

self::mockConfig(['hyde.url' => 'https://example.com']);

$this->assertSame('https://example.com/foo.html', $page->getCanonicalUrl());

self::mockConfig(['hyde.url' => 'https://example.com', 'hyde.pretty_urls' => true]);

$this->assertSame('https://example.com/foo', $page->getCanonicalUrl());

$page = new InMemoryPage('foo', ['canonicalUrl' => 'foo']);
$this->assertSame('foo', $page->getCanonicalUrl());
}
}
14 changes: 13 additions & 1 deletion tests/Unit/Pages/MarkdownPageUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,18 @@ public function testSave()

public function testGetCanonicalUrl()
{
$this->markTestSkipped('Not yet implemented');
$page = new MarkdownPage('foo');
$this->assertNull($page->getCanonicalUrl());

self::mockConfig(['hyde.url' => 'https://example.com']);

$this->assertSame('https://example.com/foo.html', $page->getCanonicalUrl());

self::mockConfig(['hyde.url' => 'https://example.com', 'hyde.pretty_urls' => true]);

$this->assertSame('https://example.com/foo', $page->getCanonicalUrl());

$page = new MarkdownPage('foo', ['canonicalUrl' => 'foo']);
$this->assertSame('foo', $page->getCanonicalUrl());
}
}
14 changes: 13 additions & 1 deletion tests/Unit/Pages/MarkdownPostUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,18 @@ public function testSave()

public function testGetCanonicalUrl()
{
$this->markTestSkipped('Not yet implemented');
$page = new MarkdownPost('foo');
$this->assertNull($page->getCanonicalUrl());

self::mockConfig(['hyde.url' => 'https://example.com']);

$this->assertSame('https://example.com/posts/foo.html', $page->getCanonicalUrl());

self::mockConfig(['hyde.url' => 'https://example.com', 'hyde.pretty_urls' => true]);

$this->assertSame('https://example.com/posts/foo', $page->getCanonicalUrl());

$page = new MarkdownPost('foo', ['canonicalUrl' => 'foo']);
$this->assertSame('foo', $page->getCanonicalUrl());
}
}

0 comments on commit bc31435

Please sign in to comment.