Skip to content

Commit f233c28

Browse files
committed
Fix #310, allow items to be hidden from sidebar with front matter
1 parent 7e45329 commit f233c28

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

src/Models/DocumentationSidebarItem.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ class DocumentationSidebarItem
1515
public string $label;
1616
public string $destination;
1717
public int $priority;
18+
public bool $hidden = false;
1819

19-
public function __construct(string $label, string $destination, ?int $priority = null)
20+
public function __construct(string $label, string $destination, ?int $priority = null, bool $hidden = false)
2021
{
2122
$this->label = $label;
2223
$this->destination = $destination;
2324
$this->priority = $priority ?? $this->findPriorityInConfig($destination);
25+
$this->hidden = $hidden;
2426
}
2527

2628
protected function findPriorityInConfig(string $slug): int
@@ -34,6 +36,11 @@ protected function findPriorityInConfig(string $slug): int
3436
return array_search($slug, $orderIndexArray); // + 250?
3537
}
3638

39+
public function isHidden(): bool
40+
{
41+
return $this->hidden;
42+
}
43+
3744
public static function parseFromFile(string $documentationPageSlug): static
3845
{
3946
$matter = YamlFrontMatter::markdownCompatibleParse(
@@ -43,7 +50,8 @@ public static function parseFromFile(string $documentationPageSlug): static
4350
return new static(
4451
$matter['label'] ?? Hyde::titleFromSlug($documentationPageSlug),
4552
$documentationPageSlug,
46-
$matter['priority'] ?? null
53+
$matter['priority'] ?? null,
54+
$matter['hidden'] ?? false
4755
);
4856
}
4957
}

src/Services/DocumentationSidebarService.php

+13-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class DocumentationSidebarService implements DocumentationSidebarServiceContract
2323
*/
2424
public static function get(): DocumentationSidebar
2525
{
26-
return ((new static)->createSidebar()->withoutIndex()->getSidebar()
26+
return ((new static)->createSidebar()->withoutIndex()->withoutHidden()->getSidebar()
2727
)->sortItems()->getCollection();
2828
}
2929

@@ -63,6 +63,18 @@ protected function withoutIndex(): self
6363
return $this;
6464
}
6565

66+
/**
67+
* Remove hidden files from the sidebar collection.
68+
*/
69+
protected function withoutHidden(): self
70+
{
71+
$this->sidebar = $this->sidebar->reject(function (DocumentationSidebarItem $item) {
72+
return $item->isHidden();
73+
});
74+
75+
return $this;
76+
}
77+
6678
/**
6779
* Get an array of source files to add to the sidebar.
6880
*/

tests/Feature/Services/DocumentationSidebarServiceTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ public function test_index_page_is_removed_from_sidebar()
6868
$this->assertCount(5, $sidebar);
6969
}
7070

71+
public function test_files_with_front_matter_hidden_set_to_true_are_removed_from_sidebar()
72+
{
73+
$this->createTestFiles();
74+
File::put(Hyde::path('_docs/test.md'), "---\nhidden: true\n---\n\n# Foo");
75+
76+
$sidebar = DocumentationSidebarService::get();
77+
$this->assertCount(5, $sidebar);
78+
}
79+
7180
public function test_sidebar_is_ordered_alphabetically_when_no_order_is_set_in_config()
7281
{
7382
Config::set('hyde.documentationPageOrder', []);

0 commit comments

Comments
 (0)