|
30 | 30 | worker = new Worker('/js/worker.js'); |
31 | 31 | const url = '/json/lunr-index.json'; |
32 | 32 |
|
33 | | - worker.postMessage({ type: 'init', url: url }); |
| 33 | + worker.postMessage({ |
| 34 | + type: 'init', |
| 35 | + lunrIndexUrl: url, |
| 36 | + rawIndexUrl: $searchInput.data('offline-search-index-json-src') |
| 37 | + }); |
34 | 38 |
|
35 | 39 | worker.onerror = function (error) { |
36 | 40 | console.error('Error in worker:', error); |
37 | 41 | }; |
38 | 42 | } |
39 | | - |
40 | | - $.ajax($searchInput.data('offline-search-index-json-src')).then( |
41 | | - (data) => { |
42 | | - data.forEach((doc) => { |
43 | | - resultDetails.set(doc.ref, { |
44 | | - version: doc.version, |
45 | | - title: doc.title, |
46 | | - excerpt: doc.excerpt, |
47 | | - }); |
48 | | - }); |
49 | | - } |
50 | | - ); |
51 | 43 |
|
52 | 44 | let currentTarget = null; |
53 | 45 |
|
54 | 46 | worker.onmessage = function (event) { |
55 | 47 | if (event.data.type === 'search') { |
56 | | - const results = event.data.results |
57 | | - console.log('Search results:', results); |
| 48 | + const docs = event.data.docs; |
58 | 49 | const $html = $('<div>'); |
59 | 50 |
|
60 | 51 | $html.append( |
|
87 | 78 | }); |
88 | 79 | $html.append($searchResultBody); |
89 | 80 |
|
90 | | - if (results.length === 0) { |
| 81 | + if (docs.size === 0) { |
91 | 82 | currentTarget.append( |
92 | 83 | $('<p>').text(`No results found for query "${searchQuery}"`) |
93 | 84 | ); |
94 | 85 | } else { |
95 | | - results.forEach((r) => { |
96 | | - const doc = resultDetails.get(r.ref); |
| 86 | + docs.forEach((doc, key) => { |
| 87 | + if (doc === undefined) { |
| 88 | + return; |
| 89 | + } |
97 | 90 |
|
98 | 91 | const href = |
99 | 92 | $searchInput.data('offline-search-base-href') + |
100 | | - r.ref.replace(/^\//, ''); |
| 93 | + key.replace(/^\//, ''); |
101 | 94 |
|
102 | 95 | const $entry = $('<div>').addClass('mt-4').addClass('search-result'); |
103 | 96 |
|
|
112 | 105 | ); |
113 | 106 |
|
114 | 107 | $entry.append( |
115 | | - $('<small>').addClass('d-block text-muted').text(r.ref) |
| 108 | + $('<small>').addClass('d-block text-muted').text(key) |
116 | 109 | ); |
117 | 110 |
|
118 | 111 | $entry.append($('<p>').text(doc.excerpt)); |
|
150 | 143 | } |
151 | 144 |
|
152 | 145 | worker.postMessage({ type: 'search', query: searchQuery, maxResults: $targetSearchInput.data('offline-search-max-results') }); |
153 | | - |
154 | | - // const results = idx |
155 | | - // .query((q) => { |
156 | | - // const tokens = lunr.tokenizer(searchQuery.toLowerCase()); |
157 | | - // tokens.forEach((token) => { |
158 | | - // const queryString = token.toString(); |
159 | | - // q.term(queryString, { |
160 | | - // boost: 100, |
161 | | - // }); |
162 | | - // q.term(queryString, { |
163 | | - // wildcard: |
164 | | - // lunr.Query.wildcard.LEADING | |
165 | | - // lunr.Query.wildcard.TRAILING, |
166 | | - // boost: 10, |
167 | | - // }); |
168 | | - // q.term(queryString, { |
169 | | - // editDistance: 2, |
170 | | - // }); |
171 | | - // }); |
172 | | - // }) |
173 | | - // .slice( |
174 | | - // 0, |
175 | | - // $targetSearchInput.data('offline-search-max-results') |
176 | | - // ); |
177 | | - |
178 | | - // |
179 | | - // Make result html |
180 | | - // |
181 | | - |
182 | | - |
183 | 146 | }; |
184 | 147 |
|
185 | 148 | // |
|
0 commit comments