Skip to content

Commit a737879

Browse files
tpetryPHP CS Fixer
andauthored
Add previewify prefix (#24)
* add required previewify prefix * Fix code style (php-cs-fixer) * only add prefix when not already available * Fix code style (php-cs-fixer) Co-authored-by: PHP CS Fixer <[email protected]>
1 parent cf8ec8d commit a737879

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,9 @@ The `previewify()` method also returns a signed URL to the image, which lets you
253253
<img alt="@seo('title')" src="@seo('previewify', 'blog')">
254254
```
255255

256+
> **Note**
257+
> The `previewify:` prefix will be automatically prepended to all provided data keys.
258+
256259
## Examples
257260

258261
### Service Provider

src/SEOManager.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,14 @@ public function previewify(string $alias, int|string|array $data = null): string
192192

193193
if ($data === null) {
194194
$data = [
195-
'title' => $this->raw('title'),
196-
'description' => $this->raw('description'),
195+
'previewify:title' => $this->raw('title'),
196+
'previewify:description' => $this->raw('description'),
197197
];
198+
} else {
199+
$data = array_combine(
200+
array_map(fn ($key) => str_starts_with($key, 'previewify:') ? $key : "previewify:{$key}", array_keys($data)),
201+
$data,
202+
);
198203
}
199204

200205
$query = base64_encode(json_encode($data, JSON_THROW_ON_ERROR));

tests/Pest/PreviewifyTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@
1818

1919
test('previewify templates can be given data', function () {
2020
seo()->previewify('blog', 1);
21-
expect(seo()->previewify('blog', ['title' => 'abc', 'excerpt' => 'def']))
21+
expect(seo()->previewify('blog', ['title' => 'abc', 'previewify:excerpt' => 'def']))
2222
->toContain('previewify.app/generate/templates/1')
23-
->toContain(base64_encode(json_encode(['title' => 'abc', 'excerpt' => 'def'])));
23+
->toContain(base64_encode(json_encode(['previewify:title' => 'abc', 'previewify:excerpt' => 'def'])));
2424
});
2525

2626
test('the previewify method returns a link to a signed url', function () {
2727
seo()->previewify('blog', 1);
2828

2929
expect(seo()->previewify('blog', ['title' => 'abc']))
30-
->toContain('?signature=' . hash_hmac('sha256', base64_encode(json_encode(['title' => 'abc'])), config('services.previewify.key')));
30+
->toContain('?signature=' . hash_hmac('sha256', base64_encode(json_encode(['previewify:title' => 'abc'])), config('services.previewify.key')));
3131
});
3232

3333
test("previewify templates use default data when they're not passed any data explicitly", function () {
@@ -37,7 +37,7 @@
3737

3838
expect(seo()->previewify('blog'))
3939
->toContain('previewify.app/generate/templates/1')
40-
->toContain(base64_encode(json_encode(['title' => 'foo', 'description' => 'bar'])));
40+
->toContain(base64_encode(json_encode(['previewify:title' => 'foo', 'previewify:description' => 'bar'])));
4141
});
4242

4343
test('previewify images are used as the cover images', function () {
@@ -66,7 +66,7 @@
6666

6767
expect(seo()->previewify('blog'))
6868
->toContain('previewify.app/generate/templates/1')
69-
->toContain(base64_encode(json_encode(['title' => 'foo', 'description' => 'bar'])));
69+
->toContain(base64_encode(json_encode(['previewify:title' => 'foo', 'previewify:description' => 'bar'])));
7070
});
7171

7272
test('the @seo helper can be used for setting a previewify image', function () {

0 commit comments

Comments
 (0)