refactor(docs-infra): simplify regex in search WebWorker (#41693)

This commit simplifies a regex used in angular.io's search WebWorker. It
also updates some comments to add more context on what the code does.

PR Close #41693
This commit is contained in:
George Kalpakas 2021-04-18 16:14:48 +03:00 committed by Andrew Kushnir
parent 343b9d0ddc
commit f10a7e1f7e

View File

@ -119,7 +119,8 @@ function queryIndex(query: string): PageInfo[] {
try {
if (query.length) {
// First try a query where every term must be present
const queryAll = query.replace(/(^|\s)([^\s]+)/g, '$1+$2');
// (see https://lunrjs.com/guides/searching.html#term-presence)
const queryAll = query.replace(/\S+/g, '+$&');
let results = index.search(queryAll);
// If that was too restrictive just query for any term to be present
@ -129,7 +130,7 @@ function queryIndex(query: string): PageInfo[] {
// If that is still too restrictive then search in the title for the first word in the query
if (results.length === 0) {
// E.g. if the search is "ngCont guide" then we search for "ngCont guide title:ngCont*"
// E.g. if the search is "ngCont guide" then we search for "ngCont guide title:*ngCont*"
const titleQuery = 'title:*' + query.split(' ', 1)[0] + '*';
results = index.search(query + ' ' + titleQuery);
}