Skip to content

Commit d6cbf19

Browse files
committed
Merge branch 'master' into support-setting-root-page-meta-description-in-front-matter
2 parents 4eaea62 + c4f4834 commit d6cbf19

File tree

9 files changed

+115
-2
lines changed

9 files changed

+115
-2
lines changed

docs/_data/partials/hyde-pages-api/hyde-page-methods.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<section id="hyde-page-methods">
22

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

66
#### `make()`
77

@@ -289,7 +289,9 @@ $page->navigationMenuGroup(): string
289289

290290
#### `getCanonicalUrl()`
291291

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

294296
```php
295297
$page->getCanonicalUrl(): string

packages/framework/src/Pages/Concerns/HydePage.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,13 @@ public function navigationMenuGroup(): ?string
397397
return $this->navigation->group;
398398
}
399399

400+
/**
401+
* Get the canonical URL for the page to use in the `<link rel="canonical">` tag.
402+
*
403+
* It can be explicitly set in the front matter using the `canonicalUrl` key,
404+
* otherwise it will be generated based on the site URL and the output path,
405+
* unless there is no configured base URL, leading to this returning null.
406+
*/
400407
public function getCanonicalUrl(): ?string
401408
{
402409
/** @var ?string $value */

packages/framework/tests/Unit/Pages/BladePageUnitTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,21 @@ public function testMatter()
179179
{
180180
$this->assertInstanceOf(FrontMatter::class, (new BladePage('foo'))->matter());
181181
}
182+
183+
public function testGetCanonicalUrl()
184+
{
185+
$page = new BladePage('foo');
186+
$this->assertNull($page->getCanonicalUrl());
187+
188+
self::mockConfig(['hyde.url' => 'https://example.com']);
189+
190+
$this->assertSame('https://example.com/foo.html', $page->getCanonicalUrl());
191+
192+
self::mockConfig(['hyde.url' => 'https://example.com', 'hyde.pretty_urls' => true]);
193+
194+
$this->assertSame('https://example.com/foo', $page->getCanonicalUrl());
195+
196+
$page = new BladePage('foo', ['canonicalUrl' => 'foo']);
197+
$this->assertSame('foo', $page->getCanonicalUrl());
198+
}
182199
}

packages/framework/tests/Unit/Pages/DocumentationPageUnitTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,21 @@ public function testSave()
202202
$this->assertFileExists('_docs/foo.md');
203203
Filesystem::unlink('_docs/foo.md');
204204
}
205+
206+
public function testGetCanonicalUrl()
207+
{
208+
$page = new DocumentationPage('foo');
209+
$this->assertNull($page->getCanonicalUrl());
210+
211+
self::mockConfig(['hyde.url' => 'https://example.com']);
212+
213+
$this->assertSame('https://example.com/docs/foo.html', $page->getCanonicalUrl());
214+
215+
self::mockConfig(['hyde.url' => 'https://example.com', 'hyde.pretty_urls' => true]);
216+
217+
$this->assertSame('https://example.com/docs/foo', $page->getCanonicalUrl());
218+
219+
$page = new DocumentationPage('foo', ['canonicalUrl' => 'foo']);
220+
$this->assertSame('foo', $page->getCanonicalUrl());
221+
}
205222
}

packages/framework/tests/Unit/Pages/HtmlPageUnitTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,4 +217,21 @@ public function testMatter()
217217
{
218218
$this->assertInstanceOf(FrontMatter::class, (new HtmlPage('404'))->matter());
219219
}
220+
221+
public function testGetCanonicalUrl()
222+
{
223+
$page = new HtmlPage('foo');
224+
$this->assertNull($page->getCanonicalUrl());
225+
226+
self::mockConfig(['hyde.url' => 'https://example.com']);
227+
228+
$this->assertSame('https://example.com/foo.html', $page->getCanonicalUrl());
229+
230+
self::mockConfig(['hyde.url' => 'https://example.com', 'hyde.pretty_urls' => true]);
231+
232+
$this->assertSame('https://example.com/foo', $page->getCanonicalUrl());
233+
234+
$page = new HtmlPage('foo', ['canonicalUrl' => 'foo']);
235+
$this->assertSame('foo', $page->getCanonicalUrl());
236+
}
220237
}

packages/framework/tests/Unit/Pages/InMemoryPageUnitTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,4 +221,21 @@ public function testMatter()
221221
{
222222
$this->assertInstanceOf(FrontMatter::class, (new InMemoryPage('404'))->matter());
223223
}
224+
225+
public function testGetCanonicalUrl()
226+
{
227+
$page = new InMemoryPage('foo');
228+
$this->assertNull($page->getCanonicalUrl());
229+
230+
self::mockConfig(['hyde.url' => 'https://example.com']);
231+
232+
$this->assertSame('https://example.com/foo.html', $page->getCanonicalUrl());
233+
234+
self::mockConfig(['hyde.url' => 'https://example.com', 'hyde.pretty_urls' => true]);
235+
236+
$this->assertSame('https://example.com/foo', $page->getCanonicalUrl());
237+
238+
$page = new InMemoryPage('foo', ['canonicalUrl' => 'foo']);
239+
$this->assertSame('foo', $page->getCanonicalUrl());
240+
}
224241
}

packages/framework/tests/Unit/Pages/MarkdownPageUnitTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,21 @@ public function testSave()
230230
$this->assertFileExists('_pages/foo.md');
231231
Filesystem::unlink('_pages/foo.md');
232232
}
233+
234+
public function testGetCanonicalUrl()
235+
{
236+
$page = new MarkdownPage('foo');
237+
$this->assertNull($page->getCanonicalUrl());
238+
239+
self::mockConfig(['hyde.url' => 'https://example.com']);
240+
241+
$this->assertSame('https://example.com/foo.html', $page->getCanonicalUrl());
242+
243+
self::mockConfig(['hyde.url' => 'https://example.com', 'hyde.pretty_urls' => true]);
244+
245+
$this->assertSame('https://example.com/foo', $page->getCanonicalUrl());
246+
247+
$page = new MarkdownPage('foo', ['canonicalUrl' => 'foo']);
248+
$this->assertSame('foo', $page->getCanonicalUrl());
249+
}
233250
}

packages/framework/tests/Unit/Pages/MarkdownPostUnitTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,21 @@ public function testSave()
230230
$this->assertFileExists('_posts/foo.md');
231231
Filesystem::unlink('_posts/foo.md');
232232
}
233+
234+
public function testGetCanonicalUrl()
235+
{
236+
$page = new MarkdownPost('foo');
237+
$this->assertNull($page->getCanonicalUrl());
238+
239+
self::mockConfig(['hyde.url' => 'https://example.com']);
240+
241+
$this->assertSame('https://example.com/posts/foo.html', $page->getCanonicalUrl());
242+
243+
self::mockConfig(['hyde.url' => 'https://example.com', 'hyde.pretty_urls' => true]);
244+
245+
$this->assertSame('https://example.com/posts/foo', $page->getCanonicalUrl());
246+
247+
$page = new MarkdownPost('foo', ['canonicalUrl' => 'foo']);
248+
$this->assertSame('foo', $page->getCanonicalUrl());
249+
}
233250
}

packages/testing/src/Common/BaseHydePageUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,6 @@ abstract public function testCompile();
113113
abstract public function testMatter();
114114

115115
abstract public function testOutputPath();
116+
117+
abstract public function testGetCanonicalUrl();
116118
}

0 commit comments

Comments
 (0)