Skip to content
Open
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
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,56 @@ KnowledgeBasePanel::configureUsing(
);
```

# Search Customization

The KnowledgeBase panel comes with powerful global search capabilities that can be easily customized to fit your specific needs. This document explains the available customization options for search attributes, result titles, and result details.

## Customizing Searchable Attributes

You can specify which attributes of your documentation model should be searchable:

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

KnowledgeBasePanel::make()
->globallySearchableAttributes(['title', 'content', 'group'])
```

By default, the panel searches only in `title` and `content` fields.

## Customizing Search Result Titles

You can customize how search result titles are displayed using a callback function:

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

KnowledgeBasePanel::make()
->globalSearchResultTitle(function ($record) {
return str($record->slug)
->replace('/', ' -> ')
->replace('-', ' ')
->title();
})
```

This example transforms slugs like "getting-started/installation" into "Getting Started -> Installation".

## Customizing Search Result Details

Control what additional information appears below each search result:

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

KnowledgeBasePanel::make()
->globalSearchResultDetails(function ($record) {
return [
'Summary' => Str::limit(strip_tags($record->content), 150),
];
})
```

#### Change brand name

For example to change the default brand name/title (displayed in the top left) of the panel, you can do:
Expand Down
42 changes: 39 additions & 3 deletions src/Filament/Panels/KnowledgeBasePanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ class KnowledgeBasePanel extends Panel
use HasArticleClass;

protected bool $guestAccess = false;

protected static bool $syntaxHighlighting = false;

protected bool $disableTableOfContents = false;

protected TableOfContentsPosition $tableOfContentsPosition = TableOfContentsPosition::End;
protected array $globallySearchableAttributes = ['title', 'content'];
protected $globalSearchResultTitleCallback = null;
protected $globalSearchResultDetailsCallback = null;

public function __construct()
{
Expand All @@ -62,6 +62,42 @@ public function __construct()
);
}

public function globallySearchableAttributes(array $attributes): static
{
$this->globallySearchableAttributes = $attributes;

return $this;
}

public function getGloballySearchableAttributes(): array
{
return $this->evaluate($this->globallySearchableAttributes);
}

public function globalSearchResultTitle(callable $callback): static
{
$this->globalSearchResultTitleCallback = $callback;

return $this;
}

public function getGlobalSearchResultTitleCallback(): ?callable
{
return $this->globalSearchResultTitleCallback;
}

public function globalSearchResultDetails(callable $callback): static
{
$this->globalSearchResultDetailsCallback = $callback;

return $this;
}

public function getGlobalSearchResultDetailsCallback(): ?callable
{
return $this->globalSearchResultDetailsCallback;
}

public function guestAccess(bool $condition = true): static
{
$this->guestAccess = $condition;
Expand Down
27 changes: 26 additions & 1 deletion src/Filament/Resources/DocumentationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public static function getModel(): string

public static function getGloballySearchableAttributes(): array
{
$panel = KnowledgeBase::panel();

return $panel->getGloballySearchableAttributes();

return ['title', 'content'];
}

Expand All @@ -43,9 +47,30 @@ public static function getGlobalSearchResultUrl(Model $record): ?string

public static function getGlobalSearchResultTitle(Model $record): string | Htmlable
{
$panel = KnowledgeBase::panel();

$callback = $panel->getGlobalSearchResultTitleCallback();

if ($callback) {
return $callback($record);
}

return str($record->slug)
->replace('/', ' -> ')
;
;
}

public static function getGlobalSearchResultDetails(Model $record): array
{
$panel = KnowledgeBase::panel();

$callback = $panel->getGlobalSearchResultDetailsCallback();

if ($callback) {
return $callback($record);
}

return [];
}

public static function resolveRecordRouteBinding(int | string $key): ?Model
Expand Down