You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

search-worker.js 1.6 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. (function () {
  2. importScripts('lunr.min.js');
  3. var lunrIndex = lunr(function () {
  4. this.pipeline.remove(lunr.stopWordFilter);
  5. this.ref('href');
  6. this.field('title', { boost: 50 });
  7. this.field('keywords', { boost: 20 });
  8. });
  9. lunr.tokenizer.seperator = /[\s\-\.]+/;
  10. var stopWordsRequest = new XMLHttpRequest();
  11. stopWordsRequest.open('GET', '../search-stopwords.json');
  12. stopWordsRequest.onload = function () {
  13. if (this.status != 200) {
  14. return;
  15. }
  16. var stopWords = JSON.parse(this.responseText);
  17. var docfxStopWordFilter = lunr.generateStopWordFilter(stopWords);
  18. lunr.Pipeline.registerFunction(docfxStopWordFilter, 'docfxStopWordFilter');
  19. lunrIndex.pipeline.add(docfxStopWordFilter);
  20. }
  21. stopWordsRequest.send();
  22. var searchData = {};
  23. var searchDataRequest = new XMLHttpRequest();
  24. searchDataRequest.open('GET', '../index.json');
  25. searchDataRequest.onload = function () {
  26. if (this.status != 200) {
  27. return;
  28. }
  29. searchData = JSON.parse(this.responseText);
  30. for (var prop in searchData) {
  31. if (searchData.hasOwnProperty(prop)) {
  32. lunrIndex.add(searchData[prop]);
  33. }
  34. }
  35. postMessage({ e: 'index-ready' });
  36. }
  37. searchDataRequest.send();
  38. onmessage = function (oEvent) {
  39. var q = oEvent.data.q;
  40. var hits = lunrIndex.search(q);
  41. var results = [];
  42. hits.forEach(function (hit) {
  43. var item = searchData[hit.ref];
  44. results.push({ 'href': item.href, 'title': item.title, 'keywords': item.keywords });
  45. });
  46. postMessage({ e: 'query-ready', q: q, d: results });
  47. }
  48. })();