1
0

match-query.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import get from 'lodash/get';
  2. export default (query, page, additionalStr = null) => {
  3. let domain = get(page, 'title', '')
  4. if (get(page, 'frontmatter.tags')) {
  5. domain += ` ${page.frontmatter.tags.join(' ')}`
  6. }
  7. if (additionalStr) {
  8. domain += ` ${additionalStr}`
  9. }
  10. // Search page.pagecontent instead of domain
  11. // var res = matchTest(query, domain) ;
  12. if (query.length < 4) return false;
  13. var res = matchTest(query, page.pagecontent);
  14. return res;
  15. }
  16. const matchTest = (query, domain) => {
  17. const escapeRegExp = str => str.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&')
  18. // eslint-disable-next-line no-control-regex
  19. const nonASCIIRegExp = new RegExp('[^\x00-\x7F]')
  20. const words = query
  21. .split(/\s+/g)
  22. .map(str => str.trim())
  23. .filter(str => !!str)
  24. if (!nonASCIIRegExp.test(query)) {
  25. // if the query only has ASCII chars, treat as English
  26. const hasTrailingSpace = query.endsWith(' ')
  27. const searchRegex = new RegExp(
  28. words
  29. .map((word, index) => {
  30. if (words.length === index + 1 && !hasTrailingSpace) {
  31. // The last word - ok with the word being "startswith"-like
  32. return `(?=.*\\b${escapeRegExp(word)})`
  33. } else {
  34. // Not the last word - expect the whole word exactly
  35. return `(?=.*\\b${escapeRegExp(word)}\\b)`
  36. }
  37. })
  38. .join('') + '.+',
  39. 'gi'
  40. )
  41. return searchRegex.test(domain)
  42. } else {
  43. // if the query has non-ASCII chars, treat as other languages
  44. return words.some(word => domain.toLowerCase().indexOf(word) > -1)
  45. }
  46. }