Skip to content

Commit 3d2d696

Browse files
authored
Merge pull request #1703 from hydephp/enable-dropdowns-when-set-in-front-matter-regardless-of-subdirectory-configuration
Honour front matter navigation groups set in front matter regardless of subdirectory setting
2 parents 575989a + ee7e8a6 commit 3d2d696

File tree

4 files changed

+81
-3
lines changed

4 files changed

+81
-3
lines changed

RELEASE_NOTES.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ This serves two purposes:
1515
- 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
1616

1717
### Changed
18-
- for changes in existing functionality.
18+
- 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)
1919

2020
### Deprecated
2121
- for soon-to-be removed features.
@@ -24,7 +24,7 @@ This serves two purposes:
2424
- for now removed features.
2525

2626
### Fixed
27-
- for any bug fixes.
27+
- 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
2828

2929
### Security
3030
- in case of vulnerabilities.

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
class NavigationMenu extends BaseNavigationMenu
1313
{
14+
private bool $hasDropdowns;
15+
1416
protected function generate(): void
1517
{
1618
parent::generate();
@@ -69,6 +71,13 @@ protected function canAddItemToDropdown(NavItem $item): bool
6971

7072
protected function dropdownsEnabled(): bool
7173
{
72-
return Config::getString('hyde.navigation.subdirectories', 'hidden') === 'dropdown';
74+
return (Config::getString('hyde.navigation.subdirectories', 'hidden') === 'dropdown') || $this->hasGroupExplicitlySetInFrontMatter();
75+
}
76+
77+
private function hasGroupExplicitlySetInFrontMatter(): bool
78+
{
79+
return $this->hasDropdowns ??= $this->items->contains(function (NavItem $item): bool {
80+
return ($item->getGroup() !== null) && ($item->destination !== (string) DocumentationPage::home());
81+
});
7382
}
7483
}

packages/framework/tests/Feature/NavigationMenuTest.php

+34
Original file line numberDiff line numberDiff line change
@@ -383,4 +383,38 @@ public function testDropdownMenuItemsAreSortedByPriority()
383383

384384
$this->assertSame(['Foo', 'Bar', 'Baz'], $dropdowns[0]->getItems()->pluck('label')->toArray());
385385
}
386+
387+
public function testHasDropdownsReturnsTrueWhenGroupIsExplicitlySetInFrontMatter()
388+
{
389+
config(['hyde.navigation.subdirectories' => 'hidden']);
390+
391+
Routes::addRoute((new MarkdownPage('foo', matter: ['navigation.group' => 'test-group']))->getRoute());
392+
393+
$this->assertTrue(NavigationMenu::create()->hasDropdowns());
394+
}
395+
396+
public function testGetDropdownsReturnsCorrectArrayWhenGroupIsExplicitlySetInFrontMatter()
397+
{
398+
config(['hyde.navigation.subdirectories' => 'hidden']);
399+
400+
Routes::addRoute((new MarkdownPage('foo', matter: ['navigation.group' => 'test-group']))->getRoute());
401+
402+
$menu = NavigationMenu::create();
403+
$this->assertCount(1, $menu->getDropdowns());
404+
405+
$this->assertEquals([
406+
DropdownNavItem::fromArray('test-group', [
407+
NavItem::fromRoute((new MarkdownPage('foo'))->getRoute()),
408+
]),
409+
], $menu->getDropdowns());
410+
}
411+
412+
public function testHasDropdownsReturnsFalseWhenGroupIsNotExplicitlySetInFrontMatter()
413+
{
414+
config(['hyde.navigation.subdirectories' => 'hidden']);
415+
416+
Routes::addRoute((new MarkdownPage('foo'))->getRoute());
417+
$menu = NavigationMenu::create();
418+
$this->assertFalse($menu->hasDropdowns());
419+
}
386420
}

packages/framework/tests/Unit/NavigationDataFactoryUnitTest.php

+35
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,36 @@ public function testRouteKeysCanBeUsedForDocumentationSidebarPriorities()
146146
$this->assertSame(502, $factory->makePriority());
147147
}
148148

149+
public function testMakeGroupUsesFrontMatterGroupIfSet()
150+
{
151+
$frontMatter = new FrontMatter(['navigation.group' => 'Test Group']);
152+
$coreDataObject = new CoreDataObject($frontMatter, new Markdown(), MarkdownPage::class, 'test.md', '', '', '');
153+
$factory = new NavigationConfigTestClass($coreDataObject);
154+
155+
$this->assertSame('Test Group', $factory->makeGroup());
156+
}
157+
158+
public function testMakeGroupUsesFrontMatterGroupIfSetRegardlessOfSubdirectoryConfiguration()
159+
{
160+
self::mockConfig(['hyde.navigation.subdirectories' => 'hidden']);
161+
162+
$frontMatter = new FrontMatter(['navigation.group' => 'Test Group']);
163+
$coreDataObject = new CoreDataObject($frontMatter, new Markdown(), MarkdownPage::class, 'test.md', '', '', '');
164+
$factory = new NavigationConfigTestClass($coreDataObject);
165+
166+
$this->assertSame('Test Group', $factory->makeGroup());
167+
}
168+
169+
public function testMakeGroupDefaultsToNullIfFrontMatterGroupNotSetAndSubdirectoriesNotUsed()
170+
{
171+
self::mockConfig(['hyde.navigation.subdirectories' => 'hidden']);
172+
173+
$coreDataObject = new CoreDataObject(new FrontMatter(), new Markdown(), MarkdownPage::class, 'test.md', '', '', '');
174+
$factory = new NavigationConfigTestClass($coreDataObject);
175+
176+
$this->assertNull($factory->makeGroup());
177+
}
178+
149179
protected function makeCoreDataObject(string $identifier = '', string $routeKey = '', string $pageClass = MarkdownPage::class): CoreDataObject
150180
{
151181
return new CoreDataObject(new FrontMatter(), new Markdown(), $pageClass, $identifier, '', '', $routeKey);
@@ -163,4 +193,9 @@ public function makePriority(): int
163193
{
164194
return parent::makePriority();
165195
}
196+
197+
public function makeGroup(): ?string
198+
{
199+
return parent::makeGroup();
200+
}
166201
}

0 commit comments

Comments
 (0)