Skip to content

Commit 3888ade

Browse files
committed
introduce topCategoryLevel options
fix #25
1 parent cf4f74d commit 3888ade

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,26 @@ module.exports = {
148148

149149
[flexsearch-options]: https://github.com/nextapps-de/flexsearch#initialize-index
150150
[search-max-suggestions]: https://vuepress.vuejs.org/theme/default-theme-config.html#built-in-search
151+
152+
153+
154+
### Top Category Level
155+
156+
You can define an option `topCategoryLevel` in order to define what is the top page title that will be displayed on search Box.
157+
Default is `0` meaning "top-most" category.
158+
If you define a level that don't exist for this page, it fails back to 0.
159+
160+
```js
161+
// /docs/.vuepress/config.js
162+
module.exports = {
163+
plugins: [
164+
[
165+
'fulltext-search',
166+
{
167+
// provide the contents of a JavaScript file
168+
topCategoryLevel: 1
169+
},
170+
],
171+
],
172+
}
173+
```

index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ const _ = require('lodash')
55
let customTitles = null
66

77
module.exports = (options, ctx, globalCtx) => ({
8+
define: {
9+
FTS_TOP_CATEGORY_LEVEL: options.topCategoryLevel || 0,
10+
},
811
extendPageData($page) {
912
try {
1013
const { html } = $page._context.markdown.render($page._strippedContent || '')
@@ -56,9 +59,9 @@ module.exports = (options, ctx, globalCtx) => ({
5659

5760
function normalizeText(text) {
5861
return text
59-
.toLowerCase()
60-
.normalize('NFD')
61-
.replace(/[\u0300-\u036f]/g, '')
62+
.toLowerCase()
63+
.normalize('NFD')
64+
.replace(/[\u0300-\u036f]/g, '')
6265
}
6366

6467
function getCustomTitles(globalCtx) {

services/flexsearchSvc.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* global FTS_TOP_CATEGORY_LEVEL */
2+
13
import Flexsearch from 'flexsearch'
24
// Use when flexSearch v0.7.0 will be available
35
// import cyrillicCharset from 'flexsearch/dist/lang/cyrillic/default.min.js'
@@ -106,9 +108,19 @@ export default {
106108
}
107109

108110
function getParentPageTitle(page) {
109-
const pathParts = page.path.split('/')
111+
const pathParts = _.drop(page.path.split('/'))
110112
let parentPagePath = '/'
111-
if (pathParts[1]) parentPagePath = `/${pathParts[1]}/`
113+
114+
if (pathParts[FTS_TOP_CATEGORY_LEVEL]) {
115+
const topCategoryPath = '/' + _.chunk(pathParts, FTS_TOP_CATEGORY_LEVEL + 1)[0].join('/') + '/'
116+
if (pagesByPath[topCategoryPath]) {
117+
parentPagePath = pagesByPath;
118+
}
119+
}
120+
121+
if (parentPagePath == '/' && pathParts[0]) {
122+
parentPagePath = `/${pathParts[0]}/`
123+
}
112124

113125
const parentPage = pagesByPath[parentPagePath] || page
114126
return parentPage.title

0 commit comments

Comments
 (0)