Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/small-trainers-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/starlight-docsearch': minor
---

Adds support for some more of the DocSearch component’s configuration options
3 changes: 3 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ i18n:
'🌟 tailwind':
- packages/tailwind/**

'🌟 docsearch':
- packages/docsearch/**

'🌟 markdoc':
- packages/markdoc/**

Expand Down
9 changes: 9 additions & 0 deletions docs/src/content/docs/guides/site-search.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ If you have access to [Algolia’s DocSearch program](https://docsearch.algolia.

With this updated configuration, the search bar on your site will now open an Algolia modal instead of the default search modal.

#### DocSearch configuration

The Starlight DocSearch plugin also supports customizing the DocSearch component with the following additional options:

- `maxResultsPerGroup`: Limit the number of results displayed for each search group. Default is `5`.
- `disableUserPersonalization`: Prevent DocSearch from saving a user’s recent searches and favorites to local storage. Default is `false`.
- `insights`: Enable the Algolia Insights plugin and send search events to your DocSearch index. Default is `false`.
- `searchParameters`: An object customizing the [Algolia Search Parameters](https://www.algolia.com/doc/api-reference/search-api-parameters/).

#### Translating the DocSearch UI

DocSearch only provides English UI strings by default.
Expand Down
3 changes: 2 additions & 1 deletion packages/docsearch/DocSearch.astro
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ const docsearchTranslations: DocSearchTranslationProps = {
super();
window.addEventListener('DOMContentLoaded', async () => {
const { default: docsearch } = await import('@docsearch/js');
const options: Parameters<typeof docsearch>[0] = { ...config, container: 'sl-doc-search' };
type DocSearchOptions = Parameters<typeof docsearch>[0];
const options = { ...config, container: 'sl-doc-search' } as DocSearchOptions;
try {
const translations = JSON.parse(this.dataset.translations || '{}');
Object.assign(options, translations);
Expand Down
18 changes: 18 additions & 0 deletions packages/docsearch/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# <img src="https://github.com/withastro/starlight/assets/357379/494fcd83-42aa-4891-87e0-87402fa0b6f3" alt="" align="left" width="40" height="40"> @astrojs/starlight-docsearch

Algolia DocSearch plugin for the [Starlight][starlight] documentation theme for [Astro][astro].

## Documentation

See the [Starlight site search guide][docs] for how to use this plugin.

## License

MIT

Copyright (c) 2023–present [Starlight contributors][contributors]

[starlight]: https://starlight.astro.build/
[astro]: https://astro.build/
[docs]: https://starlight.astro.build/guides/site-search/#algolia-docsearch
[contributors]: https://github.com/withastro/starlight/graphs/contributors
48 changes: 40 additions & 8 deletions packages/docsearch/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,49 @@
import type { StarlightPlugin } from '@astrojs/starlight/types';
import type docsearch from '@docsearch/js';
import type { AstroUserConfig, ViteUserConfig } from 'astro';
import { z } from 'astro/zod';

/** Config options users must provide for DocSearch to work. */
const DocSearchConfigSchema = z.object({
appId: z.string(),
apiKey: z.string(),
indexName: z.string(),
});
export type DocSearchConfig = z.input<typeof DocSearchConfigSchema>;
type SearchOptions = Parameters<typeof docsearch>[0]['searchParameters'];

/** DocSearch configuration options. */
const DocSearchConfigSchema = z
.object({
// Required config without which DocSearch won’t work.
/** Your Algolia application ID. */
appId: z.string(),
/** Your Algolia Search API key. */
apiKey: z.string(),
/** Your Algolia index name. */
indexName: z.string(),
// Optional DocSearch component config (only the serializable properties can be included here)
/**
* The maximum number of results to display per search group.
* @default 5
*/
maxResultsPerGroup: z.number().optional(),
/**
* Disable saving recent searches and favorites to the local storage.
* @default false
*/
disableUserPersonalization: z.boolean().optional(),
/**
* Whether to enable the Algolia Insights plugin and send search events to your DocSearch index.
* @default false
*/
insights: z.boolean().optional(),
/**
* The Algolia Search Parameters.
* @see https://www.algolia.com/doc/api-reference/search-api-parameters/
*/
searchParameters: z.custom<SearchOptions>(),
})
.strict();

type DocSearchUserConfig = z.input<typeof DocSearchConfigSchema>;
export type DocSearchConfig = z.output<typeof DocSearchConfigSchema>;

/** Starlight DocSearch plugin. */
export default function starlightDocSearch(userConfig: DocSearchConfig): StarlightPlugin {
export default function starlightDocSearch(userConfig: DocSearchUserConfig): StarlightPlugin {
const opts = DocSearchConfigSchema.parse(userConfig);
return {
name: 'starlight-docsearch',
Expand Down
Loading