Skip to content

Commit 09555c1

Browse files
committed
fix: allow backwards compatible indexName in theme config
1 parent ebaa912 commit 09555c1

6 files changed

Lines changed: 131 additions & 44 deletions

File tree

‎packages/docusaurus-theme-search-algolia/src/__tests__/validateThemeConfig.test.ts‎

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,84 @@ describe('validateThemeConfig', () => {
8383
);
8484
});
8585

86-
it('missing indices config', () => {
86+
it('missing indexName and indices config', () => {
8787
// @ts-expect-error: expected type error!
8888
const algolia: AlgoliaInput = {
8989
apiKey: 'apiKey',
9090
appId: 'BH4D9OD16A',
9191
};
92-
expect(() =>
93-
testValidateThemeConfig(algolia),
94-
).toThrowErrorMatchingInlineSnapshot(
95-
`[ValidationError: "algolia.indices" is required]`,
92+
expect(() => testValidateThemeConfig(algolia)).toThrow(
93+
'"themeConfig.algolia" must contain at least one of [indexName, indices]',
9694
);
9795
});
9896

97+
describe('indexName config', () => {
98+
it('normalizes indexName to indices', () => {
99+
const algolia: AlgoliaInput = {
100+
appId: 'BH4D9OD16A',
101+
apiKey: 'apiKey',
102+
indexName: 'index',
103+
};
104+
expect(testValidateThemeConfig(algolia)).toEqual({
105+
algolia: {
106+
...DEFAULT_CONFIG,
107+
appId: 'BH4D9OD16A',
108+
apiKey: 'apiKey',
109+
indices: [{name: 'index'}],
110+
},
111+
});
112+
});
113+
114+
it('moves top-level searchParameters into the index', () => {
115+
const algolia: AlgoliaInput = {
116+
appId: 'BH4D9OD16A',
117+
apiKey: 'apiKey',
118+
indexName: 'index',
119+
searchParameters: {facetFilters: ['language:en'], hitsPerPage: 5},
120+
};
121+
expect(testValidateThemeConfig(algolia)).toEqual({
122+
algolia: {
123+
...DEFAULT_CONFIG,
124+
appId: 'BH4D9OD16A',
125+
apiKey: 'apiKey',
126+
indices: [
127+
{
128+
name: 'index',
129+
searchParameters: {
130+
facetFilters: ['language:en'],
131+
hitsPerPage: 5,
132+
},
133+
},
134+
],
135+
},
136+
});
137+
});
138+
139+
it('rejects indexName together with indices', () => {
140+
const algolia = {
141+
appId: 'BH4D9OD16A',
142+
apiKey: 'apiKey',
143+
indexName: 'index',
144+
indices: ['index'],
145+
} as unknown as AlgoliaInput;
146+
expect(() => testValidateThemeConfig(algolia)).toThrow(
147+
'"themeConfig.algolia" contains a conflict between exclusive peers [indexName, indices]',
148+
);
149+
});
150+
151+
it('rejects top-level searchParameters together with indices', () => {
152+
const algolia = {
153+
appId: 'BH4D9OD16A',
154+
apiKey: 'apiKey',
155+
indices: ['index'],
156+
searchParameters: {facetFilters: ['language:en']},
157+
} as unknown as AlgoliaInput;
158+
expect(() => testValidateThemeConfig(algolia)).toThrow(
159+
'"themeConfig.algolia" contains a conflict between optional exclusive peers [searchParameters, indices]',
160+
);
161+
});
162+
});
163+
99164
it('missing apiKey config', () => {
100165
// @ts-expect-error: expected type error!
101166
const algolia: AlgoliaInput = {

‎packages/docusaurus-theme-search-algolia/src/theme-search-algolia.d.ts‎

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ declare module '@docusaurus/theme-search-algolia' {
1111
import type {
1212
DocSearchProps,
1313
DocSearchAskAi,
14+
DocSearchIndex,
1415
AskAiSearchParameters,
1516
} from '@docsearch/react';
1617

@@ -57,23 +58,37 @@ declare module '@docusaurus/theme-search-algolia' {
5758
algolia: ThemeConfigAlgolia;
5859
};
5960

61+
type UserAlgoliaConfig = Overwrite<
62+
DeepPartial<Omit<ThemeConfigAlgolia, 'indices'>>,
63+
{
64+
// Required fields:
65+
appId: ThemeConfigAlgolia['appId'];
66+
apiKey: ThemeConfigAlgolia['apiKey'];
67+
// askAi also accepts a shorter string form
68+
askAi?:
69+
| string
70+
| Optional<
71+
AskAiConfig,
72+
'indices' | 'appId' | 'apiKey' | 'searchParameters'
73+
>;
74+
}
75+
> &
76+
// Single index shorthand, or multiple indices (exclusive)
77+
(
78+
| {
79+
indexName: string;
80+
searchParameters?: DocSearchIndex['searchParameters'];
81+
indices?: never;
82+
}
83+
| {
84+
indices: ThemeConfigAlgolia['indices'];
85+
indexName?: never;
86+
searchParameters?: never;
87+
}
88+
);
89+
6090
export type UserThemeConfig = {
61-
algolia?: Overwrite<
62-
DeepPartial<ThemeConfigAlgolia>,
63-
{
64-
// Required fields:
65-
appId: ThemeConfigAlgolia['appId'];
66-
apiKey: ThemeConfigAlgolia['apiKey'];
67-
indices: ThemeConfigAlgolia['indices'];
68-
// askAi also accepts a shorter string form
69-
askAi?:
70-
| string
71-
| Optional<
72-
AskAiConfig,
73-
'indices' | 'appId' | 'apiKey' | 'searchParameters'
74-
>;
75-
}
76-
>;
91+
algolia?: UserAlgoliaConfig;
7792
};
7893
}
7994

‎packages/docusaurus-theme-search-algolia/src/theme/SearchPage/index.tsx‎

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
/* eslint-disable jsx-a11y/no-autofocus */
99

1010
import {type ReactNode, useEffect, useReducer, useRef, useState} from 'react';
11-
import clsx from 'clsx';
11+
import {clsx} from 'clsx';
1212

1313
import algoliaSearchHelper from 'algoliasearch-helper';
1414
import {liteClient} from 'algoliasearch/lite';
@@ -233,10 +233,9 @@ function getSearchPageTitle(searchQuery: string | undefined): string {
233233
});
234234
}
235235

236-
function getIndexName(indices: Array<string | DocSearchIndex>) {
237-
const candidate = indices[0];
238-
239-
return typeof candidate === 'string' ? candidate : candidate?.name;
236+
function getIndexName(indices: Array<string | DocSearchIndex>): string {
237+
const first = indices[0]!;
238+
return typeof first === 'string' ? first : first.name;
240239
}
241240

242241
function SearchPageContent(): ReactNode {
@@ -305,18 +304,9 @@ function SearchPageContent(): ReactNode {
305304
? ['language', 'docusaurus_tag']
306305
: [];
307306

308-
// The algoliasearch-helper only allows for a single index, here we just treat
309-
// the FIRST index in the `indices` list as the "primary" index
307+
// The algoliasearch-helper only allows a single index: use the first one
310308
const indexName = getIndexName(indices);
311309

312-
if (!indexName) {
313-
throw new Error(
314-
`Could not find a useable index in "algolia.indices" for the SearchPage.
315-
Ensure you've added the correct index names in order for search to work.
316-
`,
317-
);
318-
}
319-
320310
const algoliaClient = liteClient(appId, apiKey);
321311
const algoliaHelper = algoliaSearchHelper(algoliaClient, indexName, {
322312
// eslint-disable-next-line @typescript-eslint/ban-ts-comment

‎packages/docusaurus-theme-search-algolia/src/validateThemeConfig.ts‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
import {Joi} from '@docusaurus/utils-validation';
9+
import type {DocSearchIndex} from '@docsearch/react';
910
import type {ThemeConfigValidationContext} from '@docusaurus/types';
1011
import type {
1112
ThemeConfig,
@@ -23,8 +24,17 @@ const FacetFiltersSchema = Joi.array().items(
2324
Joi.alternatives().try(Joi.string(), Joi.array().items(Joi.string())),
2425
);
2526

27+
const SearchParametersSchema = Joi.object({
28+
facetFilters: FacetFiltersSchema.optional(),
29+
}).unknown(true);
30+
2631
const Schema = Joi.object<ThemeConfig>({
27-
algolia: Joi.object<ThemeConfigAlgolia>({
32+
algolia: Joi.object<
33+
ThemeConfigAlgolia & {
34+
indexName?: string;
35+
searchParameters?: DocSearchIndex['searchParameters'];
36+
}
37+
>({
2838
// Docusaurus attributes
2939
contextualSearch: Joi.boolean().default(DEFAULT_CONFIG.contextualSearch),
3040
externalUrlRegex: Joi.string().optional(),
@@ -34,20 +44,20 @@ const Schema = Joi.object<ThemeConfig>({
3444
'"algolia.appId" is required. If you haven\'t migrated to the new DocSearch infra, please refer to the blog post for instructions: https://docusaurus.io/blog/2021/11/21/algolia-docsearch-migration',
3545
}),
3646
apiKey: Joi.string().required(),
47+
// Single-index shorthand, normalized to `indices` below
48+
indexName: Joi.string(),
49+
searchParameters: SearchParametersSchema,
3750
indices: Joi.array()
3851
.items(
3952
Joi.alternatives().try(
4053
Joi.string(),
4154
Joi.object({
4255
name: Joi.string().required(),
43-
searchParameters: Joi.object({
44-
facetFilters: FacetFiltersSchema.optional(),
45-
}).unknown(true),
56+
searchParameters: SearchParametersSchema,
4657
}),
4758
),
4859
)
49-
.min(1)
50-
.required(),
60+
.min(1),
5161
searchPagePath: Joi.alternatives()
5262
.try(Joi.boolean().invalid(true), Joi.string())
5363
.allow(null)
@@ -152,6 +162,13 @@ const Schema = Joi.object<ThemeConfig>({
152162
'askAi must be either a string (agentId) or an object with apiKey, appId, and agentId',
153163
}),
154164
})
165+
.xor('indexName', 'indices')
166+
.oxor('searchParameters', 'indices')
167+
.custom(({indexName, searchParameters, ...rest}) =>
168+
indexName
169+
? {...rest, indices: [{name: indexName, searchParameters}]}
170+
: rest,
171+
)
155172
.label('themeConfig.algolia')
156173
.required()
157174
.unknown(),

‎website/docusaurus.config-blog-only.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export default {
4545
algolia: {
4646
appId: 'X1Z85QJPUV',
4747
apiKey: 'bf7211c161e8205da2f933a02534105a',
48-
indices: ['docusaurus-2'],
48+
indexName: 'docusaurus-2',
4949
contextualSearch: true,
5050
},
5151
navbar: {

‎website/docusaurus.config.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ export default async function createConfigAsync() {
671671
algolia: {
672672
appId: 'X1Z85QJPUV',
673673
apiKey: 'bf7211c161e8205da2f933a02534105a',
674-
indices: ['docusaurus-2'],
674+
indexName: 'docusaurus-2',
675675
// TODO Enable once there is an Agent Studio agent to use
676676
// askAi: {
677677
// cSpell:ignore IMYF

0 commit comments

Comments
 (0)