1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hyde\Framework\Concerns\Internal; |
4
|
|
|
|
5
|
|
|
use Hyde\Framework\Models\NavigationData; |
6
|
|
|
use Hyde\Framework\Models\Pages\DocumentationPage; |
7
|
|
|
use Hyde\Framework\Models\Pages\MarkdownPost; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @internal Trait for HydePages to manage data used for navigation menus and the documentation sidebar. |
12
|
|
|
* |
13
|
|
|
* @see \Hyde\Framework\Concerns\HydePage |
14
|
|
|
*/ |
15
|
|
|
trait GeneratesNavigationData |
16
|
|
|
{ |
17
|
|
|
protected function constructNavigationData(): void |
18
|
|
|
{ |
19
|
|
|
$this->setNavigationData( |
20
|
|
|
$this->findNavigationMenuLabel(), |
21
|
|
|
$this->findNavigationMenuHidden(), |
22
|
|
|
$this->findNavigationMenuPriority(), |
23
|
|
|
); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
protected function constructSidebarNavigationData(): void |
27
|
|
|
{ |
28
|
|
|
$this->setNavigationData( |
29
|
|
|
$this->findNavigationMenuLabel(), |
30
|
|
|
$this->findNavigationMenuHidden(), |
31
|
|
|
$this->matter('navigation.priority', $this->findNavigationMenuPriority()), |
|
|
|
|
32
|
|
|
$this->getDocumentationPageGroup() |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function setNavigationData(string $label, bool $hidden, int $priority, ?string $group = null): void |
37
|
|
|
{ |
38
|
|
|
$this->navigation = NavigationData::make([ |
|
|
|
|
39
|
|
|
'label' => $label, |
40
|
|
|
'group' => $group, |
41
|
|
|
'hidden' => $hidden, |
42
|
|
|
'priority' => $priority, |
43
|
|
|
]); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
private function findNavigationMenuLabel(): string |
47
|
|
|
{ |
48
|
|
|
if ($this->matter('navigation.label') !== null) { |
49
|
|
|
return $this->matter('navigation.label'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (isset($this->getNavigationLabelConfig()[$this->routeKey])) { |
53
|
|
|
return $this->getNavigationLabelConfig()[$this->routeKey]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $this->matter('title') ?? $this->title; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
private function findNavigationMenuHidden(): bool |
60
|
|
|
{ |
61
|
|
|
if ($this instanceof MarkdownPost) { |
62
|
|
|
return true; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($this->matter('navigation.hidden', false)) { |
66
|
|
|
return true; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (in_array($this->routeKey, config('hyde.navigation.exclude', ['404']))) { |
70
|
|
|
return true; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function findNavigationMenuPriority(): int |
77
|
|
|
{ |
78
|
|
|
if ($this->matter('navigation.priority') !== null) { |
79
|
|
|
return $this->matter('navigation.priority'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// Different default return values are to preserve backwards compatibility |
83
|
|
|
return $this instanceof DocumentationPage |
84
|
|
|
? $this->findNavigationMenuPriorityInSidebarConfig(array_flip(config('docs.sidebar_order', []))) ?? 500 |
85
|
|
|
: $this->findNavigationMenuPriorityInNavigationConfig(config('hyde.navigation.order', [])) ?? 999; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
private function findNavigationMenuPriorityInNavigationConfig(array $config): ?int |
89
|
|
|
{ |
90
|
|
|
return array_key_exists($this->routeKey, $config) ? (int) $config[$this->routeKey] : null; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function findNavigationMenuPriorityInSidebarConfig(array $config): ?int |
94
|
|
|
{ |
95
|
|
|
// Sidebars uses a special syntax where the keys are just the page identifiers in a flat array |
96
|
|
|
return isset($config[$this->identifier]) ? $config[$this->identifier] + 250 : null; |
97
|
|
|
// Adding 250 makes so that pages with a front matter priority that is lower |
98
|
|
|
// can be shown first. It's lower than the fallback of 500 so that they |
99
|
|
|
// still come first. This is all to make it easier to mix priorities. |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
private function getNavigationLabelConfig(): array |
103
|
|
|
{ |
104
|
|
|
return array_merge([ |
105
|
|
|
'index' => 'Home', |
106
|
|
|
'docs/index' => 'Docs', |
107
|
|
|
], config('hyde.navigation.labels', [])); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
private function getDocumentationPageGroup(): ?string |
111
|
|
|
{ |
112
|
|
|
// If the documentation page is in a subdirectory, |
113
|
|
|
// then we can use that as the category name. |
114
|
|
|
// Otherwise, we look in the front matter. |
115
|
|
|
|
116
|
|
|
return str_contains($this->identifier, '/') |
117
|
|
|
? Str::before($this->identifier, '/') |
118
|
|
|
: $this->matter('navigation.group', 'other'); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|