hematite 0.0.12 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/assets/js/search.mjs CHANGED
@@ -54,6 +54,25 @@ class Searcher {
54
54
 
55
55
  }
56
56
 
57
+ /// Get number of full matches for [query] in [text].
58
+ /// @precondition [text] is already in a searchable (i.e.
59
+ /// filtered) form.
60
+ getNumberOfMatches(query, text) {
61
+ query = query.toLowerCase();
62
+
63
+ return text.toLowerCase().split(query).length - 1;
64
+ }
65
+
66
+ /// Get the index of the first full match for [query] in
67
+ /// [text], starting at [startPos]. Returns -1 if no match
68
+ /// is found.
69
+ /// @precondition [text] is already in a searchable form.
70
+ getIdxOfFirstMatch(query, text, startPos) {
71
+ query = query.toLowerCase();
72
+
73
+ return text.toLowerCase().indexOf(query, startPos);
74
+ }
75
+
57
76
  /// Get the container element for the [n]th search
58
77
  /// result for [query] in the given [elem].
59
78
  /// Returns an Element.
@@ -77,7 +96,7 @@ class Searcher {
77
96
 
78
97
  // If the current node is a leaf,
79
98
  if (elem.childNodes.length == 0) {
80
- let numMatches = searchText.split(query).length - 1;
99
+ let numMatches = this.getNumberOfMatches(query, searchText);
81
100
  n -= numMatches;
82
101
 
83
102
  // If we've considered enough matches,
@@ -154,7 +173,7 @@ class Searcher {
154
173
  index ++;
155
174
  pageData.numMatches ++;
156
175
  startPos = matchLoc + query.length;
157
- matchLoc = toSearch.indexOf(query, startPos);
176
+ matchLoc = this.getIdxOfFirstMatch(query, toSearch, startPos);
158
177
  }
159
178
  }
160
179
 
@@ -185,26 +204,52 @@ class Searcher {
185
204
  }
186
205
  }
187
206
 
207
+ /// Extract a search query and index from the current page's URL.
208
+ function getUrlQuery() {
209
+ let query, resultIndex;
210
+
211
+ let urlArgs = UrlHelper.getPageArgs();
212
+ let pageHash = UrlHelper.getPageHash();
213
+
214
+ if (urlArgs === null) {
215
+ return;
216
+ }
217
+
218
+ // The page's hash also causes scrolling. Don't focus
219
+ // if the page has a hash.
220
+ if (pageHash != null) {
221
+ return;
222
+ }
223
+
224
+ query = urlArgs.query;
225
+ resultIndex = parseInt(urlArgs.index);
226
+
227
+ if (urlArgs.index === undefined) {
228
+ resultIndex = 0;
229
+ }
230
+
231
+ return { query, resultIndex };
232
+ }
233
+
188
234
  /// Scrolls to and shows the search result for the given [query]
189
235
  /// If neither [query] nor [resultIndex] are given, attempt to get them from
190
236
  /// the page's arguments (i.e. from https://example.com/...?search=...,n=...).
191
237
  function focusSearchResult(searcher, elem, query, resultIndex) {
192
- if (query === undefined && resultIndex === undefined) {
193
- let urlArgs = UrlHelper.getPageArgs();
194
- let pageHash = UrlHelper.getPageHash();
238
+ if (elem === undefined) {
239
+ console.warn(`focusSearchResult requires [elem] to function. Not focusing a result.`);
240
+ return;
241
+ }
195
242
 
196
- if (urlArgs === null) {
197
- return;
198
- }
243
+ if (query === undefined && resultIndex === undefined) {
244
+ let pageArgs = getUrlQuery();
199
245
 
200
- // The page's hash also causes scrolling. Don't focus
201
- // if the page has a hash.
202
- if (pageHash != null) {
246
+ // No args, nothing to focus.
247
+ if (!pageArgs) {
203
248
  return;
204
249
  }
205
250
 
206
- query = urlArgs.query;
207
- resultIndex = parseInt(urlArgs.index);
251
+ resultIndex = pageArgs.resultIndex;
252
+ query = pageArgs.query;
208
253
 
209
254
  if (isNaN(resultIndex)) {
210
255
  console.warn("Unable to navigate to result. Given idx is NaN");
@@ -355,4 +400,4 @@ function handleSearch(searcher) {
355
400
  }
356
401
 
357
402
  export default handleSearch;
358
- export { handleSearch, Searcher };
403
+ export { handleSearch, getUrlQuery, Searcher };