From f233c28b32a30c000d4703ff896d4977e669d78f Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Tue, 10 May 2022 21:53:57 +0200 Subject: [PATCH] Fix #310, allow items to be hidden from sidebar with front matter --- src/Models/DocumentationSidebarItem.php | 12 ++++++++++-- src/Services/DocumentationSidebarService.php | 14 +++++++++++++- .../Services/DocumentationSidebarServiceTest.php | 9 +++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/Models/DocumentationSidebarItem.php b/src/Models/DocumentationSidebarItem.php index a8bb1f13..7e0a5770 100644 --- a/src/Models/DocumentationSidebarItem.php +++ b/src/Models/DocumentationSidebarItem.php @@ -15,12 +15,14 @@ class DocumentationSidebarItem public string $label; public string $destination; public int $priority; + public bool $hidden = false; - public function __construct(string $label, string $destination, ?int $priority = null) + public function __construct(string $label, string $destination, ?int $priority = null, bool $hidden = false) { $this->label = $label; $this->destination = $destination; $this->priority = $priority ?? $this->findPriorityInConfig($destination); + $this->hidden = $hidden; } protected function findPriorityInConfig(string $slug): int @@ -34,6 +36,11 @@ protected function findPriorityInConfig(string $slug): int return array_search($slug, $orderIndexArray); // + 250? } + public function isHidden(): bool + { + return $this->hidden; + } + public static function parseFromFile(string $documentationPageSlug): static { $matter = YamlFrontMatter::markdownCompatibleParse( @@ -43,7 +50,8 @@ public static function parseFromFile(string $documentationPageSlug): static return new static( $matter['label'] ?? Hyde::titleFromSlug($documentationPageSlug), $documentationPageSlug, - $matter['priority'] ?? null + $matter['priority'] ?? null, + $matter['hidden'] ?? false ); } } diff --git a/src/Services/DocumentationSidebarService.php b/src/Services/DocumentationSidebarService.php index 5722e2e0..60dafa55 100644 --- a/src/Services/DocumentationSidebarService.php +++ b/src/Services/DocumentationSidebarService.php @@ -23,7 +23,7 @@ class DocumentationSidebarService implements DocumentationSidebarServiceContract */ public static function get(): DocumentationSidebar { - return ((new static)->createSidebar()->withoutIndex()->getSidebar() + return ((new static)->createSidebar()->withoutIndex()->withoutHidden()->getSidebar() )->sortItems()->getCollection(); } @@ -63,6 +63,18 @@ protected function withoutIndex(): self return $this; } + /** + * Remove hidden files from the sidebar collection. + */ + protected function withoutHidden(): self + { + $this->sidebar = $this->sidebar->reject(function (DocumentationSidebarItem $item) { + return $item->isHidden(); + }); + + return $this; + } + /** * Get an array of source files to add to the sidebar. */ diff --git a/tests/Feature/Services/DocumentationSidebarServiceTest.php b/tests/Feature/Services/DocumentationSidebarServiceTest.php index 74593f51..6345726a 100644 --- a/tests/Feature/Services/DocumentationSidebarServiceTest.php +++ b/tests/Feature/Services/DocumentationSidebarServiceTest.php @@ -68,6 +68,15 @@ public function test_index_page_is_removed_from_sidebar() $this->assertCount(5, $sidebar); } + public function test_files_with_front_matter_hidden_set_to_true_are_removed_from_sidebar() + { + $this->createTestFiles(); + File::put(Hyde::path('_docs/test.md'), "---\nhidden: true\n---\n\n# Foo"); + + $sidebar = DocumentationSidebarService::get(); + $this->assertCount(5, $sidebar); + } + public function test_sidebar_is_ordered_alphabetically_when_no_order_is_set_in_config() { Config::set('hyde.documentationPageOrder', []);