Skip to content

Commit 9f3f49c

Browse files
authored
fix($plugin-search): match non-ASCII chars (close #2242) (#2283)
1 parent 97b4684 commit 9f3f49c

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

packages/@vuepress/plugin-search/match-query.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,19 @@ export default (query, page, additionalStr = null) => {
1818
const matchTest = (query, domain) => {
1919
const escapeRegExp = str => str.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&')
2020

21+
// eslint-disable-next-line no-control-regex
22+
const nonASCIIRegExp = new RegExp('[^\x00-\x7F]')
23+
2124
const words = query
2225
.split(/\s+/g)
2326
.map(str => str.trim())
2427
.filter(str => !!str)
25-
const hasTrailingSpace = query.endsWith(' ')
26-
const searchRegex = new RegExp(
27-
words
28+
29+
if (!nonASCIIRegExp.test(query)) {
30+
// if the query only has ASCII chars, treat as English
31+
const hasTrailingSpace = query.endsWith(' ')
32+
const searchRegex = new RegExp(
33+
words
2834
.map((word, index) => {
2935
if (words.length === index + 1 && !hasTrailingSpace) {
3036
// The last word - ok with the word being "startswith"-like
@@ -35,8 +41,11 @@ const matchTest = (query, domain) => {
3541
}
3642
})
3743
.join('') + '.+',
38-
'gi'
39-
)
40-
return searchRegex.test(domain)
44+
'gi'
45+
)
46+
return searchRegex.test(domain)
47+
} else {
48+
// if the query has non-ASCII chars, treat as other languages
49+
return words.some(word => domain.toLowerCase().indexOf(word) > -1)
50+
}
4151
}
42-

0 commit comments

Comments
 (0)