summaryrefslogtreecommitdiffstats
path: root/assets/js/search.js
blob: 2dc5eaa8b982f869bc5736403c87882d24e4dcb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import {searchConfig, i18n} from '@params';

let pagesIndex, searchIndex;

async function initSearchIndex() {
  try {
    const response = await fetch(searchConfig.indexURI);

    if (response.status !== 200) return;

    pagesIndex = await response.json();

    // Create the lunr index for the search
    searchIndex = lunr(function () { // eslint-disable-line no-undef
      // Set up the pipeline for indexing content in multiple languages
      if (Array.isArray(searchConfig.lunrLanguages)) {
        let langs = new Set();
        searchConfig.lunrLanguages.forEach(item => langs.add(item));
        langs.add('en');
        const pipeline = lunr.multiLanguage( // eslint-disable-line no-undef
          ...langs
        );

        this.use(pipeline);
      }

      this.field('objectID');
      this.field('title');
      this.field('tags');
      this.field('content');

      this.ref('href');

      pagesIndex.forEach((page) => this.add(page));
    });
    document.dispatchEvent(new CustomEvent('indexed'));
  } catch (e) {
    console.log(e); // eslint-disable-line no-console
  }
}

function handleSearchQuery(query) {
  if (!query) {
    hideSearchResults();
    return;
  }

  const results = searchSite(query);
  if (!results.length) {
    displayErrorMessage(i18n.noResults);
    hideSearchResults();
    return;
  }

  hideErrorMessage();
  renderSearchResults(query, results);
}

function searchSite(query) {
  const originalQuery = query;
  const lunrQuery = getLunrSearchQuery(query);
  const results = getSearchResults(lunrQuery);

  if (results.length > 0) {
    return results;
  }

  if (lunrQuery !== originalQuery) {
    return getSearchResults(originalQuery);
  }

  return [];
}

function getSearchResults(query) {
  if (typeof searchIndex === 'undefined') return [];

  return searchIndex.search(query).flatMap((hit) => {
    if (hit.ref === 'undefined') return [];
    let pageMatch = pagesIndex.filter((page) => page.href === hit.ref)[0];
    pageMatch.score = hit.score;
    return [pageMatch];
  });
}

function getLunrSearchQuery(query) {
  const searchTerms = query.split(' ');
  if (searchTerms.length === 1) {
    return query;
  }
  let searchQuery = '';
  for (const term of searchTerms) {
    searchQuery += `+${term} `;
  }
  return searchQuery.trim();
}

function displayErrorMessage(message) {
  const searchContainer = document.querySelector('.search-container');
  searchContainer.classList.add('form-item-error');
  searchContainer.classList.remove('focused');
  document.querySelector('.search-error-message').innerHTML = message;
  document.querySelector('.search-error').classList.remove('hide-element');
}

function hideErrorMessage() {
  const searchContainer = document.querySelector('.search-container');
  searchContainer.classList.add('focused');
  searchContainer.classList.remove('form-item-error');
  document.querySelector('.search-error').classList.add('hide-element');
  document.querySelector('.search-error-message').innerHTML = '';
}

function hideSearchResults() {
  document.getElementById('site-search').classList.remove('expanded');
  document.getElementById('search-results').classList.add('hide-element');
}

function renderSearchResults(query, results) {
  clearSearchResults();
  updateSearchResults(query, results);
  showSearchResults();
  scrollToTop();
}

function clearSearchResults() {
  document.getElementById('search-results-body').innerHTML = '';
  document.getElementById('results-count').innerHTML = '';
}

function updateSearchResults(query, results) {
  document.getElementById('search-results-body').innerHTML = results
    .map((hit) => `
    <article class="post" data-score="${hit.score.toFixed(2)}">
      <header>
        <h2 class="post-title">
        <a href="${hit.href}?utm_source=search" class="search-result-page-title">${hit.title}</a>
        </h2>
      </header>
      <p class="post-content">${createSearchResultBlurb(query, hit.content)}</p>
    </article>
    `
    )
    .join('');

  document.getElementById('results-count').innerHTML = results.length;
}

function createSearchResultBlurb(query, pageContent) {
  // g: Global search
  // m: Multi-line search
  // i: Case-insensitive search
  const searchQueryRegex = new RegExp(createQueryStringRegex(query), 'gmi');

  // Since the blurb is comprised of full sentences containing any search
  // term, we need a way to identify where each sentence begins/ends. This
  // regex will be used to produce a list of all sentences from the page
  // content.
  const sentenceBoundaryRegex = /\b\.\s/gm;

  const searchQueryHits = Array.from(
    pageContent.matchAll(searchQueryRegex),
    (m) => m.index
  );

  const sentenceBoundaries = Array.from(
    pageContent.matchAll(sentenceBoundaryRegex),
    (m) => m.index
  );

  let parsedSentence = '';
  let searchResultText = '';
  let lastEndOfSentence = 0;
  for (const hitLocation of searchQueryHits) {
    if (hitLocation > lastEndOfSentence) {
      for (let i = 0; i < sentenceBoundaries.length; i++) {
        if (sentenceBoundaries[i] > hitLocation) {
          const startOfSentence = i > 0 ? sentenceBoundaries[i - 1] + 1 : 0;
          const endOfSentence = sentenceBoundaries[i];
          lastEndOfSentence = endOfSentence;
          parsedSentence = pageContent.slice(startOfSentence, endOfSentence).trim();
          searchResultText += `${parsedSentence} ... `;
          break;
        }
      }
    }
    const searchResultWords = tokenize(searchResultText);
    const pageBreakers = searchResultWords.filter((word) => word.length > 50);
    if (pageBreakers.length > 0) {
      searchResultText = fixPageBreakers(searchResultText, pageBreakers);
    }
    if (searchResultWords.length >= searchConfig.maxSummaryLength) break;
  }
  return ellipsize(searchResultText, searchConfig.maxSummaryLength).replace(
    searchQueryRegex,
    '<span class="search-item">$&</span>'
  );
}

function createQueryStringRegex(query) {
  const escaped = RegExp.escape(query);
  return escaped.split(' ').length === 1 ? `(${escaped})` : `(${escaped.split(' ').join('|')})`;
}

function tokenize(input) {
  // This is a simple regex that produces a list of words from the text
  // it is applied to. This will be used to check the number of total words
  // in the blurb as it is being built.
  const wordRegex = /\b(\w*)[\W|\s|\b]?/gm;

  const wordMatches = Array.from(input.matchAll(wordRegex), (m) => m);
  return wordMatches.map((m) => ({
    word: m[0],
    start: m.index,
    end: m.index + m[0].length,
    length: m[0].length
  }));
}

function fixPageBreakers(input, largeWords) {
  largeWords.forEach((word) => {
    const chunked = chunkify(word.word, 20);
    input = input.replace(word.word, chunked);
  });
  return input;
}

function chunkify(input, chunkSize) {
  let output = '';
  let totalChunks = (input.length / chunkSize) | 0;
  let lastChunkIsUneven = input.length % chunkSize > 0;
  if (lastChunkIsUneven) {
    totalChunks += 1;
  }
  for (let i = 0; i < totalChunks; i++) {
    let start = i * chunkSize;
    let end = start + chunkSize;
    if (lastChunkIsUneven && i === totalChunks - 1) {
      end = input.length;
    }
    output += input.slice(start, end) + ' ';
  }
  return output;
}

function showSearchResults() {
  document.getElementById('search-results').classList.remove('hide-element');
  document.getElementById('site-search').classList.remove('expanded');
}

function scrollToTop() {
  const toTopInterval = setInterval(function () {
    const supportedScrollTop = document.body.scrollTop > 0 ? document.body : document.documentElement;
    if (supportedScrollTop.scrollTop > 0) {
      supportedScrollTop.scrollTop = supportedScrollTop.scrollTop - 50;
    }
    if (supportedScrollTop.scrollTop < 1) {
      clearInterval(toTopInterval);
    }
  }, 10);
}

function ellipsize(input, maxLength) {
  const words = tokenize(input);
  if (words.length <= maxLength) {
    return input;
  }
  return input.slice(0, words[maxLength].end) + '...';
}

// RegExp.escape() polyfill
//
// For more see:
// - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Escaping
// - https://github.com/benjamingr/RegExp.escape/issues/37
if (!Object.prototype.hasOwnProperty.call(RegExp, 'escape')) {
  RegExp.escape = function(str) {
    // $& means the whole matched string
    return str.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&');
  };
}

// 'string'.matchAll(str, regex) polyfill
if (!String.prototype.matchAll) {
  String.prototype.matchAll = function (regex) {
    function ensureFlag(flags, flag) {
      return flags.includes(flag) ? flags : flags + flag;
    }
    function* matchAll(str, regex) {
      const localCopy = new RegExp(regex, ensureFlag(regex.flags, 'g'));
      let match;
      while ((match = localCopy.exec(str))) {
        match.index = localCopy.lastIndex - match[0].length;
        yield match;
      }
    }
    return matchAll(this, regex);
  };
}

function getQueryParam(key) {
  const params = new Proxy(new URLSearchParams(window.location.search), {
    get: (searchParams, prop) => searchParams.get(prop)
  });

  return params[key];
}

initSearchIndex();
document.addEventListener('DOMContentLoaded', function () {
  const searchForm = document.getElementById('search-form');
  const searchInput = document.getElementById('search');

  if (searchForm === null || searchInput === null) {
    return;
  }

  searchForm.addEventListener('submit', (e) => {
    e.preventDefault();
  });

  searchInput.addEventListener('keyup', (e) => {
    e.preventDefault();
    const query = document.getElementById('search')
      .value
      .trim()
      .toLowerCase();
    handleSearchQuery(query);
  });

  searchInput.addEventListener('input', (e) => {
    if (!e.currentTarget.value) {
      hideSearchResults();
    }
  });
});

document.addEventListener('indexed', () => {
  const query = getQueryParam('q');

  if (query) {
    document.getElementById('search').value = query;
    handleSearchQuery(query);
  }
});