Skip to content

Commit

Permalink
Merge pull request #6 from AbdelElrafa/fix-routing
Browse files Browse the repository at this point in the history
Fix routing and redirect the base route to the first doc.
  • Loading branch information
AbdelElrafa committed Sep 30, 2023
2 parents f5971cf + 0d60b6b commit d9ae424
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/DocsPanelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class DocsPanelServiceProvider extends PackageServiceProvider
/**
* @var array<int, string>
*/
protected static $groupsOrder = [];
public static $groupsOrder = [];

/**
* @var array<\Closure>
Expand Down Expand Up @@ -110,7 +110,7 @@ public function panel(Panel $panel): Panel
->each(function ($docs, $group) use (&$navigationGroups, $panel) {
$navigationItems = [];
foreach ($docs as $file) {
$routePath = rtrim(filament()->getPanel(self::$name)->getPath(), '/') . '/' . $file['route_path'];
$routePath = rtrim(filament()->getPanel(self::$name)->getPath(), '/') . '/' . $file['slug'];

if (empty($routeIsActive)) {
$routeIsActive = request()->routeIs("filament.{$panel}.pages.{$file['slug']}");
Expand Down
22 changes: 17 additions & 5 deletions src/Pages/DocsPages.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,23 @@ protected function getViewData(): array

public static function routes(Panel $panel): void
{
$files = static::getDocs();

$files = collect(static::getDocs())
->sortBy('title')
->sortBy('order')
->groupBy('group')
->sortKeys()
->sortBy(fn ($docs, $group) => ($order = array_search($group, DocsPanelServiceProvider::$groupsOrder)) !== false ? $order : PHP_INT_MAX)
->flatten(1);

// Register the root route to redirect to the first page.
Route::get('/', fn () => redirect()->route("filament.{$panel->getId()}.pages.{$files->first()['slug']}"))
->middleware(static::getRouteMiddleware($panel))
->withoutMiddleware(static::getWithoutRouteMiddleware($panel))
->name('index');

foreach ($files as $file) {
$routePath = $file['route_path'];
$routePath = $file['slug'];

Route::get("/{$routePath}", static::class)
->middleware(static::getRouteMiddleware($panel))
Expand All @@ -57,7 +70,7 @@ public static function routes(Panel $panel): void
}

/**
* @return array<array{path: string, route_path: string, slug: string, group: string, order: int, title: string, content: string}>
* @return array<array{path: string, slug: string, group: string, order: int, title: string, content: string}>
*/
public static function getDocs(): array
{
Expand Down Expand Up @@ -89,8 +102,7 @@ public static function getDocs(): array

$docs[] = [
'path' => $file->getRelativePathname(),
'route_path' => str_replace(['index.md', '.md'], '', $file->getRelativePathname()),
'slug' => $object->matter('slug') ?: str_replace(['index.md', '.md'], '', $file->getRelativePathname()) ?: 'index',
'slug' => $object->matter('slug') ?: Str::of($file->getRelativePathname())->replace(['index.md', '.md'], '')->afterLast(DIRECTORY_SEPARATOR) ?: 'index',
'group' => $object->matter('group') ?: (Str::contains($file->getRelativePathname(), '/') ? Str::of($file->getRelativePathname())->before('/')->headline() : ''),
'order' => $object->matter('order') ?: 0,
'title' => $title ?: 'Get Started',
Expand Down

0 comments on commit d9ae424

Please sign in to comment.