Skip to content

Commit

Permalink
Merge pull request #187 from hydephp/create-navigation-menu-generatio…
Browse files Browse the repository at this point in the history
…n-service

Convert navigation menu generation to service
  • Loading branch information
caendesilva committed Jul 7, 2022
2 parents 21dc056 + 4513b33 commit 25192c7
Show file tree
Hide file tree
Showing 19 changed files with 734 additions and 287 deletions.
2 changes: 1 addition & 1 deletion docs/customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ One of my (the author's) favourite features with Hyde is its automatic navigatio
#### How it works:
The sidebar works by creating a list of all the documentation pages.

The navigation menu is a bit more sophisticated, it adds all the top-level Blade and Markdown pages. It also adds an automatic link to the docs if there is an `index.md` or `readme.md` in the `_docs` directory.
The navigation menu is a bit more sophisticated, it adds all the top-level Blade and Markdown pages. It also adds an automatic link to the docs if there is an `index.md` in the `_docs` directory.

#### Reordering Items
Sadly, Hyde is not intelligent enough to determine what order items should be in (blame Dr Jekyll for this), so you will probably want to set a custom order.
Expand Down
6 changes: 3 additions & 3 deletions docs/documentation-pages.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ What is "the rest", you may ask? Well, for starters:

- Hyde compiles your Markdown content into a beautiful static HTML page based on [the Lagrafo frontend](https://github.com/caendesilva/lagrafo)
- A sidebar (which is responsive) is automatically created based on your Markdown files
- If you have an `index.md` or `readme.md` in the `_docs/` directory, it will be used as the sidebar header
- If you have an `index.md` in the `_docs/` directory, it will be used as the sidebar header
- You can even [customize the order and labels](#sidebar-page-order) of sidebar items
- If you have an `index.md` or `readme.md` in the `_docs/` directory,
- If you have an `index.md` in the `_docs/` directory,
a link to it will be added to the site navigation menu named "Docs".
- If you have a Torchlight API token in your .env file, Hyde will even automatically enable Syntax Highlighting for you.
See more about this in the [extensions page](extensions.html#torchlight).
Expand Down Expand Up @@ -171,7 +171,7 @@ for example to specify a version like the Hyde docs does, you can specify the ou

### Automatic navigation menu

By default, a link to the documentation page is added to the navigation menu when an index.md or readme.md file is found in the `_docs` directory.
By default, a link to the documentation page is added to the navigation menu when an index.md file is found in the `_docs` directory.
In version v0.38.0-beta and lower, this link had the internal priority of 500 putting it to the left of the automatic menu. In v0.39.0-beta and higher, the priority is set to 1000 to be placed at the end of the menu. See the reasoning behind this in [this GitHub issue](https://github.com/hydephp/develop/issues/24).

You can customize the priority using the following config value in the `config/docs.php` file:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<a href="{{ $item['route'] }}" {{ $item['current'] ? 'aria-current="page"' : '' }}
@php
/** @var \Hyde\Framework\Contracts\PageContract $page */
$isCurrent = $page->getRoute()->getRouteKey() === $item->route->getRouteKey();
@endphp

<a href="{{ $item->resolveLink($currentPage) }}" {{ $isCurrent ? 'aria-current="page"' : '' }}
@class(['block my-2 md:my-0 md:inline-block py-1 text-gray-700 hover:text-gray-900 dark:text-gray-100'
, 'border-l-4 border-indigo-500 md:border-none font-medium -ml-6 pl-5 md:ml-0 md:pl-0 bg-gray-100 dark:bg-gray-800 md:bg-transparent dark:md:bg-transparent'=> $item['current']
, 'border-l-4 border-indigo-500 md:border-none font-medium -ml-6 pl-5 md:ml-0 md:pl-0 bg-gray-100 dark:bg-gray-800 md:bg-transparent dark:md:bg-transparent'=> $isCurrent
])>
{{ $item['title'] }}
{{ $item->title }}
</a>
Empty file.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@php
$links = Hyde\Framework\Actions\GeneratesNavigationMenu::getNavigationLinks($currentPage);
$homeRoute = ($links[array_search('Home', array_column($links, 'title'))])['route'] ?? Hyde::pageLink('index.html');
$navigation = \Hyde\Framework\Modules\Navigation\NavigationMenu::create($page->getRoute());
$homeRoute = $navigation->homeRoute->getLink($currentPage);
@endphp

<nav aria-label="Main navigation" id="main-navigation" class="flex flex-wrap items-center justify-between p-4 shadow-lg sm:shadow-xl md:shadow-none dark:bg-gray-800">
Expand All @@ -21,7 +21,7 @@

<div id="main-navigation-links" class="w-full hidden md:flex flex-grow md:flex-grow-0 md:items-center md:w-auto px-6 -mx-4 border-t mt-3 pt-3 md:border-none md:mt-0 md:py-0 border-gray-200 dark:border-gray-700">
<ul aria-label="Navigation links" class="md:flex-grow md:flex justify-end">
@foreach ($links as $item)
@foreach ($navigation->items as $item)
<li class="md:mx-2">
@include('hyde::components.navigation.navigation-link')
</li>
Expand Down
188 changes: 0 additions & 188 deletions packages/framework/src/Actions/GeneratesNavigationMenu.php

This file was deleted.

118 changes: 118 additions & 0 deletions packages/framework/src/Concerns/CanBeInNavigation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace Hyde\Framework\Concerns;

use Hyde\Framework\Contracts\AbstractMarkdownPage;
use Hyde\Framework\Hyde;
use Hyde\Framework\Models\Pages\DocumentationPage;
use Hyde\Framework\Models\Pages\MarkdownPost;

/**
* Offloads logic related to navigation menu items for AbstractPage classes.
*
* @see \Hyde\Framework\Testing\Feature\Concerns\CanBeInNavigationTest
*/
trait CanBeInNavigation
{
/**
* Should the item should be displayed in the navigation menu?
*
* @return bool
*/
public function showInNavigation(): bool
{
if ($this instanceof MarkdownPost) {
return false;
}

if ($this instanceof DocumentationPage) {
return $this->slug === 'index';
}

if ($this instanceof AbstractMarkdownPage) {
if ($this->markdown->matter('navigation.hidden', false)) {
return false;
}
}

if (in_array($this->slug, config('hyde.navigation.exclude', ['404']))) {
return false;
}

return true;
}

/**
* The relative priority, determining the position of the item in the menu.
*
* @return int
*/
public function navigationMenuPriority(): int
{
if ($this instanceof AbstractMarkdownPage) {
if ($this->matter('navigation.priority') !== null) {
return $this->matter('navigation.priority');
}
}

if (array_key_exists($this->slug, config('hyde.navigation.order', []))) {
return (int) config('hyde.navigation.order.'.$this->slug);
}

if ($this instanceof DocumentationPage) {
return 100;
}

if ($this->slug === 'index') {
return 0;
}

if ($this->slug === 'posts') {
return 10;
}

return 1000;
}

/**
* The page title to display in the navigation menu.
*
* @return string
*/
public function navigationMenuTitle(): string
{
if ($this instanceof AbstractMarkdownPage) {
if ($this->matter('navigation.title') !== null) {
return $this->matter('navigation.title');
}

if ($this->matter('title') !== null) {
return $this->matter('title');
}
}

if ($this->slug === 'index') {
if ($this instanceof DocumentationPage) {
return 'Docs';
}

return 'Home';
}

if (isset($this->title) && ! blank($this->title)) {
return $this->title;
}

return Hyde::makeTitle($this->slug);
}

/**
* Not yet implemented.
*
* If an item returns a route collection,
* it will automatically be made into a dropdown.
*
* @return \Illuminate\Support\Collection<\Hyde\Framework\Modules\Routing\Route>
*/
// public function navigationMenuChildren(): Collection;
}
5 changes: 5 additions & 0 deletions packages/framework/src/Contracts/AbstractMarkdownPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ public function markdown(): MarkdownDocument
{
return $this->markdown;
}

public function matter(string $key = null, mixed $default = null): mixed
{
return $this->markdown->matter($key, $default);
}
}
Loading

0 comments on commit 25192c7

Please sign in to comment.