1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hyde\Framework\Models\Pages; |
4
|
|
|
|
5
|
|
|
use Hyde\Framework\Concerns\FrontMatter\Schemas\BlogPostSchema; |
6
|
|
|
use Hyde\Framework\Contracts\AbstractMarkdownPage; |
7
|
|
|
use Hyde\Framework\Hyde; |
8
|
|
|
use Hyde\Framework\Models\FrontMatter; |
9
|
|
|
use Hyde\Framework\Models\Markdown; |
10
|
|
|
use Illuminate\Support\Collection; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @see \Hyde\Framework\Testing\Feature\MarkdownPostTest |
14
|
|
|
*/ |
15
|
|
|
class MarkdownPost extends AbstractMarkdownPage |
16
|
|
|
{ |
17
|
|
|
use BlogPostSchema; |
18
|
|
|
|
19
|
|
|
public static string $sourceDirectory = '_posts'; |
20
|
|
|
public static string $outputDirectory = 'posts'; |
21
|
|
|
public static string $template = 'hyde::layouts/post'; |
22
|
|
|
|
23
|
|
|
public function __construct(string $identifier = '', ?FrontMatter $matter = null, ?Markdown $markdown = null) |
24
|
|
|
{ |
25
|
|
|
parent::__construct($identifier, $matter, $markdown); |
26
|
|
|
|
27
|
|
|
$this->constructBlogPostSchema(); |
28
|
|
|
$this->constructMetadata(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** @deprecated v0.58.x-beta (may be moved to BlogPostSchema) */ |
32
|
|
|
public function getCanonicalLink(): string |
33
|
|
|
{ |
34
|
|
|
return Hyde::url($this->getCurrentPagePath().'.html'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** @deprecated v0.58.x-beta (pull description instead) */ |
38
|
|
|
public function getPostDescription(): string |
39
|
|
|
{ |
40
|
|
|
return $this->description; |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public static function getLatestPosts(): Collection |
44
|
|
|
{ |
45
|
|
|
return static::all()->sortByDesc('matter.date'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// HasArticleMetadata (Generates article metadata for a MarkdownPost) |
49
|
|
|
|
50
|
|
|
public array $metadata = []; |
51
|
|
|
public array $properties = []; |
52
|
|
|
|
53
|
|
|
protected function constructMetadata(): void |
54
|
|
|
{ |
55
|
|
|
$this->parseFrontMatterMetadata(); |
56
|
|
|
|
57
|
|
|
$this->makeOpenGraphPropertiesForArticle(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getMetadata(): array |
61
|
|
|
{ |
62
|
|
|
return $this->metadata; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getMetaProperties(): array |
66
|
|
|
{ |
67
|
|
|
return $this->properties; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Generate metadata from the front matter that can be used in standard <meta> tags. |
72
|
|
|
* This helper is page type agnostic and works with any kind of model having front matter. |
73
|
|
|
*/ |
74
|
|
|
protected function parseFrontMatterMetadata(): void |
75
|
|
|
{ |
76
|
|
|
if ($this->matter('description') !== null) { |
77
|
|
|
$this->metadata['description'] = $this->matter('description'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ($this->matter('author') !== null) { |
81
|
|
|
$this->metadata['author'] = $this->getAuthorName($this->matter('author')); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if ($this->matter('category') !== null) { |
85
|
|
|
$this->metadata['keywords'] = $this->matter('category'); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Generate opengraph metadata from front matter for an og:article such as a blog post. |
91
|
|
|
*/ |
92
|
|
|
protected function makeOpenGraphPropertiesForArticle(): void |
93
|
|
|
{ |
94
|
|
|
$this->properties['og:type'] = 'article'; |
95
|
|
|
if (Hyde::hasSiteUrl()) { |
96
|
|
|
$this->properties['og:url'] = $this->getRoute()->getQualifiedUrl(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ($this->matter('title') !== null) { |
100
|
|
|
$this->properties['og:title'] = $this->matter('title'); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if ($this->matter('date') !== null) { |
104
|
|
|
$this->properties['og:article:published_time'] = date('c', strtotime($this->matter('date'))); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if ($this->matter('image') !== null) { |
108
|
|
|
$this->setImageMetadata(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Parse the author name string from front matter with support for both flat and array notation. |
114
|
|
|
* |
115
|
|
|
* @param string|array $author |
116
|
|
|
* @return string |
117
|
|
|
* |
118
|
|
|
* @deprecated v0.58.x-beta (Use author model instead) |
119
|
|
|
*/ |
120
|
|
|
protected function getAuthorName(string|array $author): string |
121
|
|
|
{ |
122
|
|
|
if (is_string($author)) { |
|
|
|
|
123
|
|
|
return $author; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $author['name'] ?? $author['username'] ?? 'Guest'; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
protected function setImageMetadata(): void |
130
|
|
|
{ |
131
|
|
|
if (is_string($this->matter('image'))) { |
132
|
|
|
$this->properties['og:image'] = $this->matter('image'); |
133
|
|
|
} else { |
134
|
|
|
if (isset($this->matter('image')['path'])) { |
135
|
|
|
$this->properties['og:image'] = $this->matter('image')['path']; |
136
|
|
|
} |
137
|
|
|
if (isset($this->matter('image')['uri'])) { |
138
|
|
|
$this->properties['og:image'] = $this->matter('image')['uri']; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|