|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 |
|
4 | 4 | import lunr from 'lunr' |
| 5 | +import { get, set, createStore } from 'idb-keyval' |
| 6 | +import { DocfxOptions } from './options' |
5 | 7 |
|
6 | | -let lunrIndex |
7 | | - |
8 | | -let stopWords = null |
9 | | -let searchData = {} |
10 | | - |
11 | | -lunr.tokenizer.separator = /[\s\-.()]+/ |
12 | | - |
13 | | -const stopWordsRequest = new XMLHttpRequest() |
14 | | -stopWordsRequest.open('GET', '../search-stopwords.json') |
15 | | -stopWordsRequest.onload = function() { |
16 | | - if (this.status !== 200) { |
17 | | - return |
18 | | - } |
19 | | - stopWords = JSON.parse(this.responseText) |
20 | | - buildIndex() |
| 8 | +type SearchHit = { |
| 9 | + href: string |
| 10 | + title: string |
| 11 | + keywords: string |
21 | 12 | } |
22 | | -stopWordsRequest.send() |
23 | | - |
24 | | -const searchDataRequest = new XMLHttpRequest() |
25 | | - |
26 | | -searchDataRequest.open('GET', '../index.json') |
27 | | -searchDataRequest.onload = function() { |
28 | | - if (this.status !== 200) { |
29 | | - return |
30 | | - } |
31 | | - searchData = JSON.parse(this.responseText) |
32 | 13 |
|
33 | | - buildIndex() |
| 14 | +let search: (q: string) => SearchHit[] |
34 | 15 |
|
| 16 | +async function loadIndex() { |
| 17 | + const { index, data } = await loadIndexCore() |
| 18 | + search = q => index.search(q).map(({ ref }) => data[ref]) |
35 | 19 | postMessage({ e: 'index-ready' }) |
36 | 20 | } |
37 | | -searchDataRequest.send() |
38 | 21 |
|
39 | | -onmessage = function(oEvent) { |
40 | | - const q = oEvent.data.q |
41 | | - const results = [] |
42 | | - if (lunrIndex) { |
43 | | - const hits = lunrIndex.search(q) |
44 | | - hits.forEach(function(hit) { |
45 | | - const item = searchData[hit.ref] |
46 | | - results.push({ href: item.href, title: item.title, keywords: item.keywords }) |
47 | | - }) |
| 22 | +async function loadIndexCore() { |
| 23 | + const res = await fetch('../index.json') |
| 24 | + const etag = res.headers.get('etag') |
| 25 | + const data = await res.json() as { [key: string]: SearchHit } |
| 26 | + const cache = createStore('docfx', 'lunr') |
| 27 | + |
| 28 | + if (etag) { |
| 29 | + const value = JSON.parse(await get('index', cache) || '{}') |
| 30 | + if (value && value.etag === etag) { |
| 31 | + return { index: lunr.Index.load(value), data } |
| 32 | + } |
48 | 33 | } |
49 | | - postMessage({ e: 'query-ready', q, d: results }) |
50 | | -} |
51 | 34 |
|
52 | | -function buildIndex() { |
53 | | - if (stopWords !== null && !isEmpty(searchData)) { |
54 | | - lunrIndex = lunr(function() { |
55 | | - this.pipeline.remove(lunr.stopWordFilter) |
56 | | - this.ref('href') |
57 | | - this.field('title', { boost: 50 }) |
58 | | - this.field('keywords', { boost: 20 }) |
| 35 | + const main = await import('./main.js') |
| 36 | + const docfx = main.default as DocfxOptions |
| 37 | + |
| 38 | + const index = lunr(function() { |
| 39 | + this.pipeline.remove(lunr.stopWordFilter) |
| 40 | + this.ref('href') |
| 41 | + this.field('title', { boost: 50 }) |
| 42 | + this.field('keywords', { boost: 20 }) |
59 | 43 |
|
60 | | - for (const prop in searchData) { |
61 | | - if (Object.prototype.hasOwnProperty.call(searchData, prop)) { |
62 | | - this.add(searchData[prop]) |
63 | | - } |
64 | | - } |
| 44 | + lunr.tokenizer.separator = /[\s\-.()]+/ |
| 45 | + docfx.configureLunr?.(this) |
65 | 46 |
|
66 | | - const docfxStopWordFilter = lunr.generateStopWordFilter(stopWords) |
67 | | - lunr.Pipeline.registerFunction(docfxStopWordFilter, 'docfxStopWordFilter') |
68 | | - this.pipeline.add(docfxStopWordFilter) |
69 | | - this.searchPipeline.add(docfxStopWordFilter) |
70 | | - }) |
| 47 | + for (const key in data) { |
| 48 | + this.add(data[key]) |
| 49 | + } |
| 50 | + }) |
| 51 | + |
| 52 | + if (etag) { |
| 53 | + await set('index', JSON.stringify(Object.assign(index.toJSON(), { etag })), cache) |
71 | 54 | } |
| 55 | + |
| 56 | + return { index, data } |
72 | 57 | } |
73 | 58 |
|
74 | | -function isEmpty(obj) { |
75 | | - if (!obj) return true |
| 59 | +loadIndex().catch(console.error) |
76 | 60 |
|
77 | | - for (const prop in obj) { |
78 | | - if (Object.prototype.hasOwnProperty.call(obj, prop)) { return false } |
| 61 | +onmessage = function(e) { |
| 62 | + if (search) { |
| 63 | + postMessage({ e: 'query-ready', d: search(e.data.q) }) |
79 | 64 | } |
80 | | - |
81 | | - return true |
82 | 65 | } |
0 commit comments