diff --git a/README.md b/README.md index b1358484..b16b0826 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,7 @@ module.exports = { | zhUserDictPath | string | | Provide the file path to your custom dict for language of zh, E.g.: `path.resolve("./src/zh-dict.txt")` | | searchContextByPaths | string[] | | Provide an list of sub-paths as separate search context, E.g.: `["docs", "community", "legacy/resources"]`. It will create multiple search indexes by these paths. | | hideSearchBarWithNoSearchContext | boolean | `false` | Whether to hide the search bar when no search context was matched. By default, if `searchContextByPaths` is set, pages which are not matched with it will be considered as with a search context of ROOT. By setting `hideSearchBarWithNoSearchContext` to false, these pages will be considered as with NO search context, and the search bar will be hidden. | +| useAllContextsWithNoSearchContext | boolean | `false` | Whether to show results from all the contexts if no context is provided. This option should not be used with `hideSearchBarWithNoSearchContext` set to `true` as this would show results when there is no search context. This will duplicate indexes and might have a performance cost depending on the index sizes. | ### I18N diff --git a/docusaurus-search-local/src/declarations.ts b/docusaurus-search-local/src/declarations.ts index 49b19526..9d18f643 100644 --- a/docusaurus-search-local/src/declarations.ts +++ b/docusaurus-search-local/src/declarations.ts @@ -22,6 +22,7 @@ declare module "*/generated.js" { export const indexDocs: boolean; export const searchContextByPaths: string[]; export const hideSearchBarWithNoSearchContext: boolean; + export const useAllContextsWithNoSearchContext: boolean; // These below are for mocking only. export const __setLanguage: (value: string[]) => void; export const __setRemoveDefaultStopWordFilter: (value: boolean) => void; diff --git a/docusaurus-search-local/src/index.ts b/docusaurus-search-local/src/index.ts index e21e15b7..ef04e55d 100644 --- a/docusaurus-search-local/src/index.ts +++ b/docusaurus-search-local/src/index.ts @@ -172,4 +172,14 @@ export interface PluginOptions { * @default false */ hideSearchBarWithNoSearchContext?: boolean; + + /** + * Whether to show results from all the contexts if no context is provided. + * + * This option should not be used with `hideSearchBarWithNoSearchContext` set to `true` as this would show results + * when there is no search context. + * + * @default false + */ + useAllContextsWithNoSearchContext?: boolean; } diff --git a/docusaurus-search-local/src/server/utils/generate.spec.ts b/docusaurus-search-local/src/server/utils/generate.spec.ts index ecd8460a..49440583 100644 --- a/docusaurus-search-local/src/server/utils/generate.spec.ts +++ b/docusaurus-search-local/src/server/utils/generate.spec.ts @@ -34,6 +34,7 @@ describe("generate", () => { "export const indexDocs = true;", "export const searchContextByPaths = null;", "export const hideSearchBarWithNoSearchContext = false;", + "export const useAllContextsWithNoSearchContext = false;", ], ], [ @@ -59,6 +60,7 @@ describe("generate", () => { "export const indexDocs = true;", "export const searchContextByPaths = null;", "export const hideSearchBarWithNoSearchContext = false;", + "export const useAllContextsWithNoSearchContext = false;", ], ], [ @@ -86,6 +88,7 @@ describe("generate", () => { "export const indexDocs = true;", "export const searchContextByPaths = null;", "export const hideSearchBarWithNoSearchContext = false;", + "export const useAllContextsWithNoSearchContext = false;", ], ], [ @@ -116,6 +119,7 @@ describe("generate", () => { "export const indexDocs = true;", "export const searchContextByPaths = null;", "export const hideSearchBarWithNoSearchContext = false;", + "export const useAllContextsWithNoSearchContext = false;", ], ], [ @@ -144,6 +148,7 @@ describe("generate", () => { "export const indexDocs = true;", "export const searchContextByPaths = null;", "export const hideSearchBarWithNoSearchContext = false;", + "export const useAllContextsWithNoSearchContext = false;", ], ], [ @@ -175,6 +180,7 @@ describe("generate", () => { "export const indexDocs = true;", "export const searchContextByPaths = null;", "export const hideSearchBarWithNoSearchContext = false;", + "export const useAllContextsWithNoSearchContext = false;", ], ], ])("generate({ language: %j }, dir) should work", (language, contents) => { diff --git a/docusaurus-search-local/src/server/utils/generate.ts b/docusaurus-search-local/src/server/utils/generate.ts index 578d0b4f..72624e66 100644 --- a/docusaurus-search-local/src/server/utils/generate.ts +++ b/docusaurus-search-local/src/server/utils/generate.ts @@ -19,6 +19,7 @@ export function generate(config: ProcessedPluginOptions, dir: string): string { indexDocs, searchContextByPaths, hideSearchBarWithNoSearchContext, + useAllContextsWithNoSearchContext, } = config; const indexHash = getIndexHash(config); const contents: string[] = [ @@ -136,7 +137,11 @@ export function generate(config: ProcessedPluginOptions, dir: string): string { !!hideSearchBarWithNoSearchContext )};` ); - + contents.push( + `export const useAllContextsWithNoSearchContext = ${JSON.stringify( + !!useAllContextsWithNoSearchContext + )};` + ); fs.writeFileSync(path.join(dir, "generated.js"), contents.join("\n")); return searchIndexFilename; diff --git a/docusaurus-search-local/src/server/utils/postBuildFactory.ts b/docusaurus-search-local/src/server/utils/postBuildFactory.ts index 7eab4514..3cf78cbf 100644 --- a/docusaurus-search-local/src/server/utils/postBuildFactory.ts +++ b/docusaurus-search-local/src/server/utils/postBuildFactory.ts @@ -31,7 +31,11 @@ export function postBuildFactory( debugInfo("building index"); const docsByDirMap = new Map(); - const { searchContextByPaths, hideSearchBarWithNoSearchContext } = config; + const { + searchContextByPaths, + hideSearchBarWithNoSearchContext, + useAllContextsWithNoSearchContext, + } = config; if (searchContextByPaths) { const { baseUrl } = buildData; const rootAllDocs: SearchDocument[][] = []; @@ -58,7 +62,9 @@ export function postBuildFactory( dirAllDocs[docIndex] = dirDocs = []; } dirDocs.push(doc); - continue; + if (!useAllContextsWithNoSearchContext) { + continue; + } } } rootAllDocs[docIndex].push(doc); diff --git a/docusaurus-search-local/src/server/utils/validateOptions.spec.ts b/docusaurus-search-local/src/server/utils/validateOptions.spec.ts index 661b324e..f3281526 100644 --- a/docusaurus-search-local/src/server/utils/validateOptions.spec.ts +++ b/docusaurus-search-local/src/server/utils/validateOptions.spec.ts @@ -46,6 +46,7 @@ describe("validateOptions", () => { removeDefaultStopWordFilter: false, removeDefaultStemmer: false, hideSearchBarWithNoSearchContext: false, + useAllContextsWithNoSearchContext: false, highlightSearchTermsOnTargetPage: false, searchResultLimits: 8, explicitSearchResultPath: false, @@ -71,6 +72,7 @@ describe("validateOptions", () => { removeDefaultStopWordFilter: false, removeDefaultStemmer: false, hideSearchBarWithNoSearchContext: false, + useAllContextsWithNoSearchContext: false, highlightSearchTermsOnTargetPage: false, searchResultLimits: 8, explicitSearchResultPath: false, @@ -96,6 +98,7 @@ describe("validateOptions", () => { removeDefaultStopWordFilter: false, removeDefaultStemmer: false, hideSearchBarWithNoSearchContext: false, + useAllContextsWithNoSearchContext: false, highlightSearchTermsOnTargetPage: false, searchResultLimits: 8, explicitSearchResultPath: false, @@ -121,6 +124,7 @@ describe("validateOptions", () => { removeDefaultStopWordFilter: false, removeDefaultStemmer: false, hideSearchBarWithNoSearchContext: false, + useAllContextsWithNoSearchContext: false, highlightSearchTermsOnTargetPage: false, searchResultLimits: 8, explicitSearchResultPath: false, @@ -137,6 +141,7 @@ describe("validateOptions", () => { blogDir: "src/blog", language: "en", hideSearchBarWithNoSearchContext: false, + useAllContextsWithNoSearchContext: false, highlightSearchTermsOnTargetPage: true, searchResultLimits: 5, explicitSearchResultPath: false, @@ -156,6 +161,7 @@ describe("validateOptions", () => { removeDefaultStopWordFilter: false, removeDefaultStemmer: false, hideSearchBarWithNoSearchContext: false, + useAllContextsWithNoSearchContext: false, highlightSearchTermsOnTargetPage: true, searchResultLimits: 5, explicitSearchResultPath: false, @@ -186,6 +192,7 @@ describe("validateOptions", () => { removeDefaultStopWordFilter: false, removeDefaultStemmer: false, hideSearchBarWithNoSearchContext: false, + useAllContextsWithNoSearchContext: false, highlightSearchTermsOnTargetPage: false, searchResultLimits: 8, explicitSearchResultPath: false, @@ -205,6 +212,7 @@ describe("validateOptions", () => { searchBarPosition: "left", searchContextByPaths: ["docs", "community"], hideSearchBarWithNoSearchContext: true, + useAllContextsWithNoSearchContext: false, }, { blogRouteBasePath: ["/dev/blog"], @@ -219,6 +227,43 @@ describe("validateOptions", () => { removeDefaultStopWordFilter: false, removeDefaultStemmer: false, hideSearchBarWithNoSearchContext: true, + useAllContextsWithNoSearchContext: false, + highlightSearchTermsOnTargetPage: false, + searchResultLimits: 8, + explicitSearchResultPath: false, + searchResultContextMaxLength: 50, + ignoreFiles: [], + searchBarShortcut: true, + searchBarShortcutHint: true, + searchBarPosition: "left", + docsPluginIdForPreferredVersion: "product", + searchContextByPaths: ["docs", "community"], + }, + ], + [ + { + docsRouteBasePath: ["/dev/docs"], + blogRouteBasePath: ["/dev/blog"], + docsPluginIdForPreferredVersion: "product", + hashed: "filename", + searchBarPosition: "left", + searchContextByPaths: ["docs", "community"], + useAllContextsWithNoSearchContext: true, + }, + { + blogRouteBasePath: ["/dev/blog"], + blogDir: ["blog"], + docsRouteBasePath: ["/dev/docs"], + docsDir: ["docs"], + hashed: "filename", + indexBlog: true, + indexDocs: true, + indexPages: false, + language: ["en"], + removeDefaultStopWordFilter: false, + removeDefaultStemmer: false, + hideSearchBarWithNoSearchContext: false, + useAllContextsWithNoSearchContext: true, highlightSearchTermsOnTargetPage: false, searchResultLimits: 8, explicitSearchResultPath: false, diff --git a/docusaurus-search-local/src/server/utils/validateOptions.ts b/docusaurus-search-local/src/server/utils/validateOptions.ts index bc1332a8..86733f60 100644 --- a/docusaurus-search-local/src/server/utils/validateOptions.ts +++ b/docusaurus-search-local/src/server/utils/validateOptions.ts @@ -44,6 +44,7 @@ const schema = Joi.object({ zhUserDictPath: Joi.string(), searchContextByPaths: Joi.array().items(Joi.string()), hideSearchBarWithNoSearchContext: Joi.boolean().default(false), + useAllContextsWithNoSearchContext: Joi.boolean().default(false), }); export function validateOptions({