From 9d1134b8b967214fd73162860b66bc2a89079056 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 17 May 2022 19:30:06 +0200 Subject: [PATCH 01/14] Create a helper for fluent metadata configuration --- config/hyde.php | 25 +++++++-------- resources/views/layouts/meta.blade.php | 9 +----- src/Meta.php | 43 ++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 21 deletions(-) create mode 100644 src/Meta.php diff --git a/config/hyde.php b/config/hyde.php index 85f7c20a..b6bfa784 100644 --- a/config/hyde.php +++ b/config/hyde.php @@ -18,6 +18,7 @@ */ use Hyde\Framework\Features; +use Hyde\Framework\Meta; return [ @@ -70,24 +71,22 @@ | Global Site Meta Tags |-------------------------------------------------------------------------- | - | While you can add any number of meta tags in the meta.blade.php component, - | these settings allow you to easily add tags for the meta component. + | While you can add any number of meta tags in the meta.blade.php component + | using standard HTML, you can also use the Meta helper. To add a regular + | meta tag, use Meta::name() helper. To add an Open Graph property, use + | Meta::property() helper which also adds the `og:` prefix for you. | - | The `meta` array is for standard meta tags. See the examples below. - | The `og` array is for Open Graph properties. Do not include the `og:` prefix. + | Note that these global tags may be overridden by a page's meta tags. | */ 'meta' => [ - // 'author' => 'Mr. Hyde', - // 'twitter:creator' => '@hyde_php', - // 'description' => 'My Hyde Blog', - // 'keywords' => 'Static Sites, Blogs, Documentation', - 'generator' => 'HydePHP '.Hyde\Framework\Hyde::version(), - ], - - 'ogProperties' => [ - 'site_name' => $siteName, + Meta::name('author', 'Mr. Hyde'), + Meta::name('twitter:creator', '@hyde_php'), + Meta::name('description', 'My Hyde Blog'), + Meta::name('keywords', 'Static Sites, Blogs, Documentation'), + Meta::name('generator', 'HydePHP '.Hyde\Framework\Hyde::version()), + Meta::property('site_name', $siteName), ], /* diff --git a/resources/views/layouts/meta.blade.php b/resources/views/layouts/meta.blade.php index 15cf6d2b..6795640c 100644 --- a/resources/views/layouts/meta.blade.php +++ b/resources/views/layouts/meta.blade.php @@ -1,12 +1,5 @@ {{-- Config Defined Meta Tags --}} -@foreach (config('hyde.meta', []) as $name => $content) - -@endforeach - -@foreach (config('hyde.ogProperties', []) as $property => $content) - -@endforeach +{!! Hyde\Framework\Meta::render() !!} {{-- Add any extra tags to include in the section --}} @stack('meta') - diff --git a/src/Meta.php b/src/Meta.php new file mode 100644 index 00000000..49b124e2 --- /dev/null +++ b/src/Meta.php @@ -0,0 +1,43 @@ +'; + } + return null; + } + + public static function property(string $property, string $content, bool $ifConditionIsMet = true): ?string + { + if ($ifConditionIsMet) { + $property = static::formatOpenGraphProperty($property); + return ''; + } + return null; + } + + public static function render(array $overridesGlobalMeta = []): string + { + return implode("\n", + array_merge( + static::getGlobalMeta(), + $overridesGlobalMeta + ) + ); + } + + public static function getGlobalMeta(): array + { + return config('hyde.meta', []); + } + + protected static function formatOpenGraphProperty(string $property): string + { + return str_starts_with('og:', $property) ? $property : 'og:' . $property; + } +} \ No newline at end of file From 4b355326479b4641e1d68a0c994563206fbec6ca Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Tue, 17 May 2022 17:30:32 +0000 Subject: [PATCH 02/14] Apply fixes from StyleCI --- src/Meta.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Meta.php b/src/Meta.php index 49b124e2..1d51c263 100644 --- a/src/Meta.php +++ b/src/Meta.php @@ -7,8 +7,9 @@ class Meta public static function name(string $name, string $content, bool $ifConditionIsMet = true): ?string { if ($ifConditionIsMet) { - return ''; + return ''; } + return null; } @@ -16,8 +17,10 @@ public static function property(string $property, string $content, bool $ifCondi { if ($ifConditionIsMet) { $property = static::formatOpenGraphProperty($property); - return ''; + + return ''; } + return null; } @@ -38,6 +41,6 @@ public static function getGlobalMeta(): array protected static function formatOpenGraphProperty(string $property): string { - return str_starts_with('og:', $property) ? $property : 'og:' . $property; + return str_starts_with('og:', $property) ? $property : 'og:'.$property; } -} \ No newline at end of file +} From 7982f9d52da316fea5b0d021c1d193ac0f0ad000 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 17 May 2022 19:38:03 +0200 Subject: [PATCH 03/14] Add array_unique --- src/Meta.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Meta.php b/src/Meta.php index 49b124e2..70fe5683 100644 --- a/src/Meta.php +++ b/src/Meta.php @@ -24,9 +24,11 @@ public static function property(string $property, string $content, bool $ifCondi public static function render(array $overridesGlobalMeta = []): string { return implode("\n", - array_merge( - static::getGlobalMeta(), - $overridesGlobalMeta + array_unique( + array_merge( + static::getGlobalMeta(), + $overridesGlobalMeta + ) ) ); } From ab21c6045e4d338b04eef4db604abc582b061ffd Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 17 May 2022 19:38:07 +0200 Subject: [PATCH 04/14] Add deprecations --- src/Concerns/HasMetadata.php | 5 +++++ src/Models/Metadata.php | 2 ++ 2 files changed, 7 insertions(+) diff --git a/src/Concerns/HasMetadata.php b/src/Concerns/HasMetadata.php index c405b687..05534b99 100644 --- a/src/Concerns/HasMetadata.php +++ b/src/Concerns/HasMetadata.php @@ -12,6 +12,8 @@ * * @see \Hyde\Framework\Models\Metadata * @see \Tests\Feature\Concerns\HasMetadataTest + * + * @todo Unify the $page property and handle metadata through it */ trait HasMetadata { @@ -46,6 +48,7 @@ public function getMetaProperties(): array /** * Generate metadata from the front matter that can be used in standard tags. + * @deprecated Will be refactored to parseFrontMatterMetadata */ protected function makeMetadata(): void { @@ -65,6 +68,7 @@ protected function makeMetadata(): void /** * Generate metadata from the front matter that can be used for og:type tags. * Note that this currently assumes that the object using it is a Blog Post. + * @deprecated Will be refactored to parseFrontMatterMetadata */ protected function makeMetaProperties(): void { @@ -99,6 +103,7 @@ protected function makeMetaProperties(): void /** * Parse the author string from the front matter with support for both flat and array notation. + * @deprecated Will be moved to the Author model * * @param string|array $author * @return string diff --git a/src/Models/Metadata.php b/src/Models/Metadata.php index 14bdb06f..f72253c3 100644 --- a/src/Models/Metadata.php +++ b/src/Models/Metadata.php @@ -7,6 +7,8 @@ /** * Metadata class for storing metadata about a model. * Is used in Blade views to create tags. + * + * @deprecated Will be merged with Meta class */ class Metadata { From 90e9edcd50b2777181ed1859e55fbbcc36eb591f Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Tue, 17 May 2022 17:38:51 +0000 Subject: [PATCH 05/14] Apply fixes from StyleCI --- src/Concerns/HasMetadata.php | 5 ++++- src/Models/Metadata.php | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Concerns/HasMetadata.php b/src/Concerns/HasMetadata.php index 05534b99..404f564c 100644 --- a/src/Concerns/HasMetadata.php +++ b/src/Concerns/HasMetadata.php @@ -12,7 +12,7 @@ * * @see \Hyde\Framework\Models\Metadata * @see \Tests\Feature\Concerns\HasMetadataTest - * + * * @todo Unify the $page property and handle metadata through it */ trait HasMetadata @@ -48,6 +48,7 @@ public function getMetaProperties(): array /** * Generate metadata from the front matter that can be used in standard tags. + * * @deprecated Will be refactored to parseFrontMatterMetadata */ protected function makeMetadata(): void @@ -68,6 +69,7 @@ protected function makeMetadata(): void /** * Generate metadata from the front matter that can be used for og:type tags. * Note that this currently assumes that the object using it is a Blog Post. + * * @deprecated Will be refactored to parseFrontMatterMetadata */ protected function makeMetaProperties(): void @@ -103,6 +105,7 @@ protected function makeMetaProperties(): void /** * Parse the author string from the front matter with support for both flat and array notation. + * * @deprecated Will be moved to the Author model * * @param string|array $author diff --git a/src/Models/Metadata.php b/src/Models/Metadata.php index f72253c3..27c48619 100644 --- a/src/Models/Metadata.php +++ b/src/Models/Metadata.php @@ -7,7 +7,7 @@ /** * Metadata class for storing metadata about a model. * Is used in Blade views to create tags. - * + * * @deprecated Will be merged with Meta class */ class Metadata From 683a5a79e37168824d82ff40bf62c39a0e2aefd4 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 17 May 2022 20:14:39 +0200 Subject: [PATCH 06/14] Fix #382: Unify the $page property --- resources/views/components/post/article.blade.php | 14 +++++++------- resources/views/components/post/author.blade.php | 8 ++++---- resources/views/components/post/category.blade.php | 2 +- resources/views/components/post/date.blade.php | 2 +- .../views/components/post/description.blade.php | 2 +- resources/views/components/post/image.blade.php | 6 +++--- resources/views/layouts/post.blade.php | 4 ++-- src/StaticPageBuilder.php | 6 ++++-- 8 files changed, 23 insertions(+), 21 deletions(-) diff --git a/resources/views/components/post/article.blade.php b/resources/views/components/post/article.blade.php index 296de585..f169945b 100644 --- a/resources/views/components/post/article.blade.php +++ b/resources/views/components/post/article.blade.php @@ -1,19 +1,19 @@ -
Hyde\Framework\Features::hasTorchlight()])> - + @if(Hyde::uriPath()) - + @endif

{{ $title ?? 'Blog Post' }}

- @includeWhen(isset($post->date), 'hyde::components.post.date') - @includeWhen(isset($post->author), 'hyde::components.post.author') - @includeWhen(isset($post->category), 'hyde::components.post.category') + @includeWhen(isset($page->date), 'hyde::components.post.date') + @includeWhen(isset($page->author), 'hyde::components.post.author') + @includeWhen(isset($page->category), 'hyde::components.post.category')
- @includeWhen(isset($post->image), 'hyde::components.post.image') + @includeWhen(isset($page->image), 'hyde::components.post.image')
{!! $markdown !!}
diff --git a/resources/views/components/post/author.blade.php b/resources/views/components/post/author.blade.php index 5838ff95..5455d9ef 100644 --- a/resources/views/components/post/author.blade.php +++ b/resources/views/components/post/author.blade.php @@ -1,10 +1,10 @@ by author diff --git a/resources/views/components/post/category.blade.php b/resources/views/components/post/category.blade.php index 886cf6bc..17615661 100644 --- a/resources/views/components/post/category.blade.php +++ b/resources/views/components/post/category.blade.php @@ -1 +1 @@ -in the category "{{ $post->category }}" +in the category "{{ $page->category }}" diff --git a/resources/views/components/post/date.blade.php b/resources/views/components/post/date.blade.php index cfa817ce..010d9d33 100644 --- a/resources/views/components/post/date.blade.php +++ b/resources/views/components/post/date.blade.php @@ -1 +1 @@ -Posted +Posted diff --git a/resources/views/components/post/description.blade.php b/resources/views/components/post/description.blade.php index 74cb1bc4..bebb3b6a 100644 --- a/resources/views/components/post/description.blade.php +++ b/resources/views/components/post/description.blade.php @@ -1,3 +1,3 @@

- {{ $post->matter['description'] }} + {{ $page->matter['description'] }}

\ No newline at end of file diff --git a/resources/views/components/post/image.blade.php b/resources/views/components/post/image.blade.php index d35274d0..a0fd5fee 100644 --- a/resources/views/components/post/image.blade.php +++ b/resources/views/components/post/image.blade.php @@ -1,9 +1,9 @@
- {{ $post->image->description ?? '' }} + {{ $page->image->description ?? '' }}
- {!! $post->image->getFluentAttribution() !!} + {!! $page->image->getFluentAttribution() !!}
- @foreach ($post->image->getMetadataArray() as $name => $value) + @foreach ($page->image->getMetadataArray() as $name => $value) @endforeach
diff --git a/resources/views/layouts/post.blade.php b/resources/views/layouts/post.blade.php index 8a37f227..ec3c2163 100644 --- a/resources/views/layouts/post.blade.php +++ b/resources/views/layouts/post.blade.php @@ -4,10 +4,10 @@ @push('meta') -@foreach ($post->getMetadata() as $name => $content) +@foreach ($page->getMetadata() as $name => $content) @endforeach -@foreach ($post->getMetaProperties() as $name => $content) +@foreach ($page->getMetaProperties() as $name => $content) @endforeach @endpush diff --git a/src/StaticPageBuilder.php b/src/StaticPageBuilder.php index be1e5cff..ebcb7f59 100644 --- a/src/StaticPageBuilder.php +++ b/src/StaticPageBuilder.php @@ -86,6 +86,7 @@ private function save(string $location, string $contents): bool|int private function compileView(): string { return view($this->page->view, [ + 'page' => $this->page, 'currentPage' => $this->page->view, ])->render(); } @@ -98,7 +99,7 @@ private function compileView(): string private function compilePost(): string { return view('hyde::layouts/post')->with([ - 'post' => $this->page, + 'page' => $this->page, 'title' => $this->page->title, 'markdown' => MarkdownConverter::parse($this->page->body), 'currentPage' => 'posts/'.$this->page->slug, @@ -113,6 +114,7 @@ private function compilePost(): string private function compilePage(): string { return view('hyde::layouts/page')->with([ + 'page' => $this->page, 'title' => $this->page->title, 'markdown' => MarkdownConverter::parse($this->page->body), 'currentPage' => $this->page->slug, @@ -127,7 +129,7 @@ private function compilePage(): string private function compileDocs(): string { return view('hyde::layouts/docs')->with([ - 'docs' => $this->page, + 'page' => $this->page, 'title' => $this->page->title, 'markdown' => MarkdownConverter::parse($this->page->body, DocumentationPage::class), 'currentPage' => Hyde::docsDirectory().'/'.$this->page->slug, From 8574d39c172178816578fc484aed634b8fbfe1ad Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 17 May 2022 20:24:59 +0200 Subject: [PATCH 07/14] Use view()->share to reduce boilerplate --- src/StaticPageBuilder.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/StaticPageBuilder.php b/src/StaticPageBuilder.php index ebcb7f59..fdadd21f 100644 --- a/src/StaticPageBuilder.php +++ b/src/StaticPageBuilder.php @@ -44,6 +44,8 @@ public function __construct(protected MarkdownDocument|BladePage $page, bool $se */ public function __invoke() { + view()->share('page', $this->page); + if ($this->page instanceof BladePage) { return $this->save($this->page->view, $this->compileView()); } @@ -86,7 +88,6 @@ private function save(string $location, string $contents): bool|int private function compileView(): string { return view($this->page->view, [ - 'page' => $this->page, 'currentPage' => $this->page->view, ])->render(); } @@ -99,7 +100,6 @@ private function compileView(): string private function compilePost(): string { return view('hyde::layouts/post')->with([ - 'page' => $this->page, 'title' => $this->page->title, 'markdown' => MarkdownConverter::parse($this->page->body), 'currentPage' => 'posts/'.$this->page->slug, @@ -114,7 +114,6 @@ private function compilePost(): string private function compilePage(): string { return view('hyde::layouts/page')->with([ - 'page' => $this->page, 'title' => $this->page->title, 'markdown' => MarkdownConverter::parse($this->page->body), 'currentPage' => $this->page->slug, @@ -129,7 +128,6 @@ private function compilePage(): string private function compileDocs(): string { return view('hyde::layouts/docs')->with([ - 'page' => $this->page, 'title' => $this->page->title, 'markdown' => MarkdownConverter::parse($this->page->body, DocumentationPage::class), 'currentPage' => Hyde::docsDirectory().'/'.$this->page->slug, From 2c4f28a0eb2efa4ef4e3574b089a5a557636c418 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 17 May 2022 20:25:13 +0200 Subject: [PATCH 08/14] Fix needsDirectory order --- src/StaticPageBuilder.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/StaticPageBuilder.php b/src/StaticPageBuilder.php index fdadd21f..122928e0 100644 --- a/src/StaticPageBuilder.php +++ b/src/StaticPageBuilder.php @@ -30,11 +30,11 @@ class StaticPageBuilder */ public function __construct(protected MarkdownDocument|BladePage $page, bool $selfInvoke = false) { + $this->needsDirectory(static::$outputPath); + if ($selfInvoke) { $this->__invoke(); } - - $this->needsDirectory(static::$outputPath); } /** From 15ccd271706ddf38f8011287ed28f04a60cd4076 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 17 May 2022 22:15:17 +0200 Subject: [PATCH 09/14] Add test for, and improve Meta helper --- config/hyde.php | 10 +- src/Meta.php | 44 ++++++--- tests/Feature/MetadataHelperTest.php | 142 +++++++++++++++++++++++++++ 3 files changed, 176 insertions(+), 20 deletions(-) create mode 100644 tests/Feature/MetadataHelperTest.php diff --git a/config/hyde.php b/config/hyde.php index b6bfa784..c4222178 100644 --- a/config/hyde.php +++ b/config/hyde.php @@ -76,15 +76,15 @@ | meta tag, use Meta::name() helper. To add an Open Graph property, use | Meta::property() helper which also adds the `og:` prefix for you. | - | Note that these global tags may be overridden by a page's meta tags. + | Please note that these tags might conflict with blog post tags. | */ 'meta' => [ - Meta::name('author', 'Mr. Hyde'), - Meta::name('twitter:creator', '@hyde_php'), - Meta::name('description', 'My Hyde Blog'), - Meta::name('keywords', 'Static Sites, Blogs, Documentation'), + // Meta::name('author', 'Mr. Hyde'), + // Meta::name('twitter:creator', '@hyde_php'), + // Meta::name('description', 'My Hyde Blog'), + // Meta::name('keywords', 'Static Sites, Blogs, Documentation'), Meta::name('generator', 'HydePHP '.Hyde\Framework\Hyde::version()), Meta::property('site_name', $siteName), ], diff --git a/src/Meta.php b/src/Meta.php index 2c423ddd..3c47faa3 100644 --- a/src/Meta.php +++ b/src/Meta.php @@ -2,38 +2,52 @@ namespace Hyde\Framework; +/** + * Helpers to fluently declare HTML meta tags + * @see \Tests\Feature\MetadataHelperTest + */ class Meta { - public static function name(string $name, string $content, bool $ifConditionIsMet = true): ?string + public static function name(string $name, string $content, bool $ifConditionIsMet = true): string { - if ($ifConditionIsMet) { - return ''; - } - - return null; + return ''; } - public static function property(string $property, string $content, bool $ifConditionIsMet = true): ?string + public static function property(string $property, string $content, bool $ifConditionIsMet = true): string { - if ($ifConditionIsMet) { - $property = static::formatOpenGraphProperty($property); - - return ''; - } + $property = static::formatOpenGraphProperty($property); - return null; + return ''; } public static function render(array $overridesGlobalMeta = []): string { return implode("\n", - array_unique( + static::filterUnique( array_merge( static::getGlobalMeta(), $overridesGlobalMeta ) ) ); + + } + + protected static function filterUnique(array $meta): array + { + $array = []; + $existing = []; + + foreach ($meta as $metaItem) { + $substring = substr($metaItem, 6, strpos($metaItem, ' content="') - 6); + + if (!in_array($substring, $existing)) { + $array[] = $metaItem; + $existing[] = $substring; + } + } + + return $array; } public static function getGlobalMeta(): array @@ -43,6 +57,6 @@ public static function getGlobalMeta(): array protected static function formatOpenGraphProperty(string $property): string { - return str_starts_with('og:', $property) ? $property : 'og:'.$property; + return str_starts_with($property, 'og:') ? $property : 'og:'.$property; } } diff --git a/tests/Feature/MetadataHelperTest.php b/tests/Feature/MetadataHelperTest.php new file mode 100644 index 00000000..5a709c0f --- /dev/null +++ b/tests/Feature/MetadataHelperTest.php @@ -0,0 +1,142 @@ + []]); + } + + // Test name method returns a valid HTML meta string + public function test_name_method_returns_a_valid_html_meta_string() + { + $this->assertEquals( + '', + Meta::name('foo', 'bar') + ); + } + + // Test property method returns a valid HTML meta string + public function test_property_method_returns_a_valid_html_meta_string() + { + $this->assertEquals( + '', + Meta::property('foo', 'bar') + ); + } + + // Test property method accepts property with og prefix + public function test_property_method_accepts_property_with_og_prefix() + { + $this->assertEquals( + '', + Meta::property('og:foo', 'bar') + ); + } + + // Test property method accepts property without og prefix + public function test_property_method_accepts_property_without_og_prefix() + { + $this->assertEquals( + '', + Meta::property('foo', 'bar') + ); + } + + // Test render method implodes an array of meta tags into a formatted string + public function test_render_method_implodes_an_array_of_meta_tags_into_a_formatted_string() + { + $this->assertEquals( + '' + ."\n".'', + + Meta::render([ + Meta::name('foo', 'bar'), + Meta::property('og:foo', 'bar') + ]) + ); + } + + // Test render method returns an empty string if no meta tags are supplied + public function test_render_method_returns_an_empty_string_if_no_meta_tags_are_supplied() + { + $this->assertEquals( + '', + Meta::render([]) + ); + } + + // Test render method returns config defined tags if no meta tags are supplied + public function test_render_method_returns_config_defined_tags_if_no_meta_tags_are_supplied() + { + config(['hyde.meta' => [ + Meta::name('foo', 'bar'), + Meta::property('og:foo', 'bar') + ]]); + + $this->assertEquals( + '' + ."\n".'', + + Meta::render([]) + ); + } + + // Test render method merges config defined tags with supplied meta tags + public function test_render_method_merges_config_defined_tags_with_supplied_meta_tags() + { + config(['hyde.meta' => [ + Meta::name('foo', 'bar'), + ]]); + + $this->assertEquals( + '' + ."\n".'', + + Meta::render([ + Meta::property('foo', 'bar') + ]) + ); + } + + // Test render method returns unique meta tags + public function test_render_method_returns_unique_meta_tags() + { + config(['hyde.meta' => [ + Meta::name('foo', 'bar'), + ]]); + + $this->assertEquals( + '', + Meta::render([ + Meta::name('foo', 'bar'), + ]) + ); + } + + // Test render method gives precedence to supplied meta tags + public function test_render_method_gives_precedence_to_supplied_meta_tags() + { + config(['hyde.meta' => [ + Meta::name('foo', 'bar'), + ]]); + + $this->assertEquals( + '', + + Meta::render([ + Meta::name('foo', 'baz'), + ]) + ); + } +} From dde528aa97e52dfe81a47d9cb686a88604d7dd78 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Tue, 17 May 2022 20:19:26 +0000 Subject: [PATCH 10/14] Apply fixes from StyleCI --- src/Meta.php | 6 +++--- src/StaticPageBuilder.php | 2 +- tests/Feature/MetadataHelperTest.php | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Meta.php b/src/Meta.php index 3c47faa3..7fdf6c25 100644 --- a/src/Meta.php +++ b/src/Meta.php @@ -3,7 +3,8 @@ namespace Hyde\Framework; /** - * Helpers to fluently declare HTML meta tags + * Helpers to fluently declare HTML meta tags. + * * @see \Tests\Feature\MetadataHelperTest */ class Meta @@ -30,7 +31,6 @@ public static function render(array $overridesGlobalMeta = []): string ) ) ); - } protected static function filterUnique(array $meta): array @@ -41,7 +41,7 @@ protected static function filterUnique(array $meta): array foreach ($meta as $metaItem) { $substring = substr($metaItem, 6, strpos($metaItem, ' content="') - 6); - if (!in_array($substring, $existing)) { + if (! in_array($substring, $existing)) { $array[] = $metaItem; $existing[] = $substring; } diff --git a/src/StaticPageBuilder.php b/src/StaticPageBuilder.php index 122928e0..7e6c4844 100644 --- a/src/StaticPageBuilder.php +++ b/src/StaticPageBuilder.php @@ -31,7 +31,7 @@ class StaticPageBuilder public function __construct(protected MarkdownDocument|BladePage $page, bool $selfInvoke = false) { $this->needsDirectory(static::$outputPath); - + if ($selfInvoke) { $this->__invoke(); } diff --git a/tests/Feature/MetadataHelperTest.php b/tests/Feature/MetadataHelperTest.php index 5a709c0f..cea9e116 100644 --- a/tests/Feature/MetadataHelperTest.php +++ b/tests/Feature/MetadataHelperTest.php @@ -62,7 +62,7 @@ public function test_render_method_implodes_an_array_of_meta_tags_into_a_formatt Meta::render([ Meta::name('foo', 'bar'), - Meta::property('og:foo', 'bar') + Meta::property('og:foo', 'bar'), ]) ); } @@ -81,7 +81,7 @@ public function test_render_method_returns_config_defined_tags_if_no_meta_tags_a { config(['hyde.meta' => [ Meta::name('foo', 'bar'), - Meta::property('og:foo', 'bar') + Meta::property('og:foo', 'bar'), ]]); $this->assertEquals( @@ -104,7 +104,7 @@ public function test_render_method_merges_config_defined_tags_with_supplied_meta ."\n".'', Meta::render([ - Meta::property('foo', 'bar') + Meta::property('foo', 'bar'), ]) ); } From c954bcbf72a39b07863824cac73fde54346898df Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 17 May 2022 22:22:05 +0200 Subject: [PATCH 11/14] Format and add todo --- src/Concerns/HasMetadata.php | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Concerns/HasMetadata.php b/src/Concerns/HasMetadata.php index 404f564c..ccdc4498 100644 --- a/src/Concerns/HasMetadata.php +++ b/src/Concerns/HasMetadata.php @@ -14,6 +14,7 @@ * @see \Tests\Feature\Concerns\HasMetadataTest * * @todo Unify the $page property and handle metadata through it + * @todo Only add blog post properties if the page is a blog post */ trait HasMetadata { @@ -27,14 +28,14 @@ public function constructMetadata(): void } #[ArrayShape(['name' => "\content"])] - public function getMetadata(): array - { - if (! isset($this->metadata)) { - return []; - } - - return $this->metadata->metadata; - } + public function getMetadata(): array + { + if (! isset($this->metadata)) { + return []; + } + + return $this->metadata->metadata; + } #[ArrayShape(['property' => 'content'])] public function getMetaProperties(): array From d7a55acbda1a13abdb53b1b0e792056fac85f9bf Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 17 May 2022 22:32:31 +0200 Subject: [PATCH 12/14] Move deprecated method to AuthorService --- src/Concerns/HasMetadata.php | 20 +++----------------- src/Services/AuthorService.php | 16 ++++++++++++++++ tests/Feature/AuthorServiceTest.php | 18 ++++++++++++++++++ 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/Concerns/HasMetadata.php b/src/Concerns/HasMetadata.php index ccdc4498..99d9f664 100644 --- a/src/Concerns/HasMetadata.php +++ b/src/Concerns/HasMetadata.php @@ -3,7 +3,9 @@ namespace Hyde\Framework\Concerns; use Hyde\Framework\Hyde; +use Hyde\Framework\Models\Author; use Hyde\Framework\Models\Metadata; +use Hyde\Framework\Services\AuthorService; use JetBrains\PhpStorm\ArrayShape; /** @@ -59,7 +61,7 @@ protected function makeMetadata(): void } if (isset($this->matter['author'])) { - $this->metadata->add('author', $this->getAuthor($this->matter['author'])); + $this->metadata->add('author', AuthorService::getAuthorName($this->matter['author'])); } if (isset($this->matter['category'])) { @@ -104,20 +106,4 @@ protected function makeMetaProperties(): void } } - /** - * Parse the author string from the front matter with support for both flat and array notation. - * - * @deprecated Will be moved to the Author model - * - * @param string|array $author - * @return string - */ - protected function getAuthor(string|array $author): string - { - if (is_string($author)) { - return $author; - } - - return $author['username'] ?? $author['name'] ?? 'Guest'; - } } diff --git a/src/Services/AuthorService.php b/src/Services/AuthorService.php index 80293552..b27b4624 100644 --- a/src/Services/AuthorService.php +++ b/src/Services/AuthorService.php @@ -107,4 +107,20 @@ public static function find(string $username, bool $forgiving = true): Author|fa return $service->authors->firstWhere('username', $username) ?? false; } + + + /** + * Parse the author name string from front matter with support for both flat and array notation. + * + * @param string|array $author + * @return string + */ + public static function getAuthorName(string|array $author): string + { + if (is_string($author)) { + return $author; + } + + return $author['name'] ?? $author['username'] ?? 'Guest'; + } } diff --git a/tests/Feature/AuthorServiceTest.php b/tests/Feature/AuthorServiceTest.php index 6236833d..926117fb 100644 --- a/tests/Feature/AuthorServiceTest.php +++ b/tests/Feature/AuthorServiceTest.php @@ -121,4 +121,22 @@ public function test_get_yaml_method_returns_empty_array_if_file_does_not_contai $this->assertEquals([], $service->getYaml()); } + + public function test_get_author_name_helper_returns_string_for_string() + { + $this->assertEquals('foo', AuthorService::getAuthorName('foo')); + } + + public function test_get_author_name_helper_returns_string_for_array() + { + $this->assertEquals('foo', AuthorService::getAuthorName(['name' => 'foo'])); + } + + public function test_get_author_name_helper_returns_string_for_array_with_proper_fallback_priorities() + { + $this->assertEquals('foo', AuthorService::getAuthorName(['name' => 'foo', 'username' => 'bar'])); + $this->assertEquals('bar', AuthorService::getAuthorName(['username' => 'bar'])); + $this->assertEquals('Guest', AuthorService::getAuthorName([])); + } + } From e0525ad3c24cd3f9cf2f4371ab50cdeddf03efed Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 17 May 2022 22:38:00 +0200 Subject: [PATCH 13/14] Remove unused property --- src/Meta.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Meta.php b/src/Meta.php index 7fdf6c25..154d9aaf 100644 --- a/src/Meta.php +++ b/src/Meta.php @@ -9,12 +9,12 @@ */ class Meta { - public static function name(string $name, string $content, bool $ifConditionIsMet = true): string + public static function name(string $name, string $content): string { return ''; } - public static function property(string $property, string $content, bool $ifConditionIsMet = true): string + public static function property(string $property, string $content): string { $property = static::formatOpenGraphProperty($property); From 2b038baa3068177ad63d41b208d32c9b02042ad2 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Tue, 17 May 2022 20:38:10 +0000 Subject: [PATCH 14/14] Apply fixes from StyleCI --- src/Concerns/HasMetadata.php | 2 -- src/Services/AuthorService.php | 1 - tests/Feature/AuthorServiceTest.php | 3 +-- 3 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Concerns/HasMetadata.php b/src/Concerns/HasMetadata.php index 99d9f664..dc08ab49 100644 --- a/src/Concerns/HasMetadata.php +++ b/src/Concerns/HasMetadata.php @@ -3,7 +3,6 @@ namespace Hyde\Framework\Concerns; use Hyde\Framework\Hyde; -use Hyde\Framework\Models\Author; use Hyde\Framework\Models\Metadata; use Hyde\Framework\Services\AuthorService; use JetBrains\PhpStorm\ArrayShape; @@ -105,5 +104,4 @@ protected function makeMetaProperties(): void } } } - } diff --git a/src/Services/AuthorService.php b/src/Services/AuthorService.php index b27b4624..ac0effe5 100644 --- a/src/Services/AuthorService.php +++ b/src/Services/AuthorService.php @@ -108,7 +108,6 @@ public static function find(string $username, bool $forgiving = true): Author|fa return $service->authors->firstWhere('username', $username) ?? false; } - /** * Parse the author name string from front matter with support for both flat and array notation. * diff --git a/tests/Feature/AuthorServiceTest.php b/tests/Feature/AuthorServiceTest.php index 926117fb..22642f56 100644 --- a/tests/Feature/AuthorServiceTest.php +++ b/tests/Feature/AuthorServiceTest.php @@ -121,7 +121,7 @@ public function test_get_yaml_method_returns_empty_array_if_file_does_not_contai $this->assertEquals([], $service->getYaml()); } - + public function test_get_author_name_helper_returns_string_for_string() { $this->assertEquals('foo', AuthorService::getAuthorName('foo')); @@ -138,5 +138,4 @@ public function test_get_author_name_helper_returns_string_for_array_with_proper $this->assertEquals('bar', AuthorService::getAuthorName(['username' => 'bar'])); $this->assertEquals('Guest', AuthorService::getAuthorName([])); } - }