Skip to content

Commit

Permalink
Merge pull request #311 from hydephp/310-implement-hidden-true-front-…
Browse files Browse the repository at this point in the history
…matter-to-hide-documentation-pages-from-sidebar

Fix #310, allow documentation pages to be hidden from sidebar using front matter
  • Loading branch information
caendesilva authored May 10, 2022
2 parents 7e45329 + f233c28 commit dcdac99
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/Models/DocumentationSidebarItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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
);
}
}
14 changes: 13 additions & 1 deletion src/Services/DocumentationSidebarService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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.
*/
Expand Down
9 changes: 9 additions & 0 deletions tests/Feature/Services/DocumentationSidebarServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', []);
Expand Down

0 comments on commit dcdac99

Please sign in to comment.