From b9f1655547862fddb178014e42cfe50abd0d40d3 Mon Sep 17 00:00:00 2001 From: meteorlxy Date: Mon, 6 Apr 2020 12:35:40 +0800 Subject: [PATCH] fix($plugin-search): match non-ASCII chars (close #2242) --- .../@vuepress/plugin-search/match-query.js | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/packages/@vuepress/plugin-search/match-query.js b/packages/@vuepress/plugin-search/match-query.js index d053384cbd..c1c41f3ba3 100644 --- a/packages/@vuepress/plugin-search/match-query.js +++ b/packages/@vuepress/plugin-search/match-query.js @@ -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 @@ -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) + } } -