Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup the canonical URL helper method #1885

Merged
merged 4 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions docs/_data/partials/hyde-pages-api/hyde-page-methods.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<section id="hyde-page-methods">

<!-- Start generated docs for Hyde\Pages\Concerns\HydePage -->
<!-- Generated by HydePHP DocGen script at 2024-07-08 20:48:42 in 5.42ms -->
<!-- Generated by HydePHP DocGen script at 2024-07-23 15:23:26 in 4.01ms -->

#### `make()`

Expand Down Expand Up @@ -289,7 +289,9 @@ $page->navigationMenuGroup(): string

#### `getCanonicalUrl()`

No description provided.
Get the canonical URL for the page to use in the `<link rel=&quot;canonical&quot;>` 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.

```php
$page->getCanonicalUrl(): string
Expand Down
7 changes: 7 additions & 0 deletions packages/framework/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 packages/framework/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 packages/framework/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 packages/framework/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 packages/framework/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 packages/framework/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 packages/framework/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());
}
}
2 changes: 2 additions & 0 deletions packages/testing/src/Common/BaseHydePageUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,6 @@ abstract public function testCompile();
abstract public function testMatter();

abstract public function testOutputPath();

abstract public function testGetCanonicalUrl();
}
Loading