Skip to content

Commit 97ff543

Browse files
committed
[Resolve leo-buneev#43] Add configuration for flexsearch
1 parent 5621b5e commit 97ff543

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,40 @@ async function processSuggestions(suggestions: Suggestion[], queryString: string
113113
*/
114114
async function onGoToSuggestion(index: number, suggestion: Suggestion, queryString: string, queryTerms: string[]): Boolean?
115115
```
116+
117+
### Additional configuration
118+
119+
You can set additional configuration options in the config file. For `tokenize`, `split`, and
120+
`encode`, see the [flexsearch documentation][flexsearch-options] for more details.
121+
122+
* `tokenize`: The indexing mode (tokenizer). Choose one of the built-ins or pass a custom tokenizer
123+
function.
124+
* `split`: The rule to split words when using non-custom tokenizer (built-ins e.g. "forward"). Use a
125+
string/char or use a regular expression (default: `/\W+/`).
126+
* `encode`: The encoding type. Choose one of the built-ins or pass a custom encoding function.
127+
128+
The built-in `searchMaxSuggestions` will be used if set in the
129+
[theme config][search-max-suggestions].
130+
131+
For example:
132+
133+
```js
134+
module.exports = {
135+
plugins: [
136+
[
137+
'fulltext-search',
138+
{
139+
tokenize: 'full',
140+
split: /\s+/,
141+
encode: 'icase',
142+
},
143+
],
144+
],
145+
themeConfig: {
146+
searchMaxSuggestions: 10,
147+
}
148+
}
149+
```
150+
151+
[flexsearch-options]: [https://github.com/nextapps-de/flexsearch#initialize-index]
152+
[search-max-suggestions]: https://vuepress.vuejs.org/theme/default-theme-config.html#built-in-search

components/SearchBox.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,10 @@ export default {
8888
this.getSuggestions()
8989
},
9090
},
91+
/* global OPTIONS */
9192
mounted() {
92-
flexsearchSvc.buildIndex(this.$site.pages)
93+
const options = OPTIONS || {};
94+
flexsearchSvc.buildIndex(this.$site.pages, options)
9395
this.placeholder = this.$site.themeConfig.searchPlaceholder || ''
9496
document.addEventListener('keydown', this.onHotkey)
9597

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ module.exports = (options, ctx, globalCtx) => ({
4747
content: options.hooks || 'export default {}',
4848
}
4949
},
50+
define() {
51+
return {
52+
OPTIONS: options,
53+
}
54+
},
5055
})
5156

5257
function normalizeText(text) {

services/flexsearchSvc.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ let pagesByPath = null
1212
const cjkRegex = /[\u3131-\u314e|\u314f-\u3163|\uac00-\ud7a3]|[\u4E00-\u9FCC\u3400-\u4DB5\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29]|[\ud840-\ud868][\udc00-\udfff]|\ud869[\udc00-\uded6\udf00-\udfff]|[\ud86a-\ud86c][\udc00-\udfff]|\ud86d[\udc00-\udf34\udf40-\udfff]|\ud86e[\udc00-\udc1d]|[\u3041-\u3096]|[\u30A1-\u30FA]/giu
1313

1414
export default {
15-
buildIndex(allPages) {
15+
buildIndex(allPages, options) {
1616
const pages = allPages.filter(p => !p.frontmatter || p.frontmatter.search !== false)
1717
const indexSettings = {
18-
encode: 'simple',
19-
tokenize: 'forward',
18+
encode: options.encode || 'simple',
19+
tokenize: options.tokenize || 'forward',
20+
split: options.split || /\W+/,
2021
async: true,
2122
doc: {
2223
id: 'key',

0 commit comments

Comments
 (0)