Skip to content

Commit

Permalink
Merge pull request #1703 from hydephp/enable-dropdowns-when-set-in-fr…
Browse files Browse the repository at this point in the history
…ont-matter-regardless-of-subdirectory-configuration

Honour front matter navigation groups set in front matter regardless of subdirectory setting
  • Loading branch information
caendesilva committed Apr 30, 2024
2 parents 575989a + ee7e8a6 commit 3d2d696
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 3 deletions.
4 changes: 2 additions & 2 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ This serves two purposes:
- You can now specify which path to open when using the `--open` option in the serve command in https://github.com/hydephp/develop/pull/1694

### Changed
- for changes in existing functionality.
- When a navigation group is set in front matter, it will now be used regardless of the subdirectory configuration in https://github.com/hydephp/develop/pull/1703 (fixes https://github.com/hydephp/develop/issues/1515)

### Deprecated
- for soon-to-be removed features.
Expand All @@ -24,7 +24,7 @@ This serves two purposes:
- for now removed features.

### Fixed
- for any bug fixes.
- Fixed explicitly set front matter navigation group behavior being dependent on subdirectory configuration, fixing https://github.com/hydephp/develop/issues/1515 in https://github.com/hydephp/develop/pull/1703

### Security
- in case of vulnerabilities.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

class NavigationMenu extends BaseNavigationMenu
{
private bool $hasDropdowns;

protected function generate(): void
{
parent::generate();
Expand Down Expand Up @@ -69,6 +71,13 @@ protected function canAddItemToDropdown(NavItem $item): bool

protected function dropdownsEnabled(): bool
{
return Config::getString('hyde.navigation.subdirectories', 'hidden') === 'dropdown';
return (Config::getString('hyde.navigation.subdirectories', 'hidden') === 'dropdown') || $this->hasGroupExplicitlySetInFrontMatter();
}

private function hasGroupExplicitlySetInFrontMatter(): bool
{
return $this->hasDropdowns ??= $this->items->contains(function (NavItem $item): bool {
return ($item->getGroup() !== null) && ($item->destination !== (string) DocumentationPage::home());
});
}
}
34 changes: 34 additions & 0 deletions packages/framework/tests/Feature/NavigationMenuTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,4 +383,38 @@ public function testDropdownMenuItemsAreSortedByPriority()

$this->assertSame(['Foo', 'Bar', 'Baz'], $dropdowns[0]->getItems()->pluck('label')->toArray());
}

public function testHasDropdownsReturnsTrueWhenGroupIsExplicitlySetInFrontMatter()
{
config(['hyde.navigation.subdirectories' => 'hidden']);

Routes::addRoute((new MarkdownPage('foo', matter: ['navigation.group' => 'test-group']))->getRoute());

$this->assertTrue(NavigationMenu::create()->hasDropdowns());
}

public function testGetDropdownsReturnsCorrectArrayWhenGroupIsExplicitlySetInFrontMatter()
{
config(['hyde.navigation.subdirectories' => 'hidden']);

Routes::addRoute((new MarkdownPage('foo', matter: ['navigation.group' => 'test-group']))->getRoute());

$menu = NavigationMenu::create();
$this->assertCount(1, $menu->getDropdowns());

$this->assertEquals([
DropdownNavItem::fromArray('test-group', [
NavItem::fromRoute((new MarkdownPage('foo'))->getRoute()),
]),
], $menu->getDropdowns());
}

public function testHasDropdownsReturnsFalseWhenGroupIsNotExplicitlySetInFrontMatter()
{
config(['hyde.navigation.subdirectories' => 'hidden']);

Routes::addRoute((new MarkdownPage('foo'))->getRoute());
$menu = NavigationMenu::create();
$this->assertFalse($menu->hasDropdowns());
}
}
35 changes: 35 additions & 0 deletions packages/framework/tests/Unit/NavigationDataFactoryUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,36 @@ public function testRouteKeysCanBeUsedForDocumentationSidebarPriorities()
$this->assertSame(502, $factory->makePriority());
}

public function testMakeGroupUsesFrontMatterGroupIfSet()
{
$frontMatter = new FrontMatter(['navigation.group' => 'Test Group']);
$coreDataObject = new CoreDataObject($frontMatter, new Markdown(), MarkdownPage::class, 'test.md', '', '', '');
$factory = new NavigationConfigTestClass($coreDataObject);

$this->assertSame('Test Group', $factory->makeGroup());
}

public function testMakeGroupUsesFrontMatterGroupIfSetRegardlessOfSubdirectoryConfiguration()
{
self::mockConfig(['hyde.navigation.subdirectories' => 'hidden']);

$frontMatter = new FrontMatter(['navigation.group' => 'Test Group']);
$coreDataObject = new CoreDataObject($frontMatter, new Markdown(), MarkdownPage::class, 'test.md', '', '', '');
$factory = new NavigationConfigTestClass($coreDataObject);

$this->assertSame('Test Group', $factory->makeGroup());
}

public function testMakeGroupDefaultsToNullIfFrontMatterGroupNotSetAndSubdirectoriesNotUsed()
{
self::mockConfig(['hyde.navigation.subdirectories' => 'hidden']);

$coreDataObject = new CoreDataObject(new FrontMatter(), new Markdown(), MarkdownPage::class, 'test.md', '', '', '');
$factory = new NavigationConfigTestClass($coreDataObject);

$this->assertNull($factory->makeGroup());
}

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 +193,9 @@ public function makePriority(): int
{
return parent::makePriority();
}

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

0 comments on commit 3d2d696

Please sign in to comment.