diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index e3d1b60364c..6bca2c46ffe 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -10,7 +10,8 @@ This serves two purposes: 2. At release time, you can move the Unreleased section changes into a new release version section. ### Added -- for new features. +- Added support for setting custom content when calling source file creator actions directly in https://github.com/hydephp/develop/pull/1393 +- Added support for setting a custom post date when calling post file creator action directly in https://github.com/hydephp/develop/pull/1393 ### Changed - for changes in existing functionality. @@ -22,7 +23,10 @@ This serves two purposes: - for now removed features. ### Fixed -- for any bug fixes. +- Catch RealtimeCompiler dashboard OutOfBoundsException in https://github.com/hydephp/develop/pull/1384 +- Updated dropdown navigation menus to support setting priority in config in https://github.com/hydephp/develop/pull/1387 (fixing https://github.com/hydephp/hyde/issues/229) +- Updated the vendor publish command to support parent Laravel Prompts implementation in https://github.com/hydephp/develop/pull/1388 +- Fixed wrong version constant in https://github.com/hydephp/develop/pull/1391 ### Security - in case of vulnerabilities. diff --git a/packages/framework/src/Framework/Actions/CreatesNewMarkdownPostFile.php b/packages/framework/src/Framework/Actions/CreatesNewMarkdownPostFile.php index c59383eed74..3f1f4ea9625 100644 --- a/packages/framework/src/Framework/Actions/CreatesNewMarkdownPostFile.php +++ b/packages/framework/src/Framework/Actions/CreatesNewMarkdownPostFile.php @@ -7,10 +7,9 @@ use Hyde\Framework\Exceptions\FileConflictException; use Hyde\Facades\Filesystem; use Hyde\Pages\MarkdownPost; +use Illuminate\Support\Carbon; use Illuminate\Support\Str; -use function date; - /** * Offloads logic for the make:post command. * @@ -28,6 +27,7 @@ class CreatesNewMarkdownPostFile protected string $author; protected string $date; protected string $identifier; + protected ?string $customContent; /** * Construct the class. @@ -36,15 +36,18 @@ class CreatesNewMarkdownPostFile * @param string|null $description The Post Meta Description. * @param string|null $category The Primary Post Category. * @param string|null $author The Username of the Author. + * @param string|null $date Optionally specify a custom date. + * @param string|null $customContent Optionally specify custom post content. */ - public function __construct(string $title, ?string $description, ?string $category, ?string $author) + public function __construct(string $title, ?string $description, ?string $category, ?string $author, ?string $date = null, ?string $customContent = null) { $this->title = $title; $this->description = $description ?? 'A short description used in previews and SEO'; $this->category = $category ?? 'blog'; $this->author = $author ?? 'default'; + $this->customContent = $customContent; - $this->date = date('Y-m-d H:i'); + $this->date = Carbon::make($date ?? Carbon::now())->format('Y-m-d H:i'); $this->identifier = Str::slug($title); } @@ -58,7 +61,7 @@ public function __construct(string $title, ?string $description, ?string $catego */ public function save(bool $force = false): string { - $page = new MarkdownPost($this->identifier, $this->toArray(), '## Write something awesome.'); + $page = new MarkdownPost($this->identifier, $this->toArray(), $this->customContent ?? '## Write something awesome.'); if ($force !== true && Filesystem::exists($page->getSourcePath())) { throw new FileConflictException($page->getSourcePath()); diff --git a/packages/framework/src/Framework/Actions/CreatesNewPageSourceFile.php b/packages/framework/src/Framework/Actions/CreatesNewPageSourceFile.php index 13cf19b2fbc..60ff6686f92 100644 --- a/packages/framework/src/Framework/Actions/CreatesNewPageSourceFile.php +++ b/packages/framework/src/Framework/Actions/CreatesNewPageSourceFile.php @@ -13,6 +13,8 @@ use Hyde\Framework\Concerns\InteractsWithDirectories; use Hyde\Framework\Exceptions\UnsupportedPageTypeException; +use function trim; +use function sprintf; use function file_put_contents; use function file_exists; use function basename; @@ -37,7 +39,9 @@ class CreatesNewPageSourceFile protected string $subDir = ''; protected bool $force; - public function __construct(string $title, string $pageClass = MarkdownPage::class, bool $force = false) + protected ?string $customContent; + + public function __construct(string $title, string $pageClass = MarkdownPage::class, bool $force = false, ?string $customContent = null) { $this->validateType($pageClass); $this->pageClass = $pageClass; @@ -45,6 +49,7 @@ public function __construct(string $title, string $pageClass = MarkdownPage::cla $this->title = $this->parseTitle($title); $this->filename = $this->fileName($title); $this->force = $force; + $this->customContent = $customContent; $this->outputPath = $this->makeOutputPath($pageClass); } @@ -100,7 +105,7 @@ protected function createBladeFile(): void @php(\$title = "$this->title")
-

$this->title

+ {$this->getBladePageContent()}
@endsection @@ -111,7 +116,7 @@ protected function createBladeFile(): void protected function createMarkdownFile(): void { - (new MarkdownPage($this->formatIdentifier(), ['title' => $this->title], "# $this->title"))->save(); + (new MarkdownPage($this->formatIdentifier(), ['title' => $this->title], $this->getMarkdownPageContent()))->save(); } protected function createDocumentationFile(): void @@ -137,4 +142,18 @@ protected function failIfFileCannotBeSaved(string $path): void throw new FileConflictException($path); } } + + protected function getBladePageContent(): string + { + $baseContent = "

$this->title

"; + + return $this->customContent + ? trim(sprintf("%s\n\n
\n %s\n
", $baseContent, $this->customContent)) + : $baseContent; + } + + protected function getMarkdownPageContent(): string + { + return trim(sprintf("# $this->title\n\n%s", $this->customContent ?? '')); + } } diff --git a/packages/framework/tests/Feature/Actions/CreatesNewPageSourceFileTest.php b/packages/framework/tests/Feature/Actions/CreatesNewPageSourceFileTest.php index e278885f52d..4b32145f30e 100644 --- a/packages/framework/tests/Feature/Actions/CreatesNewPageSourceFileTest.php +++ b/packages/framework/tests/Feature/Actions/CreatesNewPageSourceFileTest.php @@ -133,6 +133,57 @@ public function test_that_a_documentation_file_can_be_created_and_contains_expec Filesystem::unlink('_docs/test-page.md'); } + public function test_that_a_markdown_file_can_be_created_with_custom_content() + { + (new CreatesNewPageSourceFile('Test Page', customContent: 'Hello World!'))->save(); + + $this->assertFileExists(Hyde::path('_pages/test-page.md')); + + $this->assertSame( + <<<'MARKDOWN' + --- + title: 'Test Page' + --- + + # Test Page + + Hello World! + + MARKDOWN + , + file_get_contents(Hyde::path('_pages/test-page.md')) + ); + Filesystem::unlink('_pages/test-page.md'); + } + + public function test_that_a_blade_file_can_be_created_with_custom_content() + { + (new CreatesNewPageSourceFile('Test Page', BladePage::class, customContent: 'Hello World!'))->save(); + + $this->assertFileExists(Hyde::path('_pages/test-page.blade.php')); + + $this->assertEquals( + <<<'BLADE' + @extends('hyde::layouts.app') + @section('content') + @php($title = "Test Page") + +
+

Test Page

+ +
+ Hello World! +
+
+ + @endsection + + BLADE, file_get_contents(Hyde::path('_pages/test-page.blade.php')) + ); + + Filesystem::unlink('_pages/test-page.blade.php'); + } + public function test_that_the_file_path_can_be_returned() { $this->assertSame( diff --git a/packages/framework/tests/Feature/Commands/MakePageCommandTest.php b/packages/framework/tests/Feature/Commands/MakePageCommandTest.php index 795822c2f82..bae51389d1b 100644 --- a/packages/framework/tests/Feature/Commands/MakePageCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePageCommandTest.php @@ -12,6 +12,7 @@ /** * @covers \Hyde\Console\Commands\MakePageCommand + * @covers \Hyde\Framework\Actions\CreatesNewPageSourceFile */ class MakePageCommandTest extends TestCase { diff --git a/packages/framework/tests/Feature/Commands/MakePostCommandTest.php b/packages/framework/tests/Feature/Commands/MakePostCommandTest.php index 91da9a9f87a..f275a9e010d 100644 --- a/packages/framework/tests/Feature/Commands/MakePostCommandTest.php +++ b/packages/framework/tests/Feature/Commands/MakePostCommandTest.php @@ -8,6 +8,10 @@ use Hyde\Hyde; use Hyde\Testing\TestCase; +/** + * @covers \Hyde\Console\Commands\MakePostCommand + * @covers \Hyde\Framework\Actions\CreatesNewMarkdownPostFile + */ class MakePostCommandTest extends TestCase { public function test_command_has_expected_output_and_creates_valid_file() diff --git a/packages/framework/tests/Unit/CreatesNewMarkdownPostFileTest.php b/packages/framework/tests/Unit/CreatesNewMarkdownPostFileTest.php new file mode 100644 index 00000000000..ef8898781a9 --- /dev/null +++ b/packages/framework/tests/Unit/CreatesNewMarkdownPostFileTest.php @@ -0,0 +1,104 @@ +assertSame([ + 'title' => 'Example Title', + 'description' => 'A short description used in previews and SEO', + 'category' => 'blog', + 'author' => 'default', + 'date' => '2024-01-01 00:00', + ], $action->toArray()); + } + + public function testWithCustomData() + { + $action = new CreatesNewMarkdownPostFile('foo', 'bar', 'baz', 'qux', '2024-06-01 12:20'); + + $this->assertSame([ + 'title' => 'foo', + 'description' => 'bar', + 'category' => 'baz', + 'author' => 'qux', + 'date' => '2024-06-01 12:20', + ], $action->toArray()); + } + + public function testSave() + { + $action = new CreatesNewMarkdownPostFile('Example Post', null, null, null); + $action->save(); + + $path = Hyde::path('_posts/example-post.md'); + + $this->assertSame(<<<'MARKDOWN' + --- + title: 'Example Post' + description: 'A short description used in previews and SEO' + category: blog + author: default + date: '2024-01-01 00:00' + --- + + ## Write something awesome. + + MARKDOWN, file_get_contents($path)); + + unlink($path); + } + + public function testSaveWithCustomContent() + { + $action = new CreatesNewMarkdownPostFile('Example Post', null, null, null, null, 'Hello World!'); + $action->save(); + + $path = Hyde::path('_posts/example-post.md'); + + $this->assertSame(<<<'MARKDOWN' + --- + title: 'Example Post' + description: 'A short description used in previews and SEO' + category: blog + author: default + date: '2024-01-01 00:00' + --- + + Hello World! + + MARKDOWN, file_get_contents($path)); + + unlink($path); + } + + public function testCustomDateNormalisation() + { + $action = new CreatesNewMarkdownPostFile('Example Post', null, null, null, 'Jan 1 2024 8am'); + + $this->assertSame('2024-01-01 08:00', $action->toArray()['date']); + } +}