Skip to content

Commit

Permalink
Merge pull request #1885 from hydephp/cleanup-canonical-page-helper-m…
Browse files Browse the repository at this point in the history
…ethod

Cleanup the canonical URL helper method hydephp/develop@c4f4834
  • Loading branch information
github-actions committed Jul 23, 2024
1 parent ef20271 commit 69f8b7b
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 0 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
17 changes: 17 additions & 0 deletions tests/Unit/Pages/BladePageUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,21 @@ public function testMatter()
{
$this->assertInstanceOf(FrontMatter::class, (new BladePage('foo'))->matter());
}

public function testGetCanonicalUrl()
{
$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());
}
}
17 changes: 17 additions & 0 deletions tests/Unit/Pages/DocumentationPageUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,21 @@ public function testSave()
$this->assertFileExists('_docs/foo.md');
Filesystem::unlink('_docs/foo.md');
}

public function testGetCanonicalUrl()
{
$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());
}
}
17 changes: 17 additions & 0 deletions tests/Unit/Pages/HtmlPageUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,21 @@ public function testMatter()
{
$this->assertInstanceOf(FrontMatter::class, (new HtmlPage('404'))->matter());
}

public function testGetCanonicalUrl()
{
$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());
}
}
17 changes: 17 additions & 0 deletions tests/Unit/Pages/InMemoryPageUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,21 @@ public function testMatter()
{
$this->assertInstanceOf(FrontMatter::class, (new InMemoryPage('404'))->matter());
}

public function testGetCanonicalUrl()
{
$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());
}
}
17 changes: 17 additions & 0 deletions tests/Unit/Pages/MarkdownPageUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,21 @@ public function testSave()
$this->assertFileExists('_pages/foo.md');
Filesystem::unlink('_pages/foo.md');
}

public function testGetCanonicalUrl()
{
$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());
}
}
17 changes: 17 additions & 0 deletions tests/Unit/Pages/MarkdownPostUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,21 @@ public function testSave()
$this->assertFileExists('_posts/foo.md');
Filesystem::unlink('_posts/foo.md');
}

public function testGetCanonicalUrl()
{
$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 69f8b7b

Please sign in to comment.