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.

80 lines
2.0 KiB

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