Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overhaul the navigation menu to add configuration options #27

Merged
merged 4 commits into from
Mar 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 70 additions & 7 deletions app/Actions/GeneratesNavigationMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Actions;

use App\Hyde\Features;
use App\Hyde\Models\MarkdownPage;
use Illuminate\Support\Str;

Expand Down Expand Up @@ -43,27 +44,89 @@ public function __construct(string $currentPage)

/**
* Create the link array
*
* @todo Cache the base array and only update the 'current' attribute on each request.
*
* @return array
*/
private function getLinks(): array
{
$links = [];
$links = $this->getLinksFromConfig();

// Automatically add top level pages
foreach ($this->getListOfCustomPages() as $slug) {
$links[] = [
'title' => $this->getTitleFromSlug($slug),
'route' => $this->getRelativeRoutePathForSlug($slug),
'current' => $this->currentPage == $slug,
'priority' => $slug == "index" ? 100 : 999,
];
$title = $this->getTitleFromSlug($slug);
// Only add the automatic link if it is not present in the config array
if (!in_array($title, array_column($links, 'title'))) {
$links[] = [
'title' => $title,
'route' => $this->getRelativeRoutePathForSlug($slug),
'current' => $this->currentPage == $slug,
'priority' => $slug == "index" ? 100 : 999,
];
}
}

// Add extra links

// If the documentation feature is enabled...
if (Features::hasDocumentationPages()) {
// And there is no link to the docs...
if (!in_array('Docs', array_column($links, 'title'))) {
// But a suitable file exists...
if (file_exists('_docs/index.md') || file_exists('_docs/readme.md')) {
// Then we can add a link.
$links[] = [
'title' => 'Docs',
'route' => $this->getRelativeRoutePathForSlug(
file_exists('_docs/index.md') ? 'docs/index' : 'docs/readme'
),
'current' => false,
'priority' => 500,
];
}
}
}

// Remove config defined blacklisted links
foreach ($links as $key => $link) {
if (in_array(Str::slug($link['title']), config('hyde.navigationMenuBlacklist', []))) {
unset($links[$key]);
}
}

// Sort

$columns = array_column($links, 'priority');
array_multisort($columns, SORT_ASC, $links);

return $links;
}

/**
* Get the custom navigation links from the config, if there are any
* @return array
*/
private function getLinksFromConfig(): array
{
$configLinks = config('hyde.navigationMenuLinks', []);

$links = [];

if (sizeof($configLinks) > 0) {
foreach ($configLinks as $link) {
$links[] = [
'title' => $link['title'],
'route' => $link['destination'] ?? $this->getRelativeRoutePathForSlug($link['slug']),
'current' => isset($link['slug']) ? $this->currentPage == $link['slug'] : false,
'priority' => $link['priority'] ?? 999,
];
}
}

return $links;
}

/**
* Get the page title
*
Expand Down
89 changes: 0 additions & 89 deletions app/Commands/MakeNavigationLinkCommand.php

This file was deleted.

44 changes: 43 additions & 1 deletion config/hyde.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,49 @@

/*
|--------------------------------------------------------------------------
| Documentation sidebar page order
| Custom Navigation Menu Links
|--------------------------------------------------------------------------
|
| If you are looking to add custom navigation menu links, this is the place!
|
| Linking to an external site? Supply the full URI to the 'destination'.
| Keeping it internal? Pass the 'slug' relative to the document root.
|
| To get started quickly, you can uncomment the defaults here.
| Tip: Only the title and slug parameters are required.
|
*/

'navigationMenuLinks' => [
// [
// 'title' => 'GitHub',
// 'destination' => 'https://github.com/hydephp/hyde',
// 'priority' => 1200,
// ],
// [
// 'title' => 'Featured Blog Post',
// 'slug' => 'posts/hello-world',
// ]
],


/*
|--------------------------------------------------------------------------
| Navigation Menu Blacklist
|--------------------------------------------------------------------------
| There may be pages you want to exclude from the automatic navigation menu,
| such as error pages. Add their slugs here and they will not be included.
|
*/

'navigationMenuBlacklist' => [
'404'
],


/*
|--------------------------------------------------------------------------
| Documentation Sidebar Page Order
|--------------------------------------------------------------------------
|
| In the generated Documentation pages the navigation links in the sidebar
Expand Down