forked from github/opensource.guide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_worker.js
37 lines (32 loc) · 951 Bytes
/
search_worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
---
layout:
---
// http://jekyll.tips/jekyll-casts/jekyll-search-using-lunr-js/
importScripts("{{ "/js/lunr.min.js" | relative_url }}");
var store = {
{% for article in site.articles %}
{% capture html %}{% include search-result.html article=article %}{% endcapture %}
"{{ article.url | xml_escape }}": {
"title": "{{ article.title | xml_escape }}",
"content": {{ article.content | markdownify | strip_html | strip_newlines | jsonify }},
"url": "{{ article.url | xml_escape }}",
"html": {{ html | jsonify }}
}{% unless forloop.last %},{% endunless %}
{% endfor %}
};
// Initialize lunr
var idx = lunr(function () {
this.ref('url');
this.field('title', { boost: 10 });
this.field('content');
});
// Add content to index
for(var id in store) {
idx.add(store[id]);
}
onmessage = function (e) {
var results = idx.search(e.data).map(function(result) {
return store[result.ref].html;
});
postMessage(results);
}