From 5e3518c53481eed1ac0ba4b728c2344525a7e1ce Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 20 Mar 2022 19:21:29 +0100 Subject: [PATCH 1/4] Fix #25, automatically add link to docs --- app/Actions/GeneratesNavigationMenu.php | 26 +++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/app/Actions/GeneratesNavigationMenu.php b/app/Actions/GeneratesNavigationMenu.php index 69f50ef..3afbd61 100644 --- a/app/Actions/GeneratesNavigationMenu.php +++ b/app/Actions/GeneratesNavigationMenu.php @@ -2,6 +2,7 @@ namespace App\Actions; +use App\Hyde\Features; use App\Hyde\Models\MarkdownPage; use Illuminate\Support\Str; @@ -58,6 +59,31 @@ private function getLinks(): array ]; } + // 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 + + // Sort + $columns = array_column($links, 'priority'); array_multisort($columns, SORT_ASC, $links); From 8f3f5df2b5f3ed11a28761331dfc58165a1ffe10 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 20 Mar 2022 19:49:27 +0100 Subject: [PATCH 2/4] Add a navigation menu blacklist, fixes #26 --- app/Actions/GeneratesNavigationMenu.php | 5 +++++ config/hyde.php | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/app/Actions/GeneratesNavigationMenu.php b/app/Actions/GeneratesNavigationMenu.php index 3afbd61..81c4899 100644 --- a/app/Actions/GeneratesNavigationMenu.php +++ b/app/Actions/GeneratesNavigationMenu.php @@ -81,6 +81,11 @@ private function getLinks(): array } // Remove config defined blacklisted links + foreach ($links as $key => $link) { + if (in_array(Str::slug($link['title']), config('hyde.navigationMenuBlacklist', []))) { + unset($links[$key]); + } + } // Sort diff --git a/config/hyde.php b/config/hyde.php index 64c02a4..ffa4c1d 100644 --- a/config/hyde.php +++ b/config/hyde.php @@ -53,6 +53,12 @@ ], + // 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 From 1007d0de0a15064157ad7bf4bb801abfb1d2281e Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 20 Mar 2022 20:23:28 +0100 Subject: [PATCH 3/4] Implement #16, add custom navigation links --- app/Actions/GeneratesNavigationMenu.php | 54 ++++++++++++++++++++----- config/hyde.php | 42 +++++++++++++++++-- 2 files changed, 82 insertions(+), 14 deletions(-) diff --git a/app/Actions/GeneratesNavigationMenu.php b/app/Actions/GeneratesNavigationMenu.php index 81c4899..a6dfcd4 100644 --- a/app/Actions/GeneratesNavigationMenu.php +++ b/app/Actions/GeneratesNavigationMenu.php @@ -44,30 +44,38 @@ 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 the documentation feature is enabled... if (Features::hasDocumentationPages()) { - // And there is no link to the docs + // And there is no link to the docs... if (!in_array('Docs', array_column($links, 'title'))) { - // But a suitable file exists + // But a suitable file exists... if (file_exists('_docs/index.md') || file_exists('_docs/readme.md')) { - // Then we can add a link + // Then we can add a link. $links[] = [ 'title' => 'Docs', 'route' => $this->getRelativeRoutePathForSlug( @@ -95,6 +103,30 @@ private function getLinks(): array 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 * diff --git a/config/hyde.php b/config/hyde.php index ffa4c1d..976d831 100644 --- a/config/hyde.php +++ b/config/hyde.php @@ -53,15 +53,51 @@ ], - // 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. + /* + |-------------------------------------------------------------------------- + | 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 + | Documentation Sidebar Page Order |-------------------------------------------------------------------------- | | In the generated Documentation pages the navigation links in the sidebar From 3e72938c00915ecbd0e470e11f812a9814e2ea8b Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sun, 20 Mar 2022 20:24:26 +0100 Subject: [PATCH 4/4] Remove the deprecated, and now, unneeded command. --- app/Commands/MakeNavigationLinkCommand.php | 89 ---------------------- 1 file changed, 89 deletions(-) delete mode 100644 app/Commands/MakeNavigationLinkCommand.php diff --git a/app/Commands/MakeNavigationLinkCommand.php b/app/Commands/MakeNavigationLinkCommand.php deleted file mode 100644 index 50f5cc1..0000000 --- a/app/Commands/MakeNavigationLinkCommand.php +++ /dev/null @@ -1,89 +0,0 @@ -warn('This command is deprecated and will be removed as it becomes unnecessary when issue #16 is implemented.'); - - if (!Features::hasBladePages()) { - $this->warn('This feature requires BladePages to be enabled in config/hyde.php'); - return 1; - } - - $this->info('Creating a new navigation link!'); - $this->line('Tip: Navigation links are automatically created for Blade and Markdown pages!'); - - $title = $this->ask('What is the title of the navigation link?', 'Docs'); - - $type = $this->choice( - 'What kind of link do you want to create?', - ['Relative', 'External'], - 0 - ); - - $destination = $this->ask('Where should the link lead to?', - ($type == 'Relative') ? 'docs/index.html' : 'https://laravel.com/docs/'); - $this->line(($type == 'Relative') - ? 'Please enter a relative path' - : 'Please enter a full URI including https://'); - - $this->info("Creating an $type link named $title leading to $destination"); - if ($this->confirm('Do you wish to continue?', true)) { - try { - $filename = Str::slug($title); - $path = "resources/views/pages/$filename.blade.php"; - if (file_exists($path)) { - throw new Exception("File $path already exists!", 409); - } - if (file_put_contents($path, << - - -EOF -)) { - $this->line("Created navigation link $title with redirect file $path "); - } else { - throw new Exception('File was not saved'); - } - } catch (Exception $exception) { - $this->error('Something went wrong when trying to save the file!'); - $this->warn($exception->getMessage()); - return $exception->getCode() ?? 1; - } - } else { - $this->warn('Aborting.'); - return 0; - } - return 0; - } -}