Skip to content

Commit

Permalink
Merge pull request #383 from easyops-cn/steve/custom-search-context-l…
Browse files Browse the repository at this point in the history
…abels

feat(): customize search context labels
  • Loading branch information
weareoutman committed Dec 15, 2023
2 parents e29f21f + 33787d5 commit 5fe4374
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 43 deletions.
56 changes: 28 additions & 28 deletions README.md

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions docusaurus-search-local/src/client/theme/SearchBar/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,14 @@ export default function SearchBar({
let nextSearchContext = "";
if (location.pathname.startsWith(versionUrl)) {
const uri = location.pathname.substring(versionUrl.length);
const matchedPath = searchContextByPaths.find(
(path) => uri === path || uri.startsWith(`${path}/`)
);
let matchedPath: string | undefined;
for (const _path of searchContextByPaths) {
const path = typeof _path === "string" ? _path : _path.path;
if (uri === path || uri.startsWith(`${path}/`)) {
matchedPath = path;
break;
}
}
if (matchedPath) {
nextSearchContext = matchedPath;
}
Expand Down
19 changes: 14 additions & 5 deletions docusaurus-search-local/src/client/theme/SearchPage/SearchPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,20 @@ function SearchPageContent(): React.ReactElement {
})
: ""}
</option>
{searchContextByPaths.map((context: string) => (
<option key={context} value={context}>
{context}
</option>
))}
{searchContextByPaths.map((context) => {
let label: string;
let path: string;
if (typeof context === "string") {
label = path = context;
} else {
({ label, path } = context);
}
return (
<option key={path} value={path}>
{label}
</option>
);
})}
</select>
</div>
) : null}
Expand Down
5 changes: 4 additions & 1 deletion docusaurus-search-local/src/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ declare module "*/generated.js" {
export const searchBarPosition: "left" | "right";
export const docsPluginIdForPreferredVersion: string;
export const indexDocs: boolean;
export const searchContextByPaths: string[];
export const searchContextByPaths: (
| string
| { label: string; path: string }
)[];
export const hideSearchBarWithNoSearchContext: boolean;
export const useAllContextsWithNoSearchContext: boolean;
// These below are for mocking only.
Expand Down
2 changes: 1 addition & 1 deletion docusaurus-search-local/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export interface PluginOptions {
* 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.
*/
searchContextByPaths?: string[];
searchContextByPaths?: (string | { label: string; path: string })[];

/**
* Whether to hide the search bar when no search context was matched.
Expand Down
11 changes: 8 additions & 3 deletions docusaurus-search-local/src/server/utils/postBuildFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,14 @@ export function postBuildFactory(
for (const doc of documents) {
if (doc.u.startsWith(baseUrl)) {
const uri = doc.u.substring(baseUrl.length);
const matchedPath = searchContextByPaths.find(
(path) => uri === path || uri.startsWith(`${path}/`)
);
let matchedPath: string | undefined;
for (const _path of searchContextByPaths) {
const path = typeof _path === "string" ? _path : _path.path;
if (uri === path || uri.startsWith(`${path}/`)) {
matchedPath = path;
break;
}
}
if (matchedPath) {
let dirAllDocs = docsByDirMap.get(matchedPath);
if (!dirAllDocs) {
Expand Down
10 changes: 9 additions & 1 deletion docusaurus-search-local/src/server/utils/validateOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ const schema = Joi.object<PluginOptions>({
docsPluginIdForPreferredVersion: Joi.string(),
zhUserDict: Joi.string(),
zhUserDictPath: Joi.string(),
searchContextByPaths: Joi.array().items(Joi.string()),
searchContextByPaths: Joi.array().items(
Joi.alternatives().try(
Joi.string(),
Joi.object<{ label: string; path: string }>({
label: Joi.string(),
path: Joi.string(),
})
)
),
hideSearchBarWithNoSearchContext: Joi.boolean().default(false),
useAllContextsWithNoSearchContext: Joi.boolean().default(false),
});
Expand Down
5 changes: 4 additions & 1 deletion website-multi-docs/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ const config = {
docsRouteBasePath: ["docs", "community"],
docsDir: ["docs", "community"],
docsPluginIdForPreferredVersion: "product",
searchContextByPaths: ["docs", "community"],
searchContextByPaths: [
"docs",
{ label: "Community", path: "community" },
],
hideSearchBarWithNoSearchContext: true,
}),
],
Expand Down

0 comments on commit 5fe4374

Please sign in to comment.