Skip to content

Commit f36ed07

Browse files
author
Leonid Buneev
committed
v 2.0.2 - added support for chinese and cyrillic characters
1 parent d20e522 commit f36ed07

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vuepress-plugin-fulltext-search",
3-
"version": "2.0.1",
3+
"version": "2.0.2",
44
"description": "Adds full-text search capabilities to your vuepress site with a help of flexsearch library.",
55
"main": "index.js",
66
"repository": "https://github.com/leo-buneev/vuepress-plugin-fulltext-search.git",

services/flexsearchSvc.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ let cyrillicIndex = null
99
let cjkIndex = null
1010
let pagesByPath = null
1111

12+
const cjkRegex = /[\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]/giu
13+
1214
export default {
1315
buildIndex(pages) {
1416
const indexSettings = {
@@ -31,16 +33,29 @@ export default {
3133
if (cyrillicPages.length) {
3234
cyrillicIndex = new Flexsearch({
3335
...indexSettings,
34-
// charset: cyrillicCharset,
36+
encode: false,
37+
split: /\s+/,
38+
tokenize: 'forward',
3539
})
36-
cyrillicIndex.push(cyrillicPages)
40+
cyrillicIndex.add(cyrillicPages)
3741
}
3842
if (cjkPages.length) {
3943
cjkIndex = new Flexsearch({
4044
...indexSettings,
41-
// charset: cjkCharset,
45+
encode: false,
46+
tokenize: function(str) {
47+
const cjkWords = []
48+
let m = null
49+
do {
50+
m = cjkRegex.exec(str)
51+
if (m) {
52+
cjkWords.push(m[0])
53+
}
54+
} while (m)
55+
return cjkWords
56+
},
4257
})
43-
cjkIndex.push(cjkPages)
58+
cjkIndex.add(cjkPages)
4459
}
4560
pagesByPath = _.keyBy(pages, 'path')
4661
},
@@ -67,7 +82,7 @@ export default {
6782
const searchResult1 = await index.search(searchParams)
6883
const searchResult2 = cyrillicIndex ? await cyrillicIndex.search(searchParams) : []
6984
const searchResult3 = cjkIndex ? await cjkIndex.search(searchParams) : []
70-
const searchResult = [...searchResult1, ...searchResult2, ...searchResult3]
85+
const searchResult = _.uniqBy([...searchResult1, ...searchResult2, ...searchResult3], 'path')
7186
const result = searchResult.map(page => ({
7287
...page,
7388
parentPageTitle: getParentPageTitle(page),

0 commit comments

Comments
 (0)