Skip to content

Commit

Permalink
fix($plugin-search): match non-ASCII chars (close #2242) (#2283)
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy authored Apr 8, 2020
1 parent 97b4684 commit 9f3f49c
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions packages/@vuepress/plugin-search/match-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@ export default (query, page, additionalStr = null) => {
const matchTest = (query, domain) => {
const escapeRegExp = str => str.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&')

// eslint-disable-next-line no-control-regex
const nonASCIIRegExp = new RegExp('[^\x00-\x7F]')

const words = query
.split(/\s+/g)
.map(str => str.trim())
.filter(str => !!str)
const hasTrailingSpace = query.endsWith(' ')
const searchRegex = new RegExp(
words

if (!nonASCIIRegExp.test(query)) {
// if the query only has ASCII chars, treat as English
const hasTrailingSpace = query.endsWith(' ')
const searchRegex = new RegExp(
words
.map((word, index) => {
if (words.length === index + 1 && !hasTrailingSpace) {
// The last word - ok with the word being "startswith"-like
Expand All @@ -35,8 +41,11 @@ const matchTest = (query, domain) => {
}
})
.join('') + '.+',
'gi'
)
return searchRegex.test(domain)
'gi'
)
return searchRegex.test(domain)
} else {
// if the query has non-ASCII chars, treat as other languages
return words.some(word => domain.toLowerCase().indexOf(word) > -1)
}
}

0 comments on commit 9f3f49c

Please sign in to comment.