Skip to content

Commit 8822f78

Browse files
fashxpcmfcmf
andauthored
feat: Add option to show parent categories path in search result (#173)
Co-authored-by: Christian Flach <[email protected]>
1 parent 74f139b commit 8822f78

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ The following options are available (defaults are shown below):
7474
// Do _not_ use Infinity, the value must be a JSON-serializable integer.
7575
indexDocSidebarParentCategories: 0,
7676

77+
// Includes parent categories path in search result
78+
includeParentCategoriesInPageTitle: false,
79+
7780
// whether to index blog pages
7881
indexBlog: true,
7982

packages/docusaurus-search-local/src/server/index.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const validate = (schema, options) => {
1111
const DEFAULT_OPTIONS = {
1212
indexDocs: true,
1313
indexDocSidebarParentCategories: 0,
14+
includeParentCategoriesInPageTitle: false,
1415
indexBlog: true,
1516
indexPages: false,
1617
language: "en",
@@ -57,6 +58,7 @@ it("validates options correctly", () => {
5758
const options = {
5859
indexDocs: false,
5960
indexDocSidebarParentCategories: 3,
61+
includeParentCategoriesInPageTitle: false,
6062
indexBlog: false,
6163
indexPages: true,
6264
language: "hi",

packages/docusaurus-search-local/src/server/index.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ function codeTranslationLocalesToTry(locale: string): string[] {
7676
type MyOptions = {
7777
indexDocs: boolean;
7878
indexDocSidebarParentCategories: number;
79+
includeParentCategoriesInPageTitle: boolean;
7980
indexBlog: boolean;
8081
indexPages: boolean;
8182
language: string | string[];
@@ -124,6 +125,8 @@ const optionsSchema = Joi.object({
124125
.max(Number.MAX_SAFE_INTEGER)
125126
.default(0),
126127

128+
includeParentCategoriesInPageTitle: Joi.boolean().default(false),
129+
127130
indexBlog: Joi.boolean().default(true),
128131

129132
indexPages: Joi.boolean().default(false),
@@ -154,6 +157,7 @@ export default function cmfcmfDocusaurusSearchLocal(
154157
): Plugin<unknown> {
155158
let {
156159
indexDocSidebarParentCategories,
160+
includeParentCategoriesInPageTitle,
157161
indexBlog,
158162
indexDocs,
159163
indexPages,
@@ -543,7 +547,7 @@ export const tokenize = (input) => lunr.tokenizer(input)
543547
indexDocSidebarParentCategories > 0 &&
544548
docSidebarParentCategories
545549
) {
546-
sidebarParentCategories = docSidebarParentCategories
550+
sidebarParentCategories = [...docSidebarParentCategories]
547551
.reverse()
548552
.slice(0, indexDocSidebarParentCategories)
549553
.join(" ");
@@ -570,13 +574,29 @@ export const tokenize = (input) => lunr.tokenizer(input)
570574
sectionTitle,
571575
sectionRoute,
572576
type,
573-
}): MyDocument => ({
574-
id,
575-
pageTitle,
576-
sectionTitle,
577-
sectionRoute,
578-
type,
579-
})
577+
docSidebarParentCategories,
578+
}): MyDocument => {
579+
let fullTitle = pageTitle;
580+
581+
if (
582+
includeParentCategoriesInPageTitle &&
583+
docSidebarParentCategories &&
584+
docSidebarParentCategories.length > 0
585+
) {
586+
fullTitle = [
587+
...docSidebarParentCategories,
588+
pageTitle,
589+
].join(" > ");
590+
}
591+
592+
return {
593+
id,
594+
pageTitle: fullTitle,
595+
sectionTitle,
596+
sectionRoute,
597+
type,
598+
};
599+
}
580600
),
581601
index,
582602
}),

packages/example-docs/docusaurus.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ module.exports = {
126126
],
127127
plugins: [
128128
[require.resolve("@cmfcmf/docusaurus-search-local"), {
129-
indexPages: true
129+
indexPages: true,
130+
includeParentCategoriesInPageTitle: true,
130131
}],
131132
]
132133
};

0 commit comments

Comments
 (0)