Download the PHP package johannschopplich/kirby-algolia-docsearch without Composer
On this page you can find all versions of the php package johannschopplich/kirby-algolia-docsearch. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download johannschopplich/kirby-algolia-docsearch
More information about johannschopplich/kirby-algolia-docsearch
Files in johannschopplich/kirby-algolia-docsearch
Package kirby-algolia-docsearch
Short Description Index your Kirby site for the Algolia DocSearch modal
License MIT
Homepage https://github.com/johannschopplich/kirby-algolia-docsearch#readme
Informations about the package kirby-algolia-docsearch
When to Use
| I want to… | Use |
|---|---|
| Index the whole site | kirby algolia-docsearch:index |
| Index the site from the Panel | Janitor field with algolia-docsearch:index |
| Update the index when a page changes | hooks option |
| Index unlisted pages, too | filter option |
| Index a field instead of the rendered HTML | content closure |
| Add data to each search hit | attributes option |
| Tune Algolia's index settings | indexSettings option |
| Index from my own code | algolia()->index() |
Features
🔦 Indexing Without the Crawler
The plugin renders every listed page of the configured templates, extracts the text of one HTML tag, and writes one DocSearch record per page. Run the Kirby CLI command to replace the index with the current state of the site:
On the command line, set Kirby's url option first, or the records get root-relative URLs such as /docs/installation.
With Janitor installed, the same command runs from a button in the Panel:
🔁 Page Hooks
With the hooks option enabled, a change to a page updates its record. Index the site once, and the hooks take over from there:
- Saving content or changing the title in a translation updates that language's index; in the default language, every language's index.
- Creating a published page, changing the slug or status, moving, changing the template, and deleting update every language's index. When a page's URL or draft state changes, its descendants follow.
A page that shows content from elsewhere – a menu, a list of child pages, a site field, a file – keeps its old record until the next full index.
A hook that fails shows an Algolia error in the Panel. The page change is saved by then, and the next full index corrects its record.
The hooks stay off while Kirby's debug option is on.
🌐 One Index per Language
On a multi-language site, each language gets its own index: the index option plus the language code, e.g. example-en and example-de. Labels take one string per language:
🧩 Content From HTML or a Closure
By default, a record's content is the text inside the rendered page's <body>. Point the content option to another tag, such as main, or return the text from a closure. This one indexes a blocks field instead of the rendered page:
The closure receives the language code of the index it writes to. For different content per template, pass an array of closures keyed by template name, with default for the rest.
🛠️ Indexing From Your Own Code
In a route, a hook, or a script of your own, the algolia() helper returns the plugin's indexer:
indexPage() removes the record of a page that no longer belongs in the index, such as a draft or one the filter rejects. On a single-language site, call indexPage($page) without a language code.
Requirements
- Kirby 5 or 6
Installation
Composer (Recommended)
Manual Installation
Download a release and extract it to /site/plugins/kirby-algolia-docsearch.
DocSearch Setup
The plugin only fills the index. The search button and modal come from the DocSearch library, which you add to your frontend:
The @docsearch/js/docsearch entry is keyword search only. Use your Algolia Search API key here, never the key from the plugin options. On a multi-language site, pick the index of the current language, e.g. from the lang attribute of your <html> element:
See the DocSearch documentation for the CDN build and styling.
Options
All options are namespaced under johannschopplich.algolia-docsearch:
| Option | Default | Description |
|---|---|---|
appId |
– | Algolia application ID. Required. |
apiKey |
– | Algolia API key with write access, such as the Admin API key. It stays on the server. Required. |
index |
– | Index name. Multi-language sites get one index per language, suffixed with the language code. Required. |
templates |
[] |
Templates to index. Pages of other templates are left out, so nothing is indexed until you set this. |
filter |
listed pages | Closure fn (Page $page): bool that decides which pages of those templates get indexed. Drafts never are. |
exclude.pages |
[] |
Page IDs to leave out, as case-insensitive regular expressions matched against the whole ID, e.g. blog/.*. |
content |
body |
HTML tag whose text becomes the record's content, a closure fn (Page $page, string\|null $languageCode): string, or an array of closures per template with a default key. |
label.default |
– | Hit label (DocSearch's hierarchy.lvl0) as a string or an array per language code. Required unless label.templates covers every template. |
label.templates |
[] |
Labels per template, in the same shapes as label.default. |
attributes |
[] |
Extra record fields as closures fn (Page $page, string\|null $languageCode), keyed by field name. |
searchableAttributes |
[] |
Attributes appended to DocSearch's searchable attributes. |
indexSettings |
[] |
Algolia index settings. Each key replaces the plugin's default for that setting, including what searchableAttributes and attributes add to it. |
hooks |
false |
Update the index when a page changes. See Page Hooks. |
Example config.php:
Limits
- One record per page. Algolia's free plan rejects records larger than 10 KB. For long pages, shorten the text a
contentclosure returns, e.g. withStr::short($text, 5000). - Custom attributes need a request. DocSearch asks for its own list of attributes with every search. To receive the fields from
attributesin your hits, list them insearchParameters.attributesToRetrieveof the index entry inindices, together with DocSearch's own:hierarchy.lvl0tohierarchy.lvl6,content,type, andurl. - Only a full index writes the index settings. A change to
attributes,searchableAttributes, orindexSettingsapplies with the next full index. It resets every setting the plugin has a default for, including one you changed in the Algolia dashboard, so set it inindexSettingsinstead.
Credits
- Wolf-Dieter Grabner for sponsoring the initial version of this plugin.
- Lukas Bestle and Nico Hoffmann for the Algolia integration of getkirby.com.
License
Johann Schopplich
All versions of kirby-algolia-docsearch with dependencies
getkirby/composer-installer Version ^1
algolia/algoliasearch-client-php Version ^3.4