Skip to content

Commit

Permalink
feat: Add option to show parent categories path in search result (#173)
Browse files Browse the repository at this point in the history
Co-authored-by: Christian Flach <cmfcmf.flach@gmail.com>
  • Loading branch information
fashxp and cmfcmf authored Jun 9, 2024
1 parent 74f139b commit 8822f78
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ The following options are available (defaults are shown below):
// Do _not_ use Infinity, the value must be a JSON-serializable integer.
indexDocSidebarParentCategories: 0,

// Includes parent categories path in search result
includeParentCategoriesInPageTitle: false,

// whether to index blog pages
indexBlog: true,

Expand Down
2 changes: 2 additions & 0 deletions packages/docusaurus-search-local/src/server/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const validate = (schema, options) => {
const DEFAULT_OPTIONS = {
indexDocs: true,
indexDocSidebarParentCategories: 0,
includeParentCategoriesInPageTitle: false,
indexBlog: true,
indexPages: false,
language: "en",
Expand Down Expand Up @@ -57,6 +58,7 @@ it("validates options correctly", () => {
const options = {
indexDocs: false,
indexDocSidebarParentCategories: 3,
includeParentCategoriesInPageTitle: false,
indexBlog: false,
indexPages: true,
language: "hi",
Expand Down
36 changes: 28 additions & 8 deletions packages/docusaurus-search-local/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ function codeTranslationLocalesToTry(locale: string): string[] {
type MyOptions = {
indexDocs: boolean;
indexDocSidebarParentCategories: number;
includeParentCategoriesInPageTitle: boolean;
indexBlog: boolean;
indexPages: boolean;
language: string | string[];
Expand Down Expand Up @@ -124,6 +125,8 @@ const optionsSchema = Joi.object({
.max(Number.MAX_SAFE_INTEGER)
.default(0),

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

indexBlog: Joi.boolean().default(true),

indexPages: Joi.boolean().default(false),
Expand Down Expand Up @@ -154,6 +157,7 @@ export default function cmfcmfDocusaurusSearchLocal(
): Plugin<unknown> {
let {
indexDocSidebarParentCategories,
includeParentCategoriesInPageTitle,
indexBlog,
indexDocs,
indexPages,
Expand Down Expand Up @@ -543,7 +547,7 @@ export const tokenize = (input) => lunr.tokenizer(input)
indexDocSidebarParentCategories > 0 &&
docSidebarParentCategories
) {
sidebarParentCategories = docSidebarParentCategories
sidebarParentCategories = [...docSidebarParentCategories]
.reverse()
.slice(0, indexDocSidebarParentCategories)
.join(" ");
Expand All @@ -570,13 +574,29 @@ export const tokenize = (input) => lunr.tokenizer(input)
sectionTitle,
sectionRoute,
type,
}): MyDocument => ({
id,
pageTitle,
sectionTitle,
sectionRoute,
type,
})
docSidebarParentCategories,
}): MyDocument => {
let fullTitle = pageTitle;

if (
includeParentCategoriesInPageTitle &&
docSidebarParentCategories &&
docSidebarParentCategories.length > 0
) {
fullTitle = [
...docSidebarParentCategories,
pageTitle,
].join(" > ");
}

return {
id,
pageTitle: fullTitle,
sectionTitle,
sectionRoute,
type,
};
}
),
index,
}),
Expand Down
3 changes: 2 additions & 1 deletion packages/example-docs/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ module.exports = {
],
plugins: [
[require.resolve("@cmfcmf/docusaurus-search-local"), {
indexPages: true
indexPages: true,
includeParentCategoriesInPageTitle: true,
}],
]
};

0 comments on commit 8822f78

Please sign in to comment.