From cc4cb1db29b8386f58ee7099f5588dc2efb970eb Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Mon, 27 Nov 2023 19:16:51 +0100 Subject: [PATCH] Add config feature to set automatic sidebar group labels Fixes https://github.com/hydephp/develop/issues/1467 --- docs/creating-content/documentation-pages.md | 16 ++++++++++++++++ .../Features/Navigation/DocumentationSidebar.php | 3 ++- .../Services/DocumentationSidebarTest.php | 10 ++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/docs/creating-content/documentation-pages.md b/docs/creating-content/documentation-pages.md index a2e36adb328..75d328d0a19 100644 --- a/docs/creating-content/documentation-pages.md +++ b/docs/creating-content/documentation-pages.md @@ -234,6 +234,22 @@ Link items without an entry here will have fall back to the default priority of See [the chapter in the customization page](customization#navigation-menu--sidebar) for more details.
+### Automatic sidebar group labels + +When using the automatic sidebar grouping feature (based on subdirectories), the titles of the groups are generated from the directory names. +If these are not to your liking, you can override them in the Docs configuration file. The array key is the directory name, and the value is the label. + +Please note that this option is not added to the config file by default, as it's not a super common use case. No worries though, just add the following yourself! + +```php +// Filepath: config/docs.php + +'sidebar_group_labels' => [ + 'questions-and-answers' => 'Questions & Answers', +], +``` + + ### Table of contents settings In the `config/docs.php` file you can configure the behavior, content, and the look and feel of the sidebar table of contents. diff --git a/packages/framework/src/Framework/Features/Navigation/DocumentationSidebar.php b/packages/framework/src/Framework/Features/Navigation/DocumentationSidebar.php index bc89ad235bc..919b93434a6 100644 --- a/packages/framework/src/Framework/Features/Navigation/DocumentationSidebar.php +++ b/packages/framework/src/Framework/Features/Navigation/DocumentationSidebar.php @@ -5,6 +5,7 @@ namespace Hyde\Framework\Features\Navigation; use Hyde\Hyde; +use Hyde\Facades\Config; use Hyde\Foundation\Facades\Routes; use Hyde\Pages\DocumentationPage; use Hyde\Support\Facades\Render; @@ -59,7 +60,7 @@ public function isGroupActive(string $group): bool public function makeGroupTitle(string $group): string { - return Hyde::makeTitle($group); + return Config::getNullableString("docs.sidebar_group_labels.$group") ?? Hyde::makeTitle($group); } protected function canAddRoute(Route $route): bool diff --git a/packages/framework/tests/Feature/Services/DocumentationSidebarTest.php b/packages/framework/tests/Feature/Services/DocumentationSidebarTest.php index 3fa4e074631..5562c1ece44 100644 --- a/packages/framework/tests/Feature/Services/DocumentationSidebarTest.php +++ b/packages/framework/tests/Feature/Services/DocumentationSidebarTest.php @@ -345,6 +345,16 @@ public function test_make_group_title_turns_group_key_into_title() $this->assertSame('Hello World', DocumentationSidebar::create()->makeGroupTitle('helloWorld')); } + public function test_make_group_title_uses_configured_sidebar_group_labels_when_available() + { + Config::set('docs.sidebar_group_labels', [ + 'example' => 'Hello world!', + ]); + + $this->assertSame('Hello world!', DocumentationSidebar::create()->makeGroupTitle('example')); + $this->assertSame('Default', DocumentationSidebar::create()->makeGroupTitle('default')); + } + public function test_can_have_multiple_grouped_pages_with_the_same_name_labels() { $this->makePage('foo', ['navigation.group' => 'foo', 'navigation.label' => 'Foo']);