Skip to content

Commit 2d4134b

Browse files
committed
feat(theme-search-algolia): add support for v4.3.1 of DocSearch and new Suggested Questions
1 parent 6a38ccd commit 2d4134b

File tree

9 files changed

+115
-14
lines changed

9 files changed

+115
-14
lines changed

packages/docusaurus-theme-search-algolia/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"copy:watch": "node ../../admin/scripts/copyUntypedFiles.js --watch"
3434
},
3535
"dependencies": {
36-
"@docsearch/react": "^3.9.0 || ^4.1.0",
36+
"@docsearch/react": "^3.9.0 || ^4.3.1",
3737
"@docusaurus/core": "3.9.2",
3838
"@docusaurus/logger": "3.9.2",
3939
"@docusaurus/plugin-content-docs": "3.9.2",

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

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,5 +436,95 @@ describe('validateThemeConfig', () => {
436436
});
437437
});
438438
});
439+
440+
describe('Ask AI suggestedQuestions', () => {
441+
it('accepts suggestedQuestions as true', () => {
442+
const algolia = {
443+
appId: 'BH4D9OD16A',
444+
indexName: 'index',
445+
apiKey: 'apiKey',
446+
askAi: {
447+
assistantId: 'my-assistant-id',
448+
suggestedQuestions: true,
449+
},
450+
} satisfies AlgoliaInput;
451+
452+
expect(testValidateThemeConfig(algolia)).toEqual({
453+
algolia: {
454+
...DEFAULT_CONFIG,
455+
...algolia,
456+
askAi: {
457+
indexName: algolia.indexName,
458+
apiKey: algolia.apiKey,
459+
appId: algolia.appId,
460+
assistantId: 'my-assistant-id',
461+
suggestedQuestions: true,
462+
},
463+
},
464+
});
465+
});
466+
467+
it('accepts suggestedQuestions as false', () => {
468+
const algolia = {
469+
appId: 'BH4D9OD16A',
470+
indexName: 'index',
471+
apiKey: 'apiKey',
472+
askAi: {
473+
assistantId: 'my-assistant-id',
474+
suggestedQuestions: false,
475+
},
476+
} satisfies AlgoliaInput;
477+
478+
expect(testValidateThemeConfig(algolia)).toEqual({
479+
algolia: {
480+
...DEFAULT_CONFIG,
481+
...algolia,
482+
askAi: {
483+
indexName: algolia.indexName,
484+
apiKey: algolia.apiKey,
485+
appId: algolia.appId,
486+
assistantId: 'my-assistant-id',
487+
suggestedQuestions: false,
488+
},
489+
},
490+
});
491+
});
492+
493+
it('rejects invalid suggestedQuestions type', () => {
494+
const algolia: AlgoliaInput = {
495+
appId: 'BH4D9OD16A',
496+
indexName: 'index',
497+
apiKey: 'apiKey',
498+
askAi: {
499+
assistantId: 'my-assistant-id',
500+
// @ts-expect-error: expected type error
501+
suggestedQuestions: 'invalid-string',
502+
},
503+
};
504+
expect(() =>
505+
testValidateThemeConfig(algolia),
506+
).toThrowErrorMatchingInlineSnapshot(
507+
`""algolia.askAi.suggestedQuestions" must be a boolean"`,
508+
);
509+
});
510+
511+
it('rejects suggestedQuestions as number', () => {
512+
const algolia: AlgoliaInput = {
513+
appId: 'BH4D9OD16A',
514+
indexName: 'index',
515+
apiKey: 'apiKey',
516+
askAi: {
517+
assistantId: 'my-assistant-id',
518+
// @ts-expect-error: expected type error
519+
suggestedQuestions: 123,
520+
},
521+
};
522+
expect(() =>
523+
testValidateThemeConfig(algolia),
524+
).toThrowErrorMatchingInlineSnapshot(
525+
`""algolia.askAi.suggestedQuestions" must be a boolean"`,
526+
);
527+
});
528+
});
439529
});
440530
});

packages/docusaurus-theme-search-algolia/src/client/useAlgoliaAskAi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export function useAlgoliaAskAi(props: DocSearchV4PropsLite): UseAskAiResult {
9191
}, []);
9292

9393
const extraAskAiProps: UseAskAiResult['extraAskAiProps'] = {
94-
askAi,
94+
askAi: askAi as any,
9595
canHandleAskAi,
9696
isAskAiActive,
9797
onAskAiToggle,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ declare module '@docusaurus/theme-search-algolia' {
1717
import type {FacetFilters} from 'algoliasearch/lite';
1818

1919
// The config after normalization (e.g. AskAI string -> object)
20+
// This matches DocSearch v4.3+ AskAi configuration
2021
export type AskAiConfig = {
2122
indexName: string;
2223
apiKey: string;
@@ -25,6 +26,7 @@ declare module '@docusaurus/theme-search-algolia' {
2526
searchParameters?: {
2627
facetFilters?: FacetFilters;
2728
};
29+
suggestedQuestions?: boolean;
2830
};
2931

3032
// DocSearch props that Docusaurus exposes directly through props forwarding

packages/docusaurus-theme-search-algolia/src/theme/SearchBar/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ type DocSearchProps = Omit<
6161

6262
// extend DocSearchProps for v4 features
6363
// TODO Docusaurus v4: cleanup after we drop support for DocSearch v3
64-
interface DocSearchV4Props extends DocSearchProps {
64+
interface DocSearchV4Props extends Omit<DocSearchProps, 'askAi'> {
6565
indexName: string;
6666
askAi?: ThemeConfigAlgolia['askAi'];
6767
translations?: DocSearchTranslations;
@@ -199,7 +199,7 @@ function useSearchParameters({
199199

200200
function DocSearch({externalUrlRegex, ...props}: DocSearchV4Props) {
201201
const navigator = useNavigator({externalUrlRegex});
202-
const searchParameters = useSearchParameters({...props});
202+
const searchParameters = useSearchParameters({...props} as DocSearchProps);
203203
const transformItems = useTransformItems(props);
204204
const transformSearchClient = useTransformSearchClient();
205205

@@ -301,7 +301,7 @@ function DocSearch({externalUrlRegex, ...props}: DocSearchV4Props) {
301301
resultsFooterComponent,
302302
})}
303303
placeholder={currentPlaceholder}
304-
{...props}
304+
{...(props as any)}
305305
translations={props.translations?.modal ?? translations.modal}
306306
searchParameters={searchParameters}
307307
{...extraAskAiProps}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ export const Schema = Joi.object<ThemeConfig>({
7575
searchParameters: Joi.object({
7676
facetFilters: FacetFiltersSchema.optional(),
7777
}).optional(),
78+
suggestedQuestions: Joi.boolean().optional(),
7879
}),
7980
)
8081
.custom(

website/docs/search.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ export default {
249249
indexName: 'YOUR_ALGOLIA_INDEX_NAME',
250250
apiKey: 'YOUR_ALGOLIA_API_KEY',
251251
appId: 'YOUR_ALGOLIA_APP_ID',
252+
suggestedQuestions: true, // Optional: enable suggested questions (default: false)
252253
},
253254
// highlight-end
254255

website/versioned_docs/version-3.9.2/search.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ export default {
249249
indexName: 'YOUR_ALGOLIA_INDEX_NAME',
250250
apiKey: 'YOUR_ALGOLIA_API_KEY',
251251
appId: 'YOUR_ALGOLIA_APP_ID',
252+
suggestedQuestions: true, // Optional: enable suggested questions (default: false)
252253
},
253254
// highlight-end
254255

yarn.lock

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2077,19 +2077,25 @@
20772077
resolved "https://registry.yarnpkg.com/@discoveryjs/json-ext/-/json-ext-0.5.7.tgz#1d572bfbbe14b7704e0ba0f39b74815b84870d70"
20782078
integrity sha512-dBVuXR082gk3jsFp7Rd/JI4kytwGHecnCoTtXFb7DB6CNHp4rg5k1bhg0nWdLGLnOV71lmDzGQaLMy8iPLY0pw==
20792079

2080-
"@docsearch/css@4.1.0":
2081-
version "4.1.0"
2082-
resolved "https://registry.yarnpkg.com/@docsearch/css/-/css-4.1.0.tgz#e156e011539d73624b2354dc8be8e96ac9be9ddc"
2083-
integrity sha512-nuNKGjHj/FQeWgE9t+i83QD/V67QiaAmGY7xS9TVCRUiCqSljOgIKlsLoQZKKVwEG8f+OWKdznzZkJxGZ7d06A==
2080+
"@docsearch/core@4.3.1":
2081+
version "4.3.1"
2082+
resolved "https://registry.yarnpkg.com/@docsearch/core/-/core-4.3.1.tgz#88a97a6fe4d4025269b6dee8b9d070b76758ad82"
2083+
integrity sha512-ktVbkePE+2h9RwqCUMbWXOoebFyDOxHqImAqfs+lC8yOU+XwEW4jgvHGJK079deTeHtdhUNj0PXHSnhJINvHzQ==
20842084

2085-
"@docsearch/react@^3.9.0 || ^4.1.0":
2086-
version "4.1.0"
2087-
resolved "https://registry.yarnpkg.com/@docsearch/react/-/react-4.1.0.tgz#a04f22324067f2e39dbe12f0e1247e7e0341d26d"
2088-
integrity sha512-4GHI7TT3sJZ2Vs4Kjadv7vAkMrTsJqHvzvxO3JA7UT8iPRKaDottG5o5uNshPWhVVaBYPC35Ukf8bfCotGpjSg==
2085+
"@docsearch/css@4.3.1":
2086+
version "4.3.1"
2087+
resolved "https://registry.yarnpkg.com/@docsearch/css/-/css-4.3.1.tgz#bcb1115f3b65d41b61d30df0c9465b735c1fb28d"
2088+
integrity sha512-Jnct7LKOi/+Oxbmq215YPYASkMdZqtyyDCkma8Cj4sCcbBuybL6fvyBaX7uJoM6kVF7aIpBA38RhHAyN5ByCHg==
2089+
2090+
"@docsearch/react@^3.9.0 || ^4.3.1":
2091+
version "4.3.1"
2092+
resolved "https://registry.yarnpkg.com/@docsearch/react/-/react-4.3.1.tgz#2d87a9e17fce13be00106203545f9a801037c49c"
2093+
integrity sha512-vbKwgDuHi/YA0CiicXhJm4DSfOIOccl3FJlkdVmeZ5d61wNbPdAnAy71i7FF0JzO6noCQScidBBguOCaNfauEw==
20892094
dependencies:
20902095
"@ai-sdk/react" "^2.0.30"
20912096
"@algolia/autocomplete-core" "1.19.2"
2092-
"@docsearch/css" "4.1.0"
2097+
"@docsearch/core" "4.3.1"
2098+
"@docsearch/css" "4.3.1"
20932099
ai "^5.0.30"
20942100
algoliasearch "^5.28.0"
20952101
marked "^16.3.0"

0 commit comments

Comments
 (0)