Skip to content

Commit 8e8653f

Browse files
authored
Merge pull request #259 from Jurj-Bogdan/issue258
opengraph dynamic image and description for blog posts
2 parents 55280fc + f78ef55 commit 8e8653f

File tree

12 files changed

+66
-94
lines changed

12 files changed

+66
-94
lines changed

ADD_BLOG_ENTRY.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ draft: true
5757
public: true
5858
created: '2024-10-22T11:00:00-01:00'
5959
updated: '2024-10-22T11:00:00-01:00'
60+
openGraphImage: '2024-10-22-custom-image.png'
61+
openGraphDescription: 'Custom description'
6062
tags:
6163
- example tag
6264
- second example tag
@@ -85,6 +87,15 @@ All the following fields are **required**:
8587

8688
- `tags` **(array of strings)** - used to filter blog posts by tag
8789

90+
The Open Graph preview card has a default image and description which can be **optionally** overwritten using the following fields:
91+
92+
- `openGraphImage` **string** - custom image to replace the default in the Open Graph preview
93+
- given value **must** be the same as the file name and extension
94+
- the corresponding image file **must** be added in the `public\images\opengraph\blog` directory
95+
- to maintain cohesion, the image file **should** be named after the blog post itself (`yyyy-mm-dd-title.extension`)
96+
97+
- `openGraphDescription` **string** - custom text to be displayed in the Open Graph preview, replacing the default.
98+
8899
### File examples
89100

90101
#### author.yml
@@ -108,6 +119,8 @@ draft: false
108119
public: true
109120
created: '2024-10-24'
110121
updated: '2024-10-24'
122+
openGraphImage: '2024-10-24-custom-image.png'
123+
openGraphDescription: 'Custom description'
111124
tags:
112125
- example tag
113126
---

psalm-baseline.xml

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<files psalm-version="5.25.0@01a8eb06b9e9cc6cfb6a320bf9fb14331919d505">
2+
<files psalm-version="5.26.1@d747f6500b38ac4f7dfc5edbcae6e4b637d7add0">
33
<file src="bin/clear-config-cache.php">
44
<MixedArgument>
55
<code><![CDATA[$config['config_cache_path']]]></code>
@@ -73,11 +73,6 @@
7373
<code><![CDATA[$revisions]]></code>
7474
</MixedAssignment>
7575
</file>
76-
<file src="src/Blog/BlogPost.php">
77-
<MixedPropertyTypeCoercion>
78-
<code><![CDATA[$tags]]></code>
79-
</MixedPropertyTypeCoercion>
80-
</file>
8176
<file src="src/Blog/Console/FeedGenerator.php">
8277
<ArgumentTypeCoercion>
8378
<code><![CDATA[$latest->updated]]></code>
@@ -108,9 +103,6 @@
108103
</ArgumentTypeCoercion>
109104
</file>
110105
<file src="src/Blog/Console/SeedBlogDatabase.php">
111-
<DocblockTypeContradiction>
112-
<code><![CDATA[(new DateTimeImmutable('now'))->getTimestamp()]]></code>
113-
</DocblockTypeContradiction>
114106
<MixedArgument>
115107
<code><![CDATA[$basePath]]></code>
116108
<code><![CDATA[$dbPath]]></code>
@@ -122,9 +114,6 @@
122114
<code><![CDATA[$postsPath]]></code>
123115
<code><![CDATA[$this->authorDataRootPath]]></code>
124116
</MixedAssignment>
125-
<RedundantConditionGivenDocblockType>
126-
<code><![CDATA[$post->created]]></code>
127-
</RedundantConditionGivenDocblockType>
128117
</file>
129118
<file src="src/Blog/CreateBlogPostFromDataArray.php">
130119
<ArgumentTypeCoercion>

public/images/opengraph/blog/.gitkeep

Whitespace-only changes.

src/App/ContentParser/DocumentInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ interface DocumentInterface
1515
* public: bool,
1616
* created: string,
1717
* updated: string,
18+
* openGraphImage: string,
19+
* openGraphDescription: string,
1820
* tags: list<string>
1921
* }
2022
*/

src/Blog/BlogAuthor.php

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,11 @@
66

77
class BlogAuthor
88
{
9-
/** @var string */
10-
public $email;
11-
12-
/** @var string */
13-
public $fullname;
14-
15-
/** @var string */
16-
public $url;
17-
18-
/** @var string */
19-
public $username;
20-
219
public function __construct(
22-
string $username,
23-
string $fullname,
24-
string $email,
25-
string $url
10+
public string $username,
11+
public string $fullName,
12+
public string $email,
13+
public string $url
2614
) {
27-
$this->username = $username;
28-
$this->fullname = $fullname;
29-
$this->email = $email;
30-
$this->url = $url;
3115
}
3216
}

src/Blog/BlogPost.php

Lines changed: 16 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -8,61 +8,23 @@
88

99
class BlogPost
1010
{
11-
/** @var BlogAuthor */
12-
public $author;
13-
14-
/** @var string */
15-
public $body;
16-
17-
/** @var DateTimeInterface */
18-
public $created;
19-
20-
/** @var string */
21-
public $extended;
22-
23-
public ?string $toc;
24-
25-
/** @var string */
26-
public $id;
27-
28-
/** @var bool */
29-
public $isDraft;
30-
31-
/** @var bool */
32-
public $isPublic;
33-
34-
/** @var string[] */
35-
public $tags;
36-
37-
/** @var string */
38-
public $title;
39-
40-
/** @var null|DateTimeInterface */
41-
public $updated;
42-
11+
/**
12+
* @param string[] $tags
13+
*/
4314
public function __construct(
44-
string $id,
45-
string $title,
46-
BlogAuthor $author,
47-
DateTimeInterface $created,
48-
?DateTimeInterface $updated,
49-
array $tags,
50-
string $body,
51-
string $extended,
52-
?string $toc,
53-
bool $isDraft,
54-
bool $isPublic
15+
public string $id,
16+
public string $title,
17+
public BlogAuthor $author,
18+
public DateTimeInterface $created,
19+
public ?DateTimeInterface $updated,
20+
public array $tags,
21+
public string $body,
22+
public string $extended,
23+
public ?string $toc,
24+
public bool $isDraft,
25+
public bool $isPublic,
26+
public ?string $openGraphImage = null,
27+
public ?string $openGraphDescription = null
5528
) {
56-
$this->id = $id;
57-
$this->title = $title;
58-
$this->author = $author;
59-
$this->created = $created;
60-
$this->updated = $updated;
61-
$this->tags = $tags;
62-
$this->body = $body;
63-
$this->extended = $extended;
64-
$this->toc = $toc;
65-
$this->isDraft = $isDraft;
66-
$this->isPublic = $isPublic;
6729
}
6830
}

src/Blog/Console/FeedGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ private function generateFeed(
186186
private function getAuthor(BlogAuthor $author): array
187187
{
188188
return [
189-
'name' => $author->fullname ?: $author->username,
189+
'name' => $author->fullName ?: $author->username,
190190
'email' => $author->email,
191191
'uri' => $author->url,
192192
];

src/Blog/Console/SeedBlogDatabase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
174174
$template,
175175
$pdo->quote($post->id),
176176
$pdo->quote(substr($path, $trim)),
177-
$post->created ? $post->created->getTimestamp() : (new DateTimeImmutable('now'))->getTimestamp(),
177+
$post->created->getTimestamp(),
178178
$post->updated ? $post->updated->getTimestamp() : (new DateTimeImmutable('now'))->getTimestamp(),
179179
$pdo->quote($post->title),
180180
$pdo->quote($post->author->username),

src/Blog/CreateBlogPostFromDataArray.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ trait CreateBlogPostFromDataArray
2323
{
2424
private string $authorDataRootPath = 'data/blog/authors';
2525

26+
public string $customOpengraphImagePath = '/images/opengraph/blog';
27+
2628
private ?ParserInterface $contentParser = null;
2729

2830
/**
@@ -61,6 +63,17 @@ private function createBlogPostFromDataArray(array $post): BlogPost
6163
$updated = $post['updated'] && $post['updated'] !== $post['created']
6264
? $this->createDateTimeFromString($post['updated'])
6365
: $created;
66+
$image = isset($post['openGraphImage']) && file_exists(
67+
sprintf(
68+
'public%s/%s',
69+
$this->customOpengraphImagePath,
70+
$post['openGraphImage']
71+
)
72+
) ? sprintf(
73+
'%s/%s',
74+
$this->customOpengraphImagePath,
75+
$post['openGraphImage']
76+
) : null;
6477

6578
return new BlogPost(
6679
$post['id'],
@@ -75,7 +88,9 @@ private function createBlogPostFromDataArray(array $post): BlogPost
7588
$parts[1] ?? '',
7689
$toc,
7790
(bool) $post['draft'],
78-
(bool) $post['public']
91+
(bool) $post['public'],
92+
$image,
93+
$post['openGraphDescription'] ?? null
7994
);
8095
}
8196

src/Blog/PlatesFunctionsDelegator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ public function postAuthor(BlogPost $post): string
4848
{
4949
$author = $post->author;
5050
if ($author->url === '') {
51-
return $author->fullname ?: $author->username;
51+
return $author->fullName ?: $author->username;
5252
}
5353

54-
return sprintf('<a href="%s" target="_blank">%s</a>', $author->url, $author->fullname ?: $author->username);
54+
return sprintf('<a href="%s" target="_blank">%s</a>', $author->url, $author->fullName ?: $author->username);
5555
}
5656

5757
public function postUrl(BlogPost $post): string

0 commit comments

Comments
 (0)