Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}