mirror of
https://github.com/facebook/docusaurus.git
synced 2025-12-25 17:22:50 +00:00
feat(theme-search-algolia): add support for DocSearch v4.3.2 and new Suggested Questions (#11541)
Co-authored-by: sebastien <lorber.sebastien@gmail.com>
This commit is contained in:
parent
bbec801e3f
commit
89633b4d33
|
|
@ -33,7 +33,7 @@
|
|||
"copy:watch": "node ../../admin/scripts/copyUntypedFiles.js --watch"
|
||||
},
|
||||
"dependencies": {
|
||||
"@docsearch/react": "^3.9.0 || ^4.1.0",
|
||||
"@docsearch/react": "^3.9.0 || ^4.3.2",
|
||||
"@docusaurus/core": "3.9.2",
|
||||
"@docusaurus/logger": "3.9.2",
|
||||
"@docusaurus/plugin-content-docs": "3.9.2",
|
||||
|
|
|
|||
|
|
@ -436,5 +436,95 @@ describe('validateThemeConfig', () => {
|
|||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Ask AI suggestedQuestions', () => {
|
||||
it('accepts suggestedQuestions as true', () => {
|
||||
const algolia = {
|
||||
appId: 'BH4D9OD16A',
|
||||
indexName: 'index',
|
||||
apiKey: 'apiKey',
|
||||
askAi: {
|
||||
assistantId: 'my-assistant-id',
|
||||
suggestedQuestions: true,
|
||||
},
|
||||
} satisfies AlgoliaInput;
|
||||
|
||||
expect(testValidateThemeConfig(algolia)).toEqual({
|
||||
algolia: {
|
||||
...DEFAULT_CONFIG,
|
||||
...algolia,
|
||||
askAi: {
|
||||
indexName: algolia.indexName,
|
||||
apiKey: algolia.apiKey,
|
||||
appId: algolia.appId,
|
||||
assistantId: 'my-assistant-id',
|
||||
suggestedQuestions: true,
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('accepts suggestedQuestions as false', () => {
|
||||
const algolia = {
|
||||
appId: 'BH4D9OD16A',
|
||||
indexName: 'index',
|
||||
apiKey: 'apiKey',
|
||||
askAi: {
|
||||
assistantId: 'my-assistant-id',
|
||||
suggestedQuestions: false,
|
||||
},
|
||||
} satisfies AlgoliaInput;
|
||||
|
||||
expect(testValidateThemeConfig(algolia)).toEqual({
|
||||
algolia: {
|
||||
...DEFAULT_CONFIG,
|
||||
...algolia,
|
||||
askAi: {
|
||||
indexName: algolia.indexName,
|
||||
apiKey: algolia.apiKey,
|
||||
appId: algolia.appId,
|
||||
assistantId: 'my-assistant-id',
|
||||
suggestedQuestions: false,
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('rejects invalid suggestedQuestions type', () => {
|
||||
const algolia: AlgoliaInput = {
|
||||
appId: 'BH4D9OD16A',
|
||||
indexName: 'index',
|
||||
apiKey: 'apiKey',
|
||||
askAi: {
|
||||
assistantId: 'my-assistant-id',
|
||||
// @ts-expect-error: expected type error
|
||||
suggestedQuestions: 'invalid-string',
|
||||
},
|
||||
};
|
||||
expect(() =>
|
||||
testValidateThemeConfig(algolia),
|
||||
).toThrowErrorMatchingInlineSnapshot(
|
||||
`""algolia.askAi.suggestedQuestions" must be a boolean"`,
|
||||
);
|
||||
});
|
||||
|
||||
it('rejects suggestedQuestions as number', () => {
|
||||
const algolia: AlgoliaInput = {
|
||||
appId: 'BH4D9OD16A',
|
||||
indexName: 'index',
|
||||
apiKey: 'apiKey',
|
||||
askAi: {
|
||||
assistantId: 'my-assistant-id',
|
||||
// @ts-expect-error: expected type error
|
||||
suggestedQuestions: 123,
|
||||
},
|
||||
};
|
||||
expect(() =>
|
||||
testValidateThemeConfig(algolia),
|
||||
).toThrowErrorMatchingInlineSnapshot(
|
||||
`""algolia.askAi.suggestedQuestions" must be a boolean"`,
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -91,7 +91,7 @@ export function useAlgoliaAskAi(props: DocSearchV4PropsLite): UseAskAiResult {
|
|||
}, []);
|
||||
|
||||
const extraAskAiProps: UseAskAiResult['extraAskAiProps'] = {
|
||||
askAi,
|
||||
askAi: askAi as any,
|
||||
canHandleAskAi,
|
||||
isAskAiActive,
|
||||
onAskAiToggle,
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ declare module '@docusaurus/theme-search-algolia' {
|
|||
import type {FacetFilters} from 'algoliasearch/lite';
|
||||
|
||||
// The config after normalization (e.g. AskAI string -> object)
|
||||
// This matches DocSearch v4.3+ AskAi configuration
|
||||
export type AskAiConfig = {
|
||||
indexName: string;
|
||||
apiKey: string;
|
||||
|
|
@ -25,6 +26,7 @@ declare module '@docusaurus/theme-search-algolia' {
|
|||
searchParameters?: {
|
||||
facetFilters?: FacetFilters;
|
||||
};
|
||||
suggestedQuestions?: boolean;
|
||||
};
|
||||
|
||||
// DocSearch props that Docusaurus exposes directly through props forwarding
|
||||
|
|
|
|||
|
|
@ -61,7 +61,7 @@ type DocSearchProps = Omit<
|
|||
|
||||
// extend DocSearchProps for v4 features
|
||||
// TODO Docusaurus v4: cleanup after we drop support for DocSearch v3
|
||||
interface DocSearchV4Props extends DocSearchProps {
|
||||
interface DocSearchV4Props extends Omit<DocSearchProps, 'askAi'> {
|
||||
indexName: string;
|
||||
askAi?: ThemeConfigAlgolia['askAi'];
|
||||
translations?: DocSearchTranslations;
|
||||
|
|
@ -199,7 +199,7 @@ function useSearchParameters({
|
|||
|
||||
function DocSearch({externalUrlRegex, ...props}: DocSearchV4Props) {
|
||||
const navigator = useNavigator({externalUrlRegex});
|
||||
const searchParameters = useSearchParameters({...props});
|
||||
const searchParameters = useSearchParameters({...props} as DocSearchProps);
|
||||
const transformItems = useTransformItems(props);
|
||||
const transformSearchClient = useTransformSearchClient();
|
||||
|
||||
|
|
@ -301,7 +301,7 @@ function DocSearch({externalUrlRegex, ...props}: DocSearchV4Props) {
|
|||
resultsFooterComponent,
|
||||
})}
|
||||
placeholder={currentPlaceholder}
|
||||
{...props}
|
||||
{...(props as any)}
|
||||
translations={props.translations?.modal ?? translations.modal}
|
||||
searchParameters={searchParameters}
|
||||
{...extraAskAiProps}
|
||||
|
|
|
|||
|
|
@ -75,6 +75,7 @@ export const Schema = Joi.object<ThemeConfig>({
|
|||
searchParameters: Joi.object({
|
||||
facetFilters: FacetFiltersSchema.optional(),
|
||||
}).optional(),
|
||||
suggestedQuestions: Joi.boolean().optional(),
|
||||
}),
|
||||
)
|
||||
.custom(
|
||||
|
|
|
|||
|
|
@ -249,6 +249,7 @@ export default {
|
|||
indexName: 'YOUR_ALGOLIA_INDEX_NAME',
|
||||
apiKey: 'YOUR_ALGOLIA_API_KEY',
|
||||
appId: 'YOUR_ALGOLIA_APP_ID',
|
||||
suggestedQuestions: true, // Optional: enable suggested questions (default: false)
|
||||
},
|
||||
// highlight-end
|
||||
|
||||
|
|
|
|||
|
|
@ -679,6 +679,7 @@ export default async function createConfigAsync() {
|
|||
// cSpell:ignore IMYF
|
||||
assistantId: 'RgIMYFUmTfrN',
|
||||
indexName: 'docusaurus-markdown',
|
||||
suggestedQuestions: true,
|
||||
},
|
||||
}
|
||||
: {}),
|
||||
|
|
|
|||
|
|
@ -249,6 +249,7 @@ export default {
|
|||
indexName: 'YOUR_ALGOLIA_INDEX_NAME',
|
||||
apiKey: 'YOUR_ALGOLIA_API_KEY',
|
||||
appId: 'YOUR_ALGOLIA_APP_ID',
|
||||
suggestedQuestions: true, // Optional: enable suggested questions (default: false)
|
||||
},
|
||||
// highlight-end
|
||||
|
||||
|
|
|
|||
24
yarn.lock
24
yarn.lock
|
|
@ -2077,19 +2077,25 @@
|
|||
resolved "https://registry.yarnpkg.com/@discoveryjs/json-ext/-/json-ext-0.5.7.tgz#1d572bfbbe14b7704e0ba0f39b74815b84870d70"
|
||||
integrity sha512-dBVuXR082gk3jsFp7Rd/JI4kytwGHecnCoTtXFb7DB6CNHp4rg5k1bhg0nWdLGLnOV71lmDzGQaLMy8iPLY0pw==
|
||||
|
||||
"@docsearch/css@4.1.0":
|
||||
version "4.1.0"
|
||||
resolved "https://registry.yarnpkg.com/@docsearch/css/-/css-4.1.0.tgz#e156e011539d73624b2354dc8be8e96ac9be9ddc"
|
||||
integrity sha512-nuNKGjHj/FQeWgE9t+i83QD/V67QiaAmGY7xS9TVCRUiCqSljOgIKlsLoQZKKVwEG8f+OWKdznzZkJxGZ7d06A==
|
||||
"@docsearch/core@4.3.1":
|
||||
version "4.3.1"
|
||||
resolved "https://registry.yarnpkg.com/@docsearch/core/-/core-4.3.1.tgz#88a97a6fe4d4025269b6dee8b9d070b76758ad82"
|
||||
integrity sha512-ktVbkePE+2h9RwqCUMbWXOoebFyDOxHqImAqfs+lC8yOU+XwEW4jgvHGJK079deTeHtdhUNj0PXHSnhJINvHzQ==
|
||||
|
||||
"@docsearch/react@^3.9.0 || ^4.1.0":
|
||||
version "4.1.0"
|
||||
resolved "https://registry.yarnpkg.com/@docsearch/react/-/react-4.1.0.tgz#a04f22324067f2e39dbe12f0e1247e7e0341d26d"
|
||||
integrity sha512-4GHI7TT3sJZ2Vs4Kjadv7vAkMrTsJqHvzvxO3JA7UT8iPRKaDottG5o5uNshPWhVVaBYPC35Ukf8bfCotGpjSg==
|
||||
"@docsearch/css@4.3.2":
|
||||
version "4.3.2"
|
||||
resolved "https://registry.yarnpkg.com/@docsearch/css/-/css-4.3.2.tgz#d47d25336c9516b419245fa74e8dd5ae84a17492"
|
||||
integrity sha512-K3Yhay9MgkBjJJ0WEL5MxnACModX9xuNt3UlQQkDEDZJZ0+aeWKtOkxHNndMRkMBnHdYvQjxkm6mdlneOtU1IQ==
|
||||
|
||||
"@docsearch/react@^3.9.0 || ^4.3.2":
|
||||
version "4.3.2"
|
||||
resolved "https://registry.yarnpkg.com/@docsearch/react/-/react-4.3.2.tgz#450b8341cb5cca03737a00075d4dfd3a904a3e3e"
|
||||
integrity sha512-74SFD6WluwvgsOPqifYOviEEVwDxslxfhakTlra+JviaNcs7KK/rjsPj89kVEoQc9FUxRkAofaJnHIR7pb4TSQ==
|
||||
dependencies:
|
||||
"@ai-sdk/react" "^2.0.30"
|
||||
"@algolia/autocomplete-core" "1.19.2"
|
||||
"@docsearch/css" "4.1.0"
|
||||
"@docsearch/core" "4.3.1"
|
||||
"@docsearch/css" "4.3.2"
|
||||
ai "^5.0.30"
|
||||
algoliasearch "^5.28.0"
|
||||
marked "^16.3.0"
|
||||
|
|
|
|||
Loading…
Reference in New Issue