Skip to content

Commit f065800

Browse files
author
Hubot
committed
Sync changes from upstream repository
1 parent 1d3fed5 commit f065800

File tree

14 files changed

+2123
-450
lines changed

14 files changed

+2123
-450
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@
77
/vendor/bundle/
88
/node_modules/
99

10+
# bower brings in *a lot* of files
11+
/assets/vendor/lunr.js/*
12+
!/assets/vendor/lunr.js/lunr.js
13+
!/assets/vendor/lunr.js/lunr.min.js
14+
1015
output
1116
tmp
1217
.DS_Store
1318
.bundle
1419
crash.log
1520
npm-debug.log
16-
static/search-index.json

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ ruby '2.2.3'
33

44
gem 'nanoc', '~> 4.0'
55
gem 'nanoc-conref-fs', '~> 0.5'
6-
gem 'nokogiri', '~> 1.6.0'
76

87
# rendering
98
gem 'nanoc-html-pipeline', '0.3.3'
@@ -17,6 +16,7 @@ gem 'page-toc-filter', '~> 0.0.1'
1716
gem 'builder', '~> 3.2'
1817

1918
group :development do
19+
gem 'nokogiri', '~> 1.6.0'
2020
gem 'rake', '10.3.2'
2121
gem 'awesome_print', '1.6.1'
2222
end

Rules

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
# * The order of rules is important: for each item, only the first matching
66
# rule is applied.
77

8-
# Reset search-index by deleting it every time
98
preprocess do
10-
File.delete("output/search-index.json") if File.exists?("output/search-index.json")
119
create_individual_blog_pages
1210
generate_redirects(config[:redirects])
1311
end
@@ -25,7 +23,6 @@ compile '/integrations-directory/*' do
2523
end
2624

2725
compile '/v3{.*,/**/*}' do
28-
filter :search
2926
filter :erb
3027
filter :html_pipeline, @config[:pipeline_config]
3128
filter :enterprise_only_filter
@@ -52,6 +49,10 @@ compile '/webhooks/**/*' do
5249
layout(item[:layout] ? "/#{item[:layout]}.*" : '/webhooks.*')
5350
end
5451

52+
compile '/search/search-index.json' do
53+
filter :erb
54+
end
55+
5556
compile '/**/*' do
5657
filter :erb
5758
filter :html_pipeline, @config[:pipeline_config]
@@ -62,6 +63,10 @@ route '/changes.atom' do
6263
'/changes.atom'
6364
end
6465

66+
route '/search/search-index.json' do
67+
item.identifier.to_s
68+
end
69+
6570
route '/**/index.*' do
6671
item.identifier.without_ext + '.html'
6772
end

assets/javascripts/search.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
$(function() {
22
var searchIndex,
3-
searchHits;
3+
searchHits,
4+
searchWorker = new Worker("/assets/javascripts/search_worker.js");
45

56
$('.help-search .search-box').focus(function(){
67
$(this).css('background-position','0px -25px')
@@ -17,15 +18,22 @@ $(function() {
1718
if (localStorage['searchIndex']) {
1819
searchIndex = JSON.parse(localStorage['searchIndex']);
1920

20-
if (localStorageHasExpired())
21+
if (localStorageHasExpired()) {
2122
loadSearchIndex();
23+
}
24+
else {
25+
searchIndex.type = "index";
26+
searchWorker.postMessage(searchIndex);
27+
}
2228
} else {
2329
loadSearchIndex();
2430
}
2531

2632
function loadSearchIndex() {
27-
$.getJSON('/assets/search-index.json', function(data) {
28-
searchIndex = data["pages"];
33+
$.getJSON('/search/search-index.json', function(data) {
34+
data.type = { index: true };
35+
searchWorker.postMessage(data);
36+
searchIndex = data;
2937
localStorage['searchIndex'] = JSON.stringify(searchIndex);
3038
localStorage['updated'] = new Date().getTime();
3139
});
@@ -118,22 +126,17 @@ $(function() {
118126
function searchForString(searchString) {
119127
searchHits = [];
120128
searchString = searchString.toLowerCase();
129+
searchWorker.postMessage({ query: searchString, type: "search" })
130+
}
121131

122-
// Search for string in all pages
123-
for (var i = 0; i < searchIndex.length; i++) {
124-
var page = searchIndex[i];
125-
126-
// Add the page to the array of hits if there's a match
127-
if (page.title.toLowerCase().indexOf(searchString) !== -1) {
128-
searchHits.push(page);
129-
}
132+
searchWorker.addEventListener("message", function (e) {
133+
if (e.data.type.search) {
134+
renderResultsForSearch(e.data.query, e.data.results);
130135
}
131-
132-
renderResultsForSearch(searchString);
133-
}
136+
});
134137

135138
// Update the UI representation of the search hits
136-
function renderResultsForSearch(searchString){
139+
function renderResultsForSearch(searchString, searchHits){
137140
$("#search-results").empty();
138141

139142
// Check if there are any results. If not, show placeholder and exit
@@ -146,7 +149,7 @@ $(function() {
146149
for (var i = 0; i < Math.min(searchHits.length, 8); i++) {
147150
var page = searchHits[i];
148151

149-
$('<li class="result"><a href="' + page.url + '"><em>' + page.title + '</em><small>' + page.section + '</small></a></li>').appendTo("#search-results");
152+
$('<li class="result"><a href="' + page.url + '"><em>' + page.title + '</em></a></li>').appendTo("#search-results");
150153
}
151154

152155
// Select the first alternative
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
importScripts('lunr.min.js');
2+
3+
// create lunr.js search index specifying that we want to index the title and body fields of documents.
4+
var lunr_index = lunr(function() {
5+
this.field('title', { boost: 10 });
6+
this.field('body');
7+
this.ref('id');
8+
}),
9+
entries;
10+
11+
onmessage = function (oEvent) {
12+
13+
populateIndex = function(data) {
14+
// format the raw json into a form that is simpler to work with
15+
this.entries = data.map(this.createEntry).filter(function(n){ return n !== undefined });
16+
this.entries.forEach(function(entry) {
17+
if (entry !== null)
18+
this.lunr_index.add(entry);
19+
});
20+
21+
postMessage({type: {indexed: true}});
22+
};
23+
24+
decodeHtmlEntity = function(str) {
25+
return str.replace(/&#(\d+);/g, function(match, dec) {
26+
return String.fromCharCode(dec);
27+
});
28+
};
29+
30+
createEntry = function(entry, entry_id) {
31+
if (entry.title === undefined)
32+
return undefined;
33+
entry.id = entry_id + 1;
34+
entry.title = decodeHtmlEntity(entry.title);
35+
return entry;
36+
};
37+
38+
search = function(data) {
39+
var entries = this.entries;
40+
41+
var results = lunr_index
42+
.search(data.query)
43+
.map(function(result) {
44+
return entries.filter(function(entry) { return entry.id === parseInt(result.ref, 10); })[0];
45+
})
46+
.filter(function (result) {
47+
return typeof result !== 'undefined';
48+
});
49+
50+
postMessage({ query: data.query, results: results, type: { search: true } });
51+
}
52+
53+
// if we're asked to index, index! else, search
54+
if (oEvent.data.type == "index")
55+
populateIndex(oEvent.data);
56+
else
57+
search(oEvent.data);
58+
};

0 commit comments

Comments
 (0)