Skip to content

Commit

Permalink
feat: Add option to show parent categories path in search result
Browse files Browse the repository at this point in the history
  • Loading branch information
fashxp authored and cmfcmf committed Jun 9, 2024
1 parent 74f139b commit 8364771
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,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: true,

// 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,8 @@ export const tokenize = (input) => lunr.tokenizer(input)
indexDocSidebarParentCategories > 0 &&
docSidebarParentCategories
) {
sidebarParentCategories = docSidebarParentCategories
const clonedDocSidebarParentCategories = [... docSidebarParentCategories];
sidebarParentCategories = clonedDocSidebarParentCategories
.reverse()
.slice(0, indexDocSidebarParentCategories)
.join(" ");
Expand All @@ -570,13 +575,28 @@ 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
.join(' > ') + ' > ' + pageTitle;
}

return {
id,
pageTitle: fullTitle,
sectionTitle,
sectionRoute,
type,
}
}
),
index,
}),
Expand Down

0 comments on commit 8364771

Please sign in to comment.