From c1a1a7070e826f93602eac0a9f2d116a1de5b529 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 21 Oct 2023 17:17:04 +0200 Subject: [PATCH] Support custom content in post creator action --- .../Actions/CreatesNewMarkdownPostFile.php | 6 +++-- .../Unit/CreatesNewMarkdownPostFileTest.php | 23 +++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/packages/framework/src/Framework/Actions/CreatesNewMarkdownPostFile.php b/packages/framework/src/Framework/Actions/CreatesNewMarkdownPostFile.php index 869b6b381d0..576f9b9d522 100644 --- a/packages/framework/src/Framework/Actions/CreatesNewMarkdownPostFile.php +++ b/packages/framework/src/Framework/Actions/CreatesNewMarkdownPostFile.php @@ -27,6 +27,7 @@ class CreatesNewMarkdownPostFile protected string $author; protected string $date; protected string $identifier; + protected ?string $customContent; /** * Construct the class. @@ -36,12 +37,13 @@ class CreatesNewMarkdownPostFile * @param string|null $category The Primary Post Category. * @param string|null $author The Username of the Author. */ - public function __construct(string $title, ?string $description, ?string $category, ?string $author) + public function __construct(string $title, ?string $description, ?string $category, ?string $author, ?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 = Carbon::now()->format('Y-m-d H:i'); $this->identifier = Str::slug($title); @@ -57,7 +59,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/tests/Unit/CreatesNewMarkdownPostFileTest.php b/packages/framework/tests/Unit/CreatesNewMarkdownPostFileTest.php index 6f81f01dab5..b6274d87b3a 100644 --- a/packages/framework/tests/Unit/CreatesNewMarkdownPostFileTest.php +++ b/packages/framework/tests/Unit/CreatesNewMarkdownPostFileTest.php @@ -73,4 +73,27 @@ public function testSave() unlink($path); } + + public function testSaveWithCustomContent() + { + $action = new CreatesNewMarkdownPostFile('Example Post', 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); + } }