diff --git a/assets/views/components/meta.blade.php b/assets/views/components/meta.blade.php index 3998ea6..a09a228 100644 --- a/assets/views/components/meta.blade.php +++ b/assets/views/components/meta.blade.php @@ -1,14 +1,22 @@ @if(seo('title')) @seo('title') - + + @unless(seo()->hasTag('og:title')) + {{-- If an og:title tag is provided directly, it's included in the @foreach below --}} + + @endunless @endif -@if(seo('description')) +@if(seo('description')) @endif - +@if(seo('type')) + +@else + +@endif @if(seo('site')) @endif diff --git a/composer.json b/composer.json index df3a55a..02229b5 100644 --- a/composer.json +++ b/composer.json @@ -42,5 +42,10 @@ } }, "minimum-stability": "dev", - "prefer-stable": true + "prefer-stable": true, + "config": { + "allow-plugins": { + "pestphp/pest-plugin": true + } + } } diff --git a/src/SEOManager.php b/src/SEOManager.php index cbd8fd4..68d6d68 100644 --- a/src/SEOManager.php +++ b/src/SEOManager.php @@ -13,6 +13,7 @@ use Illuminate\Support\Str; * @method $this url(string $url = null, ...$args) Set the canonical URL. * @method $this site(string $site = null, ...$args) Set the site name. * @method $this image(string $url = null, ...$args) Set the cover image. + * @method $this type(string $type = null, ...$args) Set the page type. * @method $this twitter(enabled $bool = true, ...$args) Enable the Twitter extension. * @method $this twitterSite(string $username = null, ...$args) Set the Twitter author. * @method $this twitterTitle(string $title = null, ...$args) Set the Twitter title. @@ -53,7 +54,7 @@ class SEOManager protected function getKeys(): array { return collect([ - 'site', 'title', 'image', 'description', 'url', + 'site', 'title', 'image', 'description', 'url', 'type', 'twitter.site', 'twitter.title', 'twitter.image', 'twitter.description', ]) ->merge(array_keys($this->defaults)) @@ -201,6 +202,18 @@ class SEOManager return $this->tags; } + /** Has a specific tag been set? */ + public function hasRawTag(string $key): bool + { + return isset($this->tags[$key]) && ($this->tags[$key] !== null); + } + + /** Has a specific meta tag been set? */ + public function hasTag(string $property): bool + { + return $this->hasRawTag("meta.{$property}"); + } + /** Add a head tag. */ public function rawTag(string $key, string $tag = null): static { diff --git a/tests/Pest/ManagerTest.php b/tests/Pest/ManagerTest.php index f404c9b..f99bf91 100644 --- a/tests/Pest/ManagerTest.php +++ b/tests/Pest/ManagerTest.php @@ -127,3 +127,22 @@ test('canonical url can be changed', function () { ->toContain('') ->toContain(''); }); + +test('og:title can be overridden using a tag', function () { + seo()->title('foo') + ->tag('og:title', 'bar'); + + expect(meta()) + ->toContain('foo') + ->toContain(''); +}); + +test('type can be overridden using the type method', function () { + expect(meta())->toContain(''); // default + + seo()->type('foo'); + + expect(meta()) + ->toContain('') // overridden + ->not()->toContain('website'); +});