Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(): customize search context labels #383

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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