| 
									
										
										
										
											2020-03-28 10:42:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | import get from 'lodash/get' | 
					
						
							| 
									
										
										
										
											2020-06-08 10:53:47 +02:00
										 |  |  | import slugify from 'slugify' | 
					
						
							| 
									
										
										
										
											2020-03-28 10:42:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default (query, page, additionalStr = null) => { | 
					
						
							|  |  |  |   let domain = get(page, 'title', '') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (get(page, 'frontmatter.tags')) { | 
					
						
							|  |  |  |     domain += ` ${page.frontmatter.tags.join(' ')}` | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (additionalStr) { | 
					
						
							|  |  |  |     domain += ` ${additionalStr}` | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-06-09 16:11:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-08 10:53:47 +02:00
										 |  |  |   query = slugify(query, {lower: true, strict: true}) | 
					
						
							|  |  |  |   domain = slugify(domain, {lower: true, strict: true}) | 
					
						
							| 
									
										
										
										
											2020-03-28 10:42:54 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return matchTest(query, domain) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const matchTest = (query, domain) => { | 
					
						
							| 
									
										
										
										
											2020-06-09 16:11:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-28 10:42:54 +01:00
										 |  |  |   const escapeRegExp = str => str.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const words = query | 
					
						
							|  |  |  |     .split(/\s+/g) | 
					
						
							|  |  |  |     .map(str => str.trim()) | 
					
						
							|  |  |  |     .filter(str => !!str) | 
					
						
							|  |  |  |   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
 | 
					
						
							|  |  |  |           return `(?=.*\\b${escapeRegExp(word)})` | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |           // Not the last word - expect the whole word exactly
 | 
					
						
							|  |  |  |           return `(?=.*\\b${escapeRegExp(word)}\\b)` | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }) | 
					
						
							|  |  |  |       .join('') + '.+', | 
					
						
							|  |  |  |     'gi' | 
					
						
							|  |  |  |   ) | 
					
						
							|  |  |  |   return searchRegex.test(domain) | 
					
						
							|  |  |  | } |