Skip to content

Commit daa3db6

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

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ module.exports = (options, ctx, globalCtx) => ({
5656

5757
function normalizeText(text) {
5858
return text
59-
.toLowerCase()
60-
.normalize('NFD')
61-
.replace(/[\u0300-\u036f]/g, '')
59+
.toLowerCase()
60+
.normalize('NFD')
61+
.replace(/[\u0300-\u036f]/g, '')
6262
}
6363

6464
function getCustomTitles(globalCtx) {

services/flexsearchSvc.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
/* global OPTIONS */
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,20 @@ 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+
const topCategoryLevel = OPTIONS.topCategoryLevel || 0
114+
115+
if (pathParts[topCategoryLevel]) {
116+
const topCategoryPath = '/' + _.chunk(pathParts, topCategoryLevel + 1)[0].join('/') + '/'
117+
if (pagesByPath[topCategoryPath]) {
118+
parentPagePath = topCategoryPath;
119+
}
120+
}
121+
122+
if (parentPagePath === '/' && pathParts[0]) {
123+
parentPagePath = `/${pathParts[0]}/`
124+
}
112125

113126
const parentPage = pagesByPath[parentPagePath] || page
114127
return parentPage.title

0 commit comments

Comments
 (0)