Skip to content

Commit f22bc0b

Browse files
authored
Merge pull request #325 from CheesyTech/master
Adapts html to the inertia style to update headers and scripts
2 parents cde518c + baaff7a commit f22bc0b

File tree

4 files changed

+89
-1
lines changed

4 files changed

+89
-1
lines changed

src/SEOTools/Integrations/Inertia.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Artesaos\SEOTools\Integrations;
6+
7+
final class Inertia
8+
{
9+
public function convertHeadToInertiaStyle(string $seo)
10+
{
11+
$pattern = '/<(meta|script|title|link)/';
12+
$replacement = '<$1 inertia';
13+
14+
return preg_replace($pattern, $replacement, $seo);
15+
}
16+
}

src/SEOTools/SEOTools.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Artesaos\SEOTools;
44

55
use Artesaos\SEOTools\Contracts\SEOTools as SEOContract;
6+
use Artesaos\SEOTools\Integrations\Inertia;
67
use Illuminate\Support\Traits\Macroable;
78

89
/**
@@ -13,7 +14,7 @@
1314
class SEOTools implements SEOContract
1415
{
1516
use Macroable;
16-
17+
1718
/**
1819
* {@inheritdoc}
1920
*/
@@ -135,6 +136,10 @@ public function generate($minify = false)
135136
// Use jsonLdMulti by default; since it is just a wrapper
136137
$html .= $this->jsonLdMulti()->generate();
137138

139+
if (config('seotools.inertia') === true) {
140+
$html = app(Inertia::class)->convertHeadToInertiaStyle($html);
141+
}
142+
138143
return ($minify) ? str_replace(PHP_EOL, '', $html) : $html;
139144
}
140145
}

src/resources/config/seotools.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
return [
7+
'inertia' => env('SEO_TOOLS_INERTIA', false),
78
'meta' => [
89
/*
910
* The default configurations to be used by the meta generator.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Artesaos\SEOTools\Tests\Integrations;
6+
7+
use Artesaos\SEOTools\Integrations\Inertia;
8+
use Artesaos\SEOTools\Tests\BaseTest;
9+
10+
class InertiaTest extends BaseTest
11+
{
12+
/***
13+
* @var Inertia
14+
*/
15+
protected $inertia;
16+
protected function setUp(): void
17+
{
18+
parent::setUp();
19+
20+
$this->inertia = $this->app->make(Inertia::class);
21+
}
22+
23+
public function test_convert_meta_to_inertia_style()
24+
{
25+
$seo = '<meta name="keywords" content="viewing, mobile TV, iphone, ipad">';
26+
27+
$expected = '<meta inertia name="keywords" content="viewing, mobile TV, iphone, ipad">';
28+
29+
$converted = $this->inertia->convertHeadToInertiaStyle($seo);
30+
31+
$this->assertEquals($expected, $converted);
32+
}
33+
34+
public function test_convert_canonical_link_to_inertia_style()
35+
{
36+
$seo = '<link rel="canonical" href="https://www.example.com">';
37+
38+
$expected = '<link inertia rel="canonical" href="https://www.example.com">';
39+
40+
$converted = $this->inertia->convertHeadToInertiaStyle($seo);
41+
42+
$this->assertEquals($expected, $converted);
43+
}
44+
45+
public function test_convert_title_to_inertia_style()
46+
{
47+
$seo = '<title>Title</title>';
48+
49+
$expected = '<title inertia>Title</title>';
50+
51+
$converted = $this->inertia->convertHeadToInertiaStyle($seo);
52+
53+
$this->assertEquals($expected, $converted);
54+
}
55+
56+
public function test_convert_script_to_inertia_style()
57+
{
58+
$seo = '<script type="application/ld+json">{"@context":"https://schema.org","@type":"WebPage","name":"example"}</script>';
59+
60+
$expected = '<script inertia type="application/ld+json">{"@context":"https://schema.org","@type":"WebPage","name":"example"}</script>';
61+
62+
$converted = $this->inertia->convertHeadToInertiaStyle($seo);
63+
64+
$this->assertEquals($expected, $converted);
65+
}
66+
}

0 commit comments

Comments
 (0)