1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hyde\Framework\Concerns\Internal; |
4
|
|
|
|
5
|
|
|
use Hyde\Framework\Actions\Constructors\FindsTitleForPage; |
6
|
|
|
use Hyde\Framework\Contracts\FrontMatter\BlogPostSchema; |
7
|
|
|
use Hyde\Framework\Contracts\FrontMatter\DocumentationPageSchema; |
8
|
|
|
use Hyde\Framework\Hyde; |
9
|
|
|
use Hyde\Framework\Models\Author; |
10
|
|
|
use Hyde\Framework\Models\DateString; |
11
|
|
|
use Hyde\Framework\Models\Image; |
12
|
|
|
use Illuminate\Support\Str; |
13
|
|
|
|
14
|
|
|
trait ConstructsPageSchemas |
15
|
|
|
{ |
16
|
|
|
protected function constructPageSchemas(): void |
17
|
|
|
{ |
18
|
|
|
$this->constructPageSchema(); |
19
|
|
|
|
20
|
|
|
if ($this instanceof BlogPostSchema) { |
21
|
|
|
$this->constructBlogPostSchema(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
if ($this instanceof DocumentationPageSchema) { |
25
|
|
|
$this->constructDocumentationPageSchema(); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
protected function constructPageSchema(): void |
30
|
|
|
{ |
31
|
|
|
$this->title = FindsTitleForPage::run($this); |
|
|
|
|
32
|
|
|
$this->canonicalUrl = $this->makeCanonicalUrl(); |
|
|
|
|
33
|
|
|
|
34
|
|
|
if ($this instanceof DocumentationPageSchema) { |
35
|
|
|
$this->constructSidebarNavigationData(); |
|
|
|
|
36
|
|
|
} else { |
37
|
|
|
$this->constructNavigationData(); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
protected function makeCanonicalUrl(): ?string |
42
|
|
|
{ |
43
|
|
|
if (! empty($this->matter('canonicalUrl'))) { |
|
|
|
|
44
|
|
|
return $this->matter('canonicalUrl'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if (Hyde::hasSiteUrl() && ! empty($this->identifier)) { |
48
|
|
|
return $this->getRoute()->getQualifiedUrl(); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return null; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function constructBlogPostSchema(): void |
55
|
|
|
{ |
56
|
|
|
$this->category = $this->matter('category'); |
|
|
|
|
57
|
|
|
$this->description = $this->matter('description', $this->makeDescription()); |
|
|
|
|
58
|
|
|
$this->date = $this->matter('date') !== null ? new DateString($this->matter('date')) : null; |
|
|
|
|
59
|
|
|
$this->author = $this->getAuthor(); |
|
|
|
|
60
|
|
|
$this->image = $this->getImage(); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
protected function makeDescription(): string |
64
|
|
|
{ |
65
|
|
|
if (strlen($this->markdown) >= 128) { |
66
|
|
|
return substr($this->markdown, 0, 125).'...'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return (string) $this->markdown; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
protected function getAuthor(): ?Author |
73
|
|
|
{ |
74
|
|
|
if ($this->matter('author')) { |
75
|
|
|
return Author::make($this->matter('author')); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return null; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function getImage(): ?Image |
82
|
|
|
{ |
83
|
|
|
if ($this->matter('image')) { |
84
|
|
|
return Image::make($this->matter('image')); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return null; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
protected function constructDocumentationPageSchema(): void |
91
|
|
|
{ |
92
|
|
|
$this->category = $this->getDocumentationPageCategory(); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
protected function getDocumentationPageCategory(): ?string |
96
|
|
|
{ |
97
|
|
|
// If the documentation page is in a subdirectory, |
98
|
|
|
// then we can use that as the category name. |
99
|
|
|
// Otherwise, we look in the front matter. |
100
|
|
|
|
101
|
|
|
return str_contains($this->identifier, '/') |
102
|
|
|
? Str::before($this->identifier, '/') |
103
|
|
|
: $this->matter('category', 'other'); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|