Skip to content

Commit a524c17

Browse files
authored
Merge pull request #1472 from hydephp/add-documentation-index-page-to-sidebar-when-it-is-the-only-page
Add documentation index page to the sidebar when it's the only page
2 parents 34eaffb + 300694c commit a524c17

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

packages/framework/src/Framework/Features/Navigation/DocumentationSidebar.php

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ protected function generate(): void
2222
$this->items->put($route->getRouteKey(), NavItem::fromRoute($route));
2323
}
2424
});
25+
26+
// If there are no pages other than the index page, we add it to the sidebar so that it's not empty
27+
if ($this->items->count() === 0 && DocumentationPage::home() !== null) {
28+
$this->items->push(NavItem::fromRoute(DocumentationPage::home(), group: 'other'));
29+
}
2530
}
2631

2732
public function hasGroups(): bool

packages/framework/tests/Feature/Services/DocumentationSidebarTest.php

+25
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,31 @@ public function test_is_group_active_for_index_page_with_no_groups()
376376
$this->assertFalse(DocumentationSidebar::create()->isGroupActive('foo'));
377377
}
378378

379+
public function test_index_page_added_to_sidebar_when_it_is_the_only_page()
380+
{
381+
Filesystem::touch('_docs/index.md');
382+
$sidebar = DocumentationSidebar::create();
383+
384+
$this->assertCount(1, $sidebar->items);
385+
$this->assertEquals(
386+
collect([NavItem::fromRoute(Routes::get('docs/index'))]),
387+
$sidebar->items
388+
);
389+
}
390+
391+
public function test_index_page_not_added_to_sidebar_when_other_pages_exist()
392+
{
393+
$this->createTestFiles(1);
394+
Filesystem::touch('_docs/index.md');
395+
$sidebar = DocumentationSidebar::create();
396+
397+
$this->assertCount(1, $sidebar->items);
398+
$this->assertEquals(
399+
collect([NavItem::fromRoute(Routes::get('docs/test-0'))]),
400+
$sidebar->items
401+
);
402+
}
403+
379404
protected function createTestFiles(int $count = 5): void
380405
{
381406
for ($i = 0; $i < $count; $i++) {

0 commit comments

Comments
 (0)