Skip to content

Commit

Permalink
feat: Add option to filter search results by url/pathname
Browse files Browse the repository at this point in the history
  • Loading branch information
Herbert Nikolajewski committed Dec 9, 2024
1 parent ab8f8d6 commit 27058df
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 4 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ The following options are available (defaults are shown below):
// searches, but simply does not display additional search results that have been found.
maxSearchResults: 8,

// This option allows restricting search context to the current URL.
filterByPathName: false,

// This option only works if filterByPathName is set to true. It allows further filtering of the search context.
// Example: Suppose this is our endpoint: "/help/page1/subpage1/topic". If I set subPath: 2, then the search context
// will be limited to everything that can be found under help/page1/*. Anything that's under /help/page3 or similar
// will be filtered out. subPath: 3 => then only help/page1/subpage1/* not help/page1/subpage2/*, etc.
// The default value is set to -1, which always allows searching the current page only as search context.
subPath: -1,

// lunr.js-specific settings
lunr: {
// When indexing your documents, their content is split into "tokens".
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ const SearchBar = () => {
parentCategoriesBoost,
indexDocSidebarParentCategories,
maxSearchResults,
filterByPathName,
subPath,
} = usePluginData("@cmfcmf/docusaurus-search-local") as DSLAPluginData;

const history = useHistory<DSLALocationState>();
Expand Down Expand Up @@ -310,8 +312,8 @@ const SearchBar = () => {
const terms = tokenize(input);

return indexes
.flatMap(({ index, documents }) =>
index
.flatMap(({ index, documents }) => {
const queriedIndex = index
.query((query) => {
query.term(terms, {
fields: ["title"],
Expand Down Expand Up @@ -360,8 +362,21 @@ const SearchBar = () => {
)!,
score: result.score,
terms,
})),
)
}));

if (filterByPathName) {
const pathNameArray = window.location.pathname.split("/");
const pathNamePart =
pathNameArray[
subPath === -1 ? pathNameArray.length - 1 : subPath
];
return queriedIndex.filter((item) =>
item.document.sectionRoute.includes(pathNamePart),
);
}

return queriedIndex;
})
.sort((a, b) => b.score - a.score)
.slice(0, maxSearchResults);
},
Expand Down
4 changes: 4 additions & 0 deletions packages/docusaurus-search-local/src/server/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const DEFAULT_OPTIONS = {
language: "en",
style: undefined,
maxSearchResults: 8,
filterByPathName: false,
subPath: -1,
lunr: {
tokenizerSeparator: undefined,
b: 0.75,
Expand Down Expand Up @@ -64,6 +66,8 @@ it("validates options correctly", () => {
language: "hi",
style: "none",
maxSearchResults: 123,
filterByPathName: false,
subPath: -1,
lunr: {
tokenizerSeparator: /-+/,
b: 0.6,
Expand Down
10 changes: 10 additions & 0 deletions packages/docusaurus-search-local/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ type MyOptions = {
language: string | string[];
style?: "none";
maxSearchResults: number;
filterByPathName: boolean;
subPath: number;
lunr: {
tokenizerSeparator?: string;
k1: number;
Expand Down Expand Up @@ -140,6 +142,10 @@ const optionsSchema = Joi.object({

maxSearchResults: Joi.number().integer().min(1).default(8),

filterByPathName: Joi.boolean().default(false),

subPath: Joi.number().integer().min(-1).default(-1),

lunr: Joi.object({
tokenizerSeparator: Joi.object().regex(),
b: Joi.number().min(0).max(1).default(0.75),
Expand All @@ -164,6 +170,8 @@ export default function cmfcmfDocusaurusSearchLocal(
language,
style,
maxSearchResults,
filterByPathName,
subPath,
lunr: {
tokenizerSeparator: lunrTokenizerSeparator,
k1,
Expand Down Expand Up @@ -300,6 +308,8 @@ export const tokenize = (input) => lunr.tokenizer(input)
parentCategoriesBoost,
indexDocSidebarParentCategories,
maxSearchResults,
filterByPathName,
subPath,
};
setGlobalData(data);
},
Expand Down
2 changes: 2 additions & 0 deletions packages/docusaurus-search-local/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export type DSLAPluginData = {
tagsBoost: number;
parentCategoriesBoost: number;
maxSearchResults: number;
filterByPathName: boolean;
subPath: number;
};

export type MyDocument = {
Expand Down

0 comments on commit 27058df

Please sign in to comment.