Skip to content

Commit

Permalink
Add option to useAllContextsWithNoSearchContext
Browse files Browse the repository at this point in the history
  • Loading branch information
aericson committed Feb 10, 2023
1 parent a3b762a commit 56b045d
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions docusaurus-search-local/src/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions docusaurus-search-local/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
6 changes: 6 additions & 0 deletions docusaurus-search-local/src/server/utils/generate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ describe("generate", () => {
"export const indexDocs = true;",
"export const searchContextByPaths = null;",
"export const hideSearchBarWithNoSearchContext = false;",
"export const useAllContextsWithNoSearchContext = false;",
],
],
[
Expand All @@ -59,6 +60,7 @@ describe("generate", () => {
"export const indexDocs = true;",
"export const searchContextByPaths = null;",
"export const hideSearchBarWithNoSearchContext = false;",
"export const useAllContextsWithNoSearchContext = false;",
],
],
[
Expand Down Expand Up @@ -86,6 +88,7 @@ describe("generate", () => {
"export const indexDocs = true;",
"export const searchContextByPaths = null;",
"export const hideSearchBarWithNoSearchContext = false;",
"export const useAllContextsWithNoSearchContext = false;",
],
],
[
Expand Down Expand Up @@ -116,6 +119,7 @@ describe("generate", () => {
"export const indexDocs = true;",
"export const searchContextByPaths = null;",
"export const hideSearchBarWithNoSearchContext = false;",
"export const useAllContextsWithNoSearchContext = false;",
],
],
[
Expand Down Expand Up @@ -144,6 +148,7 @@ describe("generate", () => {
"export const indexDocs = true;",
"export const searchContextByPaths = null;",
"export const hideSearchBarWithNoSearchContext = false;",
"export const useAllContextsWithNoSearchContext = false;",
],
],
[
Expand Down Expand Up @@ -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) => {
Expand Down
7 changes: 6 additions & 1 deletion docusaurus-search-local/src/server/utils/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export function generate(config: ProcessedPluginOptions, dir: string): string {
indexDocs,
searchContextByPaths,
hideSearchBarWithNoSearchContext,
useAllContextsWithNoSearchContext,
} = config;
const indexHash = getIndexHash(config);
const contents: string[] = [
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 8 additions & 2 deletions docusaurus-search-local/src/server/utils/postBuildFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ export function postBuildFactory(
debugInfo("building index");

const docsByDirMap = new Map<string, SearchDocument[][]>();
const { searchContextByPaths, hideSearchBarWithNoSearchContext } = config;
const {
searchContextByPaths,
hideSearchBarWithNoSearchContext,
useAllContextsWithNoSearchContext,
} = config;
if (searchContextByPaths) {
const { baseUrl } = buildData;
const rootAllDocs: SearchDocument[][] = [];
Expand All @@ -58,7 +62,9 @@ export function postBuildFactory(
dirAllDocs[docIndex] = dirDocs = [];
}
dirDocs.push(doc);
continue;
if (!useAllContextsWithNoSearchContext) {
continue;
}
}
}
rootAllDocs[docIndex].push(doc);
Expand Down
45 changes: 45 additions & 0 deletions docusaurus-search-local/src/server/utils/validateOptions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ describe("validateOptions", () => {
removeDefaultStopWordFilter: false,
removeDefaultStemmer: false,
hideSearchBarWithNoSearchContext: false,
useAllContextsWithNoSearchContext: false,
highlightSearchTermsOnTargetPage: false,
searchResultLimits: 8,
explicitSearchResultPath: false,
Expand All @@ -71,6 +72,7 @@ describe("validateOptions", () => {
removeDefaultStopWordFilter: false,
removeDefaultStemmer: false,
hideSearchBarWithNoSearchContext: false,
useAllContextsWithNoSearchContext: false,
highlightSearchTermsOnTargetPage: false,
searchResultLimits: 8,
explicitSearchResultPath: false,
Expand All @@ -96,6 +98,7 @@ describe("validateOptions", () => {
removeDefaultStopWordFilter: false,
removeDefaultStemmer: false,
hideSearchBarWithNoSearchContext: false,
useAllContextsWithNoSearchContext: false,
highlightSearchTermsOnTargetPage: false,
searchResultLimits: 8,
explicitSearchResultPath: false,
Expand All @@ -121,6 +124,7 @@ describe("validateOptions", () => {
removeDefaultStopWordFilter: false,
removeDefaultStemmer: false,
hideSearchBarWithNoSearchContext: false,
useAllContextsWithNoSearchContext: false,
highlightSearchTermsOnTargetPage: false,
searchResultLimits: 8,
explicitSearchResultPath: false,
Expand All @@ -137,6 +141,7 @@ describe("validateOptions", () => {
blogDir: "src/blog",
language: "en",
hideSearchBarWithNoSearchContext: false,
useAllContextsWithNoSearchContext: false,
highlightSearchTermsOnTargetPage: true,
searchResultLimits: 5,
explicitSearchResultPath: false,
Expand All @@ -156,6 +161,7 @@ describe("validateOptions", () => {
removeDefaultStopWordFilter: false,
removeDefaultStemmer: false,
hideSearchBarWithNoSearchContext: false,
useAllContextsWithNoSearchContext: false,
highlightSearchTermsOnTargetPage: true,
searchResultLimits: 5,
explicitSearchResultPath: false,
Expand Down Expand Up @@ -186,6 +192,7 @@ describe("validateOptions", () => {
removeDefaultStopWordFilter: false,
removeDefaultStemmer: false,
hideSearchBarWithNoSearchContext: false,
useAllContextsWithNoSearchContext: false,
highlightSearchTermsOnTargetPage: false,
searchResultLimits: 8,
explicitSearchResultPath: false,
Expand All @@ -205,6 +212,7 @@ describe("validateOptions", () => {
searchBarPosition: "left",
searchContextByPaths: ["docs", "community"],
hideSearchBarWithNoSearchContext: true,
useAllContextsWithNoSearchContext: false,
},
{
blogRouteBasePath: ["/dev/blog"],
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const schema = Joi.object<PluginOptions>({
zhUserDictPath: Joi.string(),
searchContextByPaths: Joi.array().items(Joi.string()),
hideSearchBarWithNoSearchContext: Joi.boolean().default(false),
useAllContextsWithNoSearchContext: Joi.boolean().default(false),
});

export function validateOptions({
Expand Down

0 comments on commit 56b045d

Please sign in to comment.