Skip to content

Commit

Permalink
Merge branch 'master' into realtime-compiler-dashboard-improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Oct 21, 2023
2 parents f57c822 + 5d3b6f4 commit 33684cc
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 10 deletions.
8 changes: 6 additions & 2 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -28,6 +27,7 @@ class CreatesNewMarkdownPostFile
protected string $author;
protected string $date;
protected string $identifier;
protected ?string $customContent;

/**
* Construct the class.
Expand All @@ -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);
}

Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,14 +39,17 @@ 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;

$this->title = $this->parseTitle($title);
$this->filename = $this->fileName($title);
$this->force = $force;
$this->customContent = $customContent;

$this->outputPath = $this->makeOutputPath($pageClass);
}
Expand Down Expand Up @@ -100,7 +105,7 @@ protected function createBladeFile(): void
@php(\$title = "$this->title")
<main class="mx-auto max-w-7xl py-16 px-8">
<h1 class="text-center text-3xl font-bold">$this->title</h1>
{$this->getBladePageContent()}
</main>
@endsection
Expand All @@ -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
Expand All @@ -137,4 +142,18 @@ protected function failIfFileCannotBeSaved(string $path): void
throw new FileConflictException($path);
}
}

protected function getBladePageContent(): string
{
$baseContent = "<h1 class=\"text-center text-3xl font-bold\">$this->title</h1>";

return $this->customContent
? trim(sprintf("%s\n\n <div>\n %s\n </div>", $baseContent, $this->customContent))
: $baseContent;
}

protected function getMarkdownPageContent(): string
{
return trim(sprintf("# $this->title\n\n%s", $this->customContent ?? ''));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
<main class="mx-auto max-w-7xl py-16 px-8">
<h1 class="text-center text-3xl font-bold">Test Page</h1>
<div>
Hello World!
</div>
</main>
@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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

/**
* @covers \Hyde\Console\Commands\MakePageCommand
* @covers \Hyde\Framework\Actions\CreatesNewPageSourceFile
*/
class MakePageCommandTest extends TestCase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
104 changes: 104 additions & 0 deletions packages/framework/tests/Unit/CreatesNewMarkdownPostFileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

declare(strict_types=1);

namespace Hyde\Framework\Testing\Unit;

use Hyde\Hyde;
use Hyde\Testing\TestCase;
use Illuminate\Support\Carbon;
use Hyde\Framework\Actions\CreatesNewMarkdownPostFile;

/**
* @covers \Hyde\Framework\Actions\CreatesNewMarkdownPostFile
*
* @see \Hyde\Framework\Testing\Feature\Commands\MakePostCommandTest
*/
class CreatesNewMarkdownPostFileTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

Carbon::setTestNow(Carbon::create(2024));
}

public function testWithDefaultData()
{
$action = new CreatesNewMarkdownPostFile('Example Title', null, null, null);

$this->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']);
}
}

0 comments on commit 33684cc

Please sign in to comment.