1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hyde\Framework\Concerns\FrontMatter\Schemas\Constructors; |
4
|
|
|
|
5
|
|
|
use Hyde\Framework\Actions\Constructors\FindsNavigationDataForPage; |
6
|
|
|
use Hyde\Framework\Actions\Constructors\FindsTitleForPage; |
7
|
|
|
use Hyde\Framework\Concerns\FrontMatter\Schemas\BlogPostSchema; |
8
|
|
|
use Hyde\Framework\Concerns\FrontMatter\Schemas\DocumentationPageSchema; |
9
|
|
|
use Hyde\Framework\Concerns\FrontMatter\Schemas\PageSchema; |
10
|
|
|
use Hyde\Framework\Hyde; |
11
|
|
|
use Hyde\Framework\Models\Author; |
12
|
|
|
use Hyde\Framework\Models\DateString; |
13
|
|
|
use Hyde\Framework\Models\Image; |
14
|
|
|
use Illuminate\Support\Str; |
15
|
|
|
|
16
|
|
|
trait ConstructsPageSchemas |
17
|
|
|
{ |
18
|
|
|
protected function constructPageSchemas(): void |
19
|
|
|
{ |
20
|
|
|
if ($this->usesSchema(PageSchema::class)) { |
21
|
|
|
$this->constructPageSchema(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
if ($this->usesSchema(BlogPostSchema::class)) { |
25
|
|
|
$this->constructBlogPostSchema(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
if ($this->usesSchema(DocumentationPageSchema::class)) { |
29
|
|
|
$this->constructDocumentationPageSchema(); |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
protected function constructPageSchema(): void |
34
|
|
|
{ |
35
|
|
|
$this->title = FindsTitleForPage::run($this); |
|
|
|
|
36
|
|
|
$this->navigation = FindsNavigationDataForPage::run($this); |
|
|
|
|
37
|
|
|
$this->canonicalUrl = $this->makeCanonicalUrl(); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function makeCanonicalUrl(): ?string |
41
|
|
|
{ |
42
|
|
|
if (! empty($this->matter('canonicalUrl'))) { |
|
|
|
|
43
|
|
|
return $this->matter('canonicalUrl'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if (Hyde::hasSiteUrl() && ! empty($this->identifier)) { |
47
|
|
|
return $this->getRoute()->getQualifiedUrl(); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return null; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function constructBlogPostSchema(): void |
54
|
|
|
{ |
55
|
|
|
$this->category = $this->matter('category'); |
|
|
|
|
56
|
|
|
$this->description = $this->matter('description', $this->makeDescription()); |
|
|
|
|
57
|
|
|
$this->date = $this->matter('date') !== null ? new DateString($this->matter('date')) : null; |
|
|
|
|
58
|
|
|
$this->author = $this->getAuthor(); |
|
|
|
|
59
|
|
|
$this->image = $this->getImage(); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function makeDescription(): string |
63
|
|
|
{ |
64
|
|
|
if (strlen($this->markdown) >= 128) { |
65
|
|
|
return substr($this->markdown, 0, 125).'...'; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return (string) $this->markdown; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function getAuthor(): ?Author |
72
|
|
|
{ |
73
|
|
|
if ($this->matter('author')) { |
74
|
|
|
return Author::make($this->matter('author')); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return null; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
protected function getImage(): ?Image |
81
|
|
|
{ |
82
|
|
|
if ($this->matter('image')) { |
83
|
|
|
return Image::make($this->matter('image')); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
protected function constructDocumentationPageSchema(): void |
90
|
|
|
{ |
91
|
|
|
$this->category = $this->getDocumentationPageCategory(); |
|
|
|
|
92
|
|
|
|
93
|
|
|
$this->label = $this->matter('label', Hyde::makeTitle(basename($this->identifier))); |
|
|
|
|
94
|
|
|
$this->hidden = $this->matter('hidden', $this->identifier === 'index'); |
|
|
|
|
95
|
|
|
$this->priority = $this->matter('priority', $this->findPriorityInConfig()); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
protected function getDocumentationPageCategory(): ?string |
99
|
|
|
{ |
100
|
|
|
// If the documentation page is in a subdirectory, |
101
|
|
|
// then we can use that as the category name. |
102
|
|
|
// Otherwise, we look in the front matter. |
103
|
|
|
|
104
|
|
|
return str_contains($this->identifier, '/') |
105
|
|
|
? Str::before($this->identifier, '/') |
106
|
|
|
: $this->matter('category', 'other'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
protected function findPriorityInConfig(): int |
110
|
|
|
{ |
111
|
|
|
$orderIndexArray = config('docs.sidebar_order', []); |
112
|
|
|
|
113
|
|
|
if (! in_array($this->identifier, $orderIndexArray)) { |
114
|
|
|
return 500; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return array_search($this->identifier, $orderIndexArray) + 250; |
118
|
|
|
|
119
|
|
|
// Adding 250 makes so that pages with a front matter priority that is lower |
120
|
|
|
// can be shown first. It's lower than the fallback of 500 so that they |
121
|
|
|
// still come first. This is all to make it easier to mix priorities. |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
protected function usesSchema(string $schema): bool |
125
|
|
|
{ |
126
|
|
|
return in_array($schema, class_uses_recursive($this)); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|