Skip to content

Commit

Permalink
feat: add option to disable breadrumbs, add missing URLs in breadcrumbs
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Frey <mail@lukasfrey.cz>
  • Loading branch information
lukas-frey committed May 9, 2024
1 parent 4642cf7 commit 71e0827
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 8 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,20 @@ $plugin->slideOverPreviews();

![Modal Slideover Example](/docs/images/screenshot_modal_slideovers.jpeg)

### Enable breadcrumbs in modal preview titles
### Breadcrubs
#### Disable breadcrumbs

By default on each documentation page, there is a breadcrumb at the top. You can disable it if you wish:

```php
use Guava\FilamentKnowledgeBase\Filament\Panels\KnowledgeBasePanel;

KnowledgeBasePanel::configureUsing(
fn(KnowledgeBasePanel $panel) => $panel
->disableBreadcrumbs()
);
```
#### Enable breadcrumbs in modal preview titles

When using modal previews, by default the title shows just that, the title of the documentation page.

Expand Down
20 changes: 20 additions & 0 deletions src/Concerns/CanDisableBreadcrumbs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Guava\FilamentKnowledgeBase\Concerns;

trait CanDisableBreadcrumbs
{
protected bool $disableBreadcrumbs = false;

public function disableBreadcrumbs(bool $condition = true): static
{
$this->disableBreadcrumbs = $condition;

return $this;
}

public function shouldDisableBreadcrumbs(): bool
{
return $this->evaluate($this->disableBreadcrumbs);
}
}
4 changes: 4 additions & 0 deletions src/Filament/Pages/ViewDocumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public function getSubNavigationPosition(): SubNavigationPosition

public function getBreadcrumbs(): array
{
if (KnowledgeBase::panel()->shouldDisableBreadcrumbs()) {
return [];
}

return $this->record->getBreadcrumbs();
}

Expand Down
2 changes: 2 additions & 0 deletions src/Filament/Panels/KnowledgeBasePanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Filament\Widgets\AccountWidget;
use Guava\FilamentKnowledgeBase\Concerns\CanDisableAnchors;
use Guava\FilamentKnowledgeBase\Concerns\CanDisableBackToDefaultPanelButton;
use Guava\FilamentKnowledgeBase\Concerns\CanDisableBreadcrumbs;
use Guava\FilamentKnowledgeBase\Concerns\HasAnchorSymbol;
use Guava\FilamentKnowledgeBase\Concerns\HasArticleClass;
use Guava\FilamentKnowledgeBase\Contracts\Documentable;
Expand All @@ -42,6 +43,7 @@
class KnowledgeBasePanel extends Panel
{
use CanDisableBackToDefaultPanelButton;
use CanDisableBreadcrumbs;
use HasAnchorSymbol;
use HasArticleClass;

Expand Down
42 changes: 35 additions & 7 deletions src/Models/FlatfileDocumentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Guava\FilamentKnowledgeBase\Models;

use Arr;
use Filament\Navigation\NavigationGroup;
use Guava\FilamentKnowledgeBase\Contracts\Documentable;
use Guava\FilamentKnowledgeBase\Facades\KnowledgeBase;
use Guava\FilamentKnowledgeBase\Filament\Pages\ViewDocumentation;
Expand All @@ -15,7 +17,6 @@
use League\CommonMark\Extension\FrontMatter\Output\RenderedContentWithFrontMatter;
use League\CommonMark\Extension\HeadingPermalink\HeadingPermalink;
use League\CommonMark\Output\RenderedContentInterface;
use Spatie\StructureDiscoverer\Discover;
use Sushi\Sushi;

class FlatfileDocumentation extends Model implements Documentable
Expand Down Expand Up @@ -67,9 +68,6 @@ public function getRows()
})
->toArray()
;
collect(Discover::in(app_path('KnowledgeBasePanel'))
->extending(\Guava\FilamentKnowledgeBase\Pages\Documentation::class)
->get());
}

public function getFrontMatter(): array
Expand Down Expand Up @@ -163,6 +161,16 @@ public function getParent(): ?string
return $this->parent;
}

public function getParentId(): ?string
{
$parts = str($this->getId())->explode('.', 3);
if (count($parts) >= 3) {
return $parts[0] . '.' . $parts[1];
}

return null;
}

public function getGroup(): ?string
{
return $this->group;
Expand All @@ -186,20 +194,40 @@ public function isRegistered(): bool
public function getBreadcrumbs(): array
{
return collect([
__('filament-knowledge-base::translations.knowledge-base'),
KnowledgeBase::panel()->getUrl() => __('filament-knowledge-base::translations.knowledge-base'),
])
->when(
$group = $this->getGroup(),
fn (Collection $collection) => $collection->push($group)
fn (Collection $collection) => $collection->put($this->getGroupUrl() . '#', $group)
)
->when(
$parent = $this->getParent(),
fn (Collection $collection) => $collection->push($parent)
fn (Collection $collection) => $collection->put(
ViewDocumentation::getUrl([
'record' => KnowledgeBase::documentable($this->getParentId()),
], panel: config('filament-knowledge-base.panel.id', 'knowledge-base')),
$parent,
)
)
->put(ViewDocumentation::getUrl([
'record' => $this,
], panel: config('filament-knowledge-base.panel.id', 'knowledge-base')), $this->getTitle())
->toArray()
;
}

public function getGroupUrl()
{
$group = collect(KnowledgeBase::panel()->getNavigation())
->first(function ($item) {
return $item instanceof NavigationGroup && $item->getLabel() === $this->getGroup();
})
;

if ($group) {
return Arr::first($group->getItems())->getUrl();
}

return null;
}
}

0 comments on commit 71e0827

Please sign in to comment.