Skip to content

Commit 0741722

Browse files
committed
Merge pull request #469 from algolia/master
Add Algolia search on header bar
2 parents 50816a8 + d0d18a5 commit 0741722

File tree

6 files changed

+321
-150
lines changed

6 files changed

+321
-150
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ _site
44
.settings
55
*~
66
vendor/bundle
7+
.idea/

_includes/footerbar.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,9 @@
3434
</p>
3535
</div>
3636
</div>
37+
38+
<!-- search -->
39+
<script src="//cdn.jsdelivr.net/algoliasearch/3/algoliasearch.min.js"></script>
40+
<script src="//cdn.jsdelivr.net/autocomplete.js/0/autocomplete.min.js"></script>
41+
<script src="{{ site.baseurl }}/resources/javascript/searchbar.js" type="text/javascript" ></script>
42+

_includes/header.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,4 @@
153153
</style>
154154

155155
</head>
156-
<body onload="styleCode()">
156+
<body onload="styleCode()">

_includes/topbar.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@
3636

3737
<li><a href="{{ site.baseurl }}/contribute.html">Contribute</a></li>
3838
<li><a href="{{ site.baseurl }}/sips">SIPs/SLIPs</a></li>
39-
</ul>
40-
<form method="get" id="searchform" action="{{ site.baseurl }}/search.html">
41-
<input type="text" placeholder="Search" class="field" name="q" id="q"/>
42-
</form>
43-
</li>
44-
</ul>
39+
<li>
40+
<form id="searchform" action="#">
41+
<input type="text" placeholder="Search in documentation..." class="field" name="q" id="q"/>
42+
</form>
43+
</li>
44+
</ul>
4545
</div>
4646
</div>
4747
</div>

resources/javascript/searchbar.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
(function($) {
2+
var template =
3+
'<div class="aa-dataset-a">' +
4+
'</div>' +
5+
'<div class="branding-footer">' +
6+
'<div class="branding"><span>Powered by</span><a target="_blank" href="https://www.algolia.com/?utm_source=scaladocs"><img src="https://www.algolia.com/assets/algolia128x40.png"/></a></div>' +
7+
'</div>';
8+
9+
var client = algoliasearch('BH4D9OD16A', '3403ae7bb4cb839fba71e2fae9ab1534');
10+
var index = client.initIndex('scaladocs');
11+
var ATTRIBUTES = ['content', 'h6', 'h5', 'h4', 'h3', 'h2', 'h1' ];
12+
13+
var capitalize = function(string) {
14+
return string.charAt(0).toUpperCase() + string.slice(1);
15+
};
16+
17+
var hitsSource = autocomplete.sources.hits(index, { hitsPerPage: 7, tagFilters: [ 'en' ], numericFilters: 'importance>0' });
18+
19+
autocomplete('#q', {
20+
hint: false,
21+
debug: false,
22+
templates: {
23+
dropdownMenu: template
24+
}
25+
}, [
26+
{
27+
source: function(query, callback) {
28+
hitsSource(query, function(suggestions) {
29+
var categories = {};
30+
suggestions.forEach(function(suggestion) {
31+
var enIndex = suggestion._tags.indexOf('en');
32+
if(enIndex > -1) {
33+
suggestion._tags.splice(enIndex, 1);
34+
}
35+
categories[suggestion._tags[0]] = categories[suggestion._tags[0]] || []
36+
categories[suggestion._tags[0]].push(suggestion)
37+
});
38+
39+
categories = $.map(Object.keys(categories).sort(), function(categoryName) {
40+
var items = categories[categoryName];
41+
items[0].isCategoryHeader = true;
42+
items[0].categoryName = capitalize(categoryName);
43+
44+
return items;
45+
});
46+
callback(categories);
47+
});
48+
},
49+
name: 'a',
50+
displayKey: 'title',
51+
templates: {
52+
suggestion: function(suggestion) {
53+
var html = [];
54+
if(suggestion.isCategoryHeader) {
55+
html.push('<div class="suggestion-category">' + suggestion.categoryName + '</div>');
56+
}
57+
58+
var highlight = suggestion._highlightResult || {};
59+
var snippet = suggestion._snippetResult || {};
60+
var title = highlight.title.value;
61+
var text = '';
62+
for(var i = 0 ; i < ATTRIBUTES.length ; i++) {
63+
if(highlight[ATTRIBUTES[i]] !== undefined) {
64+
text = highlight[ATTRIBUTES[i]].value;
65+
text = (snippet[ATTRIBUTES[i]] || {}).value || text;
66+
break;
67+
}
68+
}
69+
70+
html.push(' <div class="suggestion-content">');
71+
html.push(' <div class="suggestion-title">' + title + '</div>');
72+
html.push(' <div class="suggestion-text">' + text + '</div>');
73+
html.push(' </div>');
74+
return html.join(' ');
75+
}
76+
}
77+
}
78+
]).on('autocomplete:selected', function(event, suggestion) {
79+
window.location.href = suggestion.link;
80+
});
81+
})(jQuery);

0 commit comments

Comments
 (0)