diff --git a/packages/docusaurus-theme-search-algolia/src/__tests__/validateThemeConfig.test.js b/packages/docusaurus-theme-search-algolia/src/__tests__/validateThemeConfig.test.js index cb842e823352..f9b53c5f2eb2 100644 --- a/packages/docusaurus-theme-search-algolia/src/__tests__/validateThemeConfig.test.js +++ b/packages/docusaurus-theme-search-algolia/src/__tests__/validateThemeConfig.test.js @@ -88,4 +88,50 @@ describe('validateThemeConfig', () => { testValidateThemeConfig({algolia}), ).toThrowErrorMatchingInlineSnapshot(`"\\"algolia.apiKey\\" is required"`); }); + + test('contextualSearch config', () => { + const algolia = { + indexName: 'index', + apiKey: 'apiKey', + contextualSearch: true, + }; + expect(testValidateThemeConfig({algolia})).toEqual({ + algolia: { + ...DEFAULT_CONFIG, + ...algolia, + }, + }); + }); + + test('searchParameters.facetFilters search config', () => { + const algolia = { + indexName: 'index', + apiKey: 'apiKey', + searchParameters: { + facetFilters: ['version:1.0'], + }, + }; + expect(testValidateThemeConfig({algolia})).toEqual({ + algolia: { + ...DEFAULT_CONFIG, + ...algolia, + }, + }); + }); + + test('contextualSearch + searchParameters.facetFilters config', () => { + const algolia = { + indexName: 'index', + apiKey: 'apiKey', + contextualSearch: true, + searchParameters: { + facetFilters: ['version:1.0'], + }, + }; + expect(() => + testValidateThemeConfig({algolia}), + ).toThrowErrorMatchingInlineSnapshot( + `"If you are using algolia.contextualSearch: true, you should not provide algolia.searchParameters.facetFilters, as it is computed for you dynamically"`, + ); + }); }); diff --git a/packages/docusaurus-theme-search-algolia/src/validateThemeConfig.js b/packages/docusaurus-theme-search-algolia/src/validateThemeConfig.js index f9fae1356684..df83366227e8 100644 --- a/packages/docusaurus-theme-search-algolia/src/validateThemeConfig.js +++ b/packages/docusaurus-theme-search-algolia/src/validateThemeConfig.js @@ -8,22 +8,28 @@ const Joi = require('@hapi/joi'); const DEFAULT_CONFIG = { + contextualSearch: false, // future: maybe we want to enable this by default + // By default, all Docusaurus sites are using the same AppId // This has been designed on purpose with Algolia. appId: 'BH4D9OD16A', + + searchParameters: {}, }; exports.DEFAULT_CONFIG = DEFAULT_CONFIG; const Schema = Joi.object({ algolia: Joi.object({ // Docusaurus attributes - contextualSearch: Joi.boolean().default(false), + contextualSearch: Joi.boolean().default(DEFAULT_CONFIG.contextualSearch), // Algolia attributes appId: Joi.string().default(DEFAULT_CONFIG.appId), apiKey: Joi.string().required(), indexName: Joi.string().required(), - searchParameters: Joi.object().default({}).unknown(), + searchParameters: Joi.object() + .default(DEFAULT_CONFIG.searchParameters) + .unknown(), }) .label('themeConfig.algolia') .required()