Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function toArray(): array
protected function makeLabel(): ?string
{
return $this->searchForLabelInFrontMatter()
?? $this->searchForLabelInConfig()
?? $this->searchForLabelInConfigs()
?? $this->getMatter('title')
?? $this->title;
}
Expand All @@ -91,7 +91,7 @@ protected function makeHidden(): bool
{
return $this->isInstanceOf(MarkdownPost::class)
|| $this->searchForHiddenInFrontMatter()
|| $this->isPageHiddenInNavigationConfiguration()
|| $this->searchForHiddenInConfigs()
|| $this->isNonDocumentationPageInHiddenSubdirectory();
}

Expand Down Expand Up @@ -120,11 +120,29 @@ private function searchForHiddenInFrontMatter(): ?bool
?? $this->invert($this->getMatter('navigation.visible'));
}

private function searchForHiddenInConfigs(): ?bool
{
return $this->isInstanceOf(DocumentationPage::class)
? $this->isPageHiddenInSidebarConfiguration()
: $this->isPageHiddenInNavigationConfiguration();
}

private function isPageHiddenInNavigationConfiguration(): ?bool
{
return in_array($this->routeKey, Config::getArray('hyde.navigation.exclude', ['404']));
}

private function isPageHiddenInSidebarConfiguration(): ?bool
{
$config = Config::getArray('docs.sidebar.exclude', ['404']);

return
// Check if the page is hidden from the sidebar by route key or identifier.
(in_array($this->routeKey, $config) || in_array($this->identifier, $config))
// Check if the page is hidden from the main navigation by its route key.
|| $this->isPageHiddenInNavigationConfiguration();
}

private function isNonDocumentationPageInHiddenSubdirectory(): bool
{
return ! $this->isInstanceOf(DocumentationPage::class)
Expand All @@ -139,7 +157,14 @@ private function searchForPriorityInFrontMatter(): ?int
?? $this->getMatter('navigation.order');
}

private function searchForLabelInConfig(): ?string
private function searchForLabelInConfigs(): ?string
{
return $this->isInstanceOf(DocumentationPage::class)
? $this->searchForLabelInSidebarConfig()
: $this->searchForLabelInNavigationConfig();
}

private function searchForLabelInNavigationConfig(): ?string
{
/** @var array<string, string> $config */
$config = Config::getArray('hyde.navigation.labels', [
Expand All @@ -150,6 +175,16 @@ private function searchForLabelInConfig(): ?string
return $config[$this->routeKey] ?? null;
}

private function searchForLabelInSidebarConfig(): ?string
{
/** @var array<string>|array<string, string> $config */
$config = Config::getArray('docs.sidebar.labels', [
DocumentationPage::homeRouteName() => 'Docs',
]);

return $config[$this->routeKey] ?? $config[$this->identifier] ?? null;
}

private function searchForPriorityInConfigs(): ?int
{
return $this->isInstanceOf(DocumentationPage::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -847,8 +847,6 @@ public function testSidebarWithMixedConfigOrders()

public function testSidebarWithConfigLabels()
{
$this->markTestSkipped('Not supported (yet?)');

config(['docs.sidebar.labels' => ['foo' => 'First', 'bar' => 'Second', 'baz' => 'Third']]);

$this->assertSidebarEquals(['First', 'Second', 'Third'], [
Expand All @@ -871,8 +869,6 @@ public function testSidebarGroupLabelsCanBeSetInConfig()

public function testSidebarWithConfigHidden()
{
$this->markTestSkipped('Not supported (yet?)');

config(['docs.sidebar.exclude' => ['foo', 'bar', 'baz']]);

$this->assertSidebarEquals([], [
Expand Down
101 changes: 101 additions & 0 deletions packages/framework/tests/Unit/NavigationDataFactoryUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,97 @@ public function testRouteKeysCanBeUsedForDocumentationSidebarPriorities()
$this->assertSame(502, $factory->makePriority());
}

public function testSearchForLabelInNavigationConfigForMarkdownPage()
{
self::mockConfig([
'hyde.navigation.labels' => [
'foo' => 'Foo Label',
'bar' => 'Bar Label',
],
]);

$factory = new NavigationConfigTestClass($this->makeCoreDataObject(routeKey: 'foo'));
$this->assertSame('Foo Label', $factory->makeLabel());

$factory = new NavigationConfigTestClass($this->makeCoreDataObject(routeKey: 'bar'));
$this->assertSame('Bar Label', $factory->makeLabel());
}

public function testSearchForLabelInSidebarConfigForDocumentationPage()
{
self::mockConfig([
'docs.sidebar.labels' => [
'foo' => 'Documentation Foo Label',
'bar' => 'Documentation Bar Label',
],
]);

$factory = new NavigationConfigTestClass($this->makeCoreDataObject(routeKey: 'foo', pageClass: DocumentationPage::class));
$this->assertSame('Documentation Foo Label', $factory->makeLabel());

$factory = new NavigationConfigTestClass($this->makeCoreDataObject(routeKey: 'bar', pageClass: DocumentationPage::class));
$this->assertSame('Documentation Bar Label', $factory->makeLabel());
}

public function testLabelFallbackToTitleIfNotDefinedInConfig()
{
self::mockConfig([
'hyde.navigation.labels' => [],
'docs.sidebar.labels' => [],
]);

// Assuming the title fallback is correctly set in front matter or title property
$frontMatter = new FrontMatter(['title' => 'Fallback Title']);
$coreDataObject = new CoreDataObject($frontMatter, new Markdown(), MarkdownPage::class, '', '', '', 'undefinedKey');

$factory = new NavigationConfigTestClass($coreDataObject);
$this->assertSame('Fallback Title', $factory->makeLabel());
}

public function testPageIsHiddenBasedOnNavigationConfiguration()
{
self::mockConfig(['hyde.navigation.exclude' => ['hiddenPage']]);

$factory = new NavigationConfigTestClass($this->makeCoreDataObject(routeKey: 'hiddenPage'));
$this->assertTrue($factory->makeHidden());

$factory = new NavigationConfigTestClass($this->makeCoreDataObject(routeKey: 'visiblePage'));
$this->assertFalse($factory->makeHidden());
}

public function testPageIsHiddenBasedOnSidebarConfigurationForDocumentationPage()
{
self::mockConfig(['docs.sidebar.exclude' => ['hiddenDocPage']]);

$factory = new NavigationConfigTestClass($this->makeCoreDataObject('hiddenDocPage', pageClass: DocumentationPage::class));
$this->assertTrue($factory->makeHidden());

$factory = new NavigationConfigTestClass($this->makeCoreDataObject('visibleDocPage', pageClass: DocumentationPage::class));
$this->assertFalse($factory->makeHidden());
}

public function testSearchForHiddenInConfigsSelectsCorrectConfigurationBasedOnPageType()
{
self::mockConfig([
'hyde.navigation.exclude' => ['hiddenPage'],
'docs.sidebar.exclude' => ['hiddenDocPage'],
]);

// Test for a Markdown page, should use navigation.exclude config
$factory = new NavigationConfigTestClass($this->makeCoreDataObject(routeKey: 'hiddenPage'));
$this->assertTrue($factory->makeHidden());

$factory = new NavigationConfigTestClass($this->makeCoreDataObject(routeKey: 'visiblePage'));
$this->assertFalse($factory->makeHidden());

// Test for a Documentation page, should use docs.sidebar.exclude config
$factory = new NavigationConfigTestClass($this->makeCoreDataObject('hiddenDocPage', pageClass: DocumentationPage::class));
$this->assertTrue($factory->makeHidden());

$factory = new NavigationConfigTestClass($this->makeCoreDataObject('visibleDocPage', pageClass: DocumentationPage::class));
$this->assertFalse($factory->makeHidden());
}

protected function makeCoreDataObject(string $identifier = '', string $routeKey = '', string $pageClass = MarkdownPage::class): CoreDataObject
{
return new CoreDataObject(new FrontMatter(), new Markdown(), $pageClass, $identifier, '', '', $routeKey);
Expand All @@ -163,4 +254,14 @@ public function makePriority(): int
{
return parent::makePriority();
}

public function makeLabel(): ?string
{
return parent::makeLabel();
}

public function makeHidden(): bool
{
return parent::makeHidden();
}
}