forked from tableau/document-api-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.js
More file actions
71 lines (58 loc) · 2.24 KB
/
search.js
File metadata and controls
71 lines (58 loc) · 2.24 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
(function() {
// Use query param as search query
var searchQuery = decodeURIComponent(window.location.href.split("?q=")[1]);
// Initialize the search index
var lunrIndex = lunr(function () {
this.ref('url');
this.field('title', {boost: 10});
this.field('content');
});
// Add content from search blob to index
function addContentToIndex() {
for (var page in search_blob) {
lunrIndex.add({
url: page,
title: search_blob[page].title,
content: search_blob[page].content
});
}
}
function displaySearchHeading(query) {
var heading = document.getElementById("searchHeading");
heading.innerHTML = "Search results for: " + query;
}
// Get the raw search results
function getRawSearchResults(query) {
return lunrIndex.search(query);
}
// Try to find the end of a word on which to end the blurb
function getResultBlurb(result_content) {
var rangeForEndOfWord = result_content.substr(250, 275),
endOfWordIndex = rangeForEndOfWord.indexOf(" ");
return result_content.substr(0, 250 + endOfWordIndex);
}
function displaySearchResults(results) {
var container = document.getElementById("searchResultsContainer"),
ref, title;
// Clear the loading text before inserting the results
container.innerHTML = "";
if (results.length) {
for (var result in results) {
ref = results[result].ref;
if (ref !== "") {
// Some pages might not have a title set
title = search_blob[ref].title || '(No title)';
container.innerHTML += "<a href='" + ref + "'>" + title + "</a>";
container.innerHTML += "<p>" + getResultBlurb(search_blob[ref].content) + "...</p><br />";
}
}
} else {
container.innerHTML += "<div class='search-result'>No results found.</div>";
}
}
addContentToIndex();
window.addEventListener("load", function () {
displaySearchHeading(searchQuery);
displaySearchResults(getRawSearchResults(searchQuery));
});
})();