Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make navigation behaviour for nested pages configurable #740

Merged
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
4 changes: 4 additions & 0 deletions config/hyde.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@
'custom' => [
// NavItem::toLink('https://github.com/hydephp/hyde', 'GitHub', 200),
],

// How should pages in subdirectories be displayed in the menu?
// You can choose between 'dropdown', 'flat', and 'hidden'.
'subdirectories' => 'hidden',
],

/*
Expand Down
4 changes: 4 additions & 0 deletions packages/framework/config/hyde.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@
'custom' => [
// NavItem::toLink('https://github.com/hydephp/hyde', 'GitHub', 200),
],

// How should pages in subdirectories be displayed in the menu?
// You can choose between 'dropdown', 'flat', and 'hidden'.
'subdirectories' => 'hidden',
],

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ protected function makeGroup(): ?string
return $this->getDocumentationPageGroup();
}

if (Str::contains($this->identifier, '/') && $this->getSubdirectoryConfiguration() === 'dropdown') {
return Str::before($this->identifier, '/');
}

// TODO Check in front matter for group?

return null;
}

Expand All @@ -100,6 +106,10 @@ protected function makeHidden(): ?bool
return true;
}

if (Str::contains($this->identifier, '/') && $this->getSubdirectoryConfiguration() === 'hidden') {
return true;
}

return false;
}

Expand Down Expand Up @@ -167,4 +177,9 @@ protected function findGroupFromMatter(): mixed
?? $this->matter('navigation.category')
?? 'other';
}

protected static function getSubdirectoryConfiguration(): string
{
return config('hyde.navigation.subdirectories', 'hidden');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public static function create(): static
/** @return $this */
public function generate(): static
{
Router::each(function (Route $route) {
Router::each(function (Route $route): void {
$this->items->push(NavItem::fromRoute($route));
});

collect(config('hyde.navigation.custom', []))->each(function (NavItem $item) {
collect(config('hyde.navigation.custom', []))->each(function (NavItem $item): void {
$this->items->push($item);
});

Expand All @@ -62,14 +62,14 @@ public function sort(): static

protected function filterHiddenItems(): Collection
{
return $this->items->reject(function (NavItem $item) {
return $this->items->reject(function (NavItem $item): bool {
return $item->hidden || $this->filterDocumentationPage($item);
})->values();
}

protected function filterDuplicateItems(): Collection
{
return $this->items->unique(function (NavItem $item) {
return $this->items->unique(function (NavItem $item): string {
return $item->resolveLink();
});
}
Expand Down
31 changes: 31 additions & 0 deletions packages/framework/tests/Feature/HydePageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,37 @@ public function test_all_pages_are_routable()
}
}

public function test_navigation_data_factory_hides_page_from_navigation_when_in_a_subdirectory()
{
$page = MarkdownPage::make('foo/bar');
$this->assertFalse($page->showInNavigation());
$this->assertNull($page->navigationMenuGroup());
}

public function test_navigation_data_factory_hides_page_from_navigation_when_in_a_and_config_is_set_to_hidden()
{
config(['hyde.navigation.subdirectories' => 'hidden']);
$page = MarkdownPage::make('foo/bar');
$this->assertFalse($page->showInNavigation());
$this->assertNull($page->navigationMenuGroup());
}

public function test_navigation_data_factory_does_not_hide_page_from_navigation_when_in_a_subdirectory_and_allowed_in_configuration()
{
config(['hyde.navigation.subdirectories' => 'flat']);
$page = MarkdownPage::make('foo/bar');
$this->assertTrue($page->showInNavigation());
$this->assertNull($page->navigationMenuGroup());
}

public function test_navigation_data_factory_allows_show_in_navigation_and_sets_group_when_dropdown_is_selected_in_config()
{
config(['hyde.navigation.subdirectories' => 'dropdown']);
$page = MarkdownPage::make('foo/bar');
$this->assertTrue($page->showInNavigation());
$this->assertEquals('foo', $page->navigationMenuGroup());
}

protected function assertSameIgnoringDirSeparatorType(string $expected, string $actual): void
{
$this->assertSame(
Expand Down
44 changes: 44 additions & 0 deletions packages/framework/tests/Feature/NavigationMenuTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,48 @@ public function test_documentation_pages_that_are_not_index_are_not_added_to_the
Hyde::unlink('_docs/foo.md');
Hyde::unlink('_docs/index.md');
}

public function test_pages_in_subdirectories_are_not_added_to_the_navigation_menu()
{
$this->directory('_pages/foo');
Hyde::touch('_pages/foo/bar.md');

$menu = NavigationMenu::create();
$expected = collect([NavItem::fromRoute(Route::get('index'))]);

$this->assertCount(count($expected), $menu->items);
$this->assertEquals($expected, $menu->items);
}

public function test_pages_in_subdirectories_can_be_added_to_the_navigation_menu_with_config_flat_setting()
{
config(['hyde.navigation.subdirectories' => 'flat']);
$this->directory('_pages/foo');
Hyde::touch('_pages/foo/bar.md');

$menu = NavigationMenu::create();
$expected = collect([
NavItem::fromRoute(Route::get('index')),
NavItem::fromRoute(Route::get('foo/bar')),
]);

$this->assertCount(count($expected), $menu->items);
$this->assertEquals($expected, $menu->items);
}

public function test_pages_in_subdirectories_can_be_added_to_the_navigation_menu_with_config_dropdown_setting()
{
config(['hyde.navigation.subdirectories' => 'dropdown']);
$this->directory('_pages/foo');
Hyde::touch('_pages/foo/bar.md');

$menu = NavigationMenu::create();
$expected = collect([
NavItem::fromRoute(Route::get('index')),
NavItem::fromRoute(Route::get('foo/bar')),
]);

$this->assertCount(count($expected), $menu->items);
$this->assertEquals($expected, $menu->items);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Hyde\Framework\Testing\Unit;

use function config;
use Hyde\Framework\Concerns\InteractsWithDirectories;
use Hyde\Hyde;
use Hyde\Testing\TestCase;
Expand All @@ -18,6 +19,7 @@ protected function setUp(): void
parent::setUp();

config(['hyde.cache_busting' => false]);
config(['hyde.navigation.subdirectories' => 'flat']);

$this->needsDirectory('_pages/nested');
$this->file('_pages/root.md');
Expand Down