-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2000 from hydephp/support-date-prefixes-in-blog-p…
…osts [2.x] Support date prefixes in blog posts
- Loading branch information
Showing
7 changed files
with
375 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
packages/framework/src/Framework/Features/Blogging/BlogPostDatePrefixHelper.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Hyde\Framework\Features\Blogging; | ||
|
||
use DateTime; | ||
use DateTimeInterface; | ||
use InvalidArgumentException; | ||
|
||
/** | ||
* @internal Helper class for handling date prefixes in blog post filenames | ||
*/ | ||
class BlogPostDatePrefixHelper | ||
{ | ||
/** | ||
* We accept ISO 8601 dates in the format 'YYYY-MM-DD' and optionally a time in the format 'HH-MM', separated by a hyphen. | ||
* | ||
* @var string The regular expression pattern for matching a date prefix in a filename | ||
*/ | ||
protected const DATE_PATTERN = '/^(\d{4}-\d{2}-\d{2})(?:-(\d{2}-\d{2}))?-/'; | ||
|
||
public static function hasDatePrefix(string $filepath): bool | ||
{ | ||
return preg_match(static::DATE_PATTERN, basename($filepath)) === 1; | ||
} | ||
|
||
public static function extractDate(string $filepath): DateTimeInterface | ||
{ | ||
if (! preg_match(static::DATE_PATTERN, basename($filepath), $matches)) { | ||
throw new InvalidArgumentException('The given filepath does not contain a valid ISO 8601 date prefix.'); | ||
} | ||
|
||
$dateString = $matches[1]; | ||
|
||
if (isset($matches[2])) { | ||
$dateString .= ' '.str_replace('-', ':', $matches[2]); | ||
} | ||
|
||
return new DateTime($dateString); | ||
} | ||
|
||
public static function stripDatePrefix(string $filepath): string | ||
{ | ||
return preg_replace(static::DATE_PATTERN, '', basename($filepath)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
packages/framework/tests/Feature/BlogPostDatePrefixHelperTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Hyde\Framework\Testing\Feature; | ||
|
||
use Hyde\Support\Models\DateString; | ||
use Hyde\Framework\Features\Blogging\BlogPostDatePrefixHelper; | ||
use Hyde\Pages\MarkdownPost; | ||
use Hyde\Testing\TestCase; | ||
|
||
/** | ||
* High level test for the feature that allows dates to be set using prefixes in blog post filenames. | ||
* | ||
* @covers \Hyde\Framework\Features\Blogging\BlogPostDatePrefixHelper | ||
* @covers \Hyde\Framework\Factories\BlogPostDataFactory | ||
* @covers \Hyde\Support\Models\RouteKey | ||
* | ||
* @see \Hyde\Framework\Testing\Unit\BlogPostDatePrefixHelperUnitTest | ||
*/ | ||
class BlogPostDatePrefixHelperTest extends TestCase | ||
{ | ||
public function testCanDetectDatePrefix() | ||
{ | ||
$this->assertTrue(BlogPostDatePrefixHelper::hasDatePrefix('2024-11-05-my-post.md')); | ||
$this->assertTrue(BlogPostDatePrefixHelper::hasDatePrefix('2024-11-05-10-30-my-post.md')); | ||
$this->assertFalse(BlogPostDatePrefixHelper::hasDatePrefix('my-post.md')); | ||
} | ||
|
||
public function testCanExtractDateFromPrefix() | ||
{ | ||
$date = BlogPostDatePrefixHelper::extractDate('2024-11-05-my-post.md'); | ||
$this->assertNotNull($date); | ||
$this->assertSame('2024-11-05', $date->format('Y-m-d')); | ||
|
||
$date = BlogPostDatePrefixHelper::extractDate('2024-11-05-10-30-my-post.md'); | ||
$this->assertNotNull($date); | ||
$this->assertSame('2024-11-05 10:30', $date->format('Y-m-d H:i')); | ||
} | ||
|
||
public function testCanGetDateFromBlogPostFilename() | ||
{ | ||
$this->file('_posts/2024-11-05-my-post.md', '# Hello World'); | ||
$post = MarkdownPost::parse('2024-11-05-my-post'); | ||
|
||
$this->assertInstanceOf(DateString::class, $post->date); | ||
$this->assertSame('2024-11-05 00:00', $post->date->string); | ||
} | ||
|
||
public function testCanGetDateFromBlogPostFilenameWithTime() | ||
{ | ||
$this->file('_posts/2024-11-05-10-30-my-post.md', '# Hello World'); | ||
$post = MarkdownPost::parse('2024-11-05-10-30-my-post'); | ||
|
||
$this->assertInstanceOf(DateString::class, $post->date); | ||
$this->assertSame('2024-11-05 10:30', $post->date->string); | ||
} | ||
|
||
public function testDatePrefixIsStrippedFromRouteKey() | ||
{ | ||
$this->file('_posts/2024-11-05-my-post.md', '# Hello World'); | ||
$post = MarkdownPost::parse('2024-11-05-my-post'); | ||
|
||
$this->assertSame('posts/my-post', $post->getRouteKey()); | ||
} | ||
|
||
public function testDateFromPrefixIsUsedWhenNoFrontMatterDate() | ||
{ | ||
$this->file('_posts/2024-11-05-my-post.md', '# Hello World'); | ||
$post = MarkdownPost::parse('2024-11-05-my-post'); | ||
|
||
$this->assertSame('2024-11-05 00:00', $post->date->string); | ||
} | ||
|
||
public function testFrontMatterDateTakesPrecedenceOverPrefix() | ||
{ | ||
$this->file('_posts/2024-11-05-my-post.md', <<<'MD' | ||
--- | ||
date: "2024-12-25" | ||
--- | ||
# Hello World | ||
MD); | ||
|
||
$post = MarkdownPost::parse('2024-11-05-my-post'); | ||
$this->assertSame('2024-12-25', $post->date->string); | ||
} | ||
} |
Oops, something went wrong.