Skip to content

Add Algolia search on header bar #469

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ _site
.settings
*~
vendor/bundle
.idea/
6 changes: 6 additions & 0 deletions _includes/footerbar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,9 @@
</p>
</div>
</div>

<!-- search -->
<script src="//cdn.jsdelivr.net/algoliasearch/3/algoliasearch.min.js"></script>
<script src="//cdn.jsdelivr.net/autocomplete.js/0/autocomplete.min.js"></script>
<script src="{{ site.baseurl }}/resources/javascript/searchbar.js" type="text/javascript" ></script>

2 changes: 1 addition & 1 deletion _includes/header.txt
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,4 @@
</style>

</head>
<body onload="styleCode()">
<body onload="styleCode()">
12 changes: 6 additions & 6 deletions _includes/topbar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@

<li><a href="{{ site.baseurl }}/contribute.html">Contribute</a></li>
<li><a href="{{ site.baseurl }}/sips">SIPs/SLIPs</a></li>
</ul>
<form method="get" id="searchform" action="{{ site.baseurl }}/search.html">
<input type="text" placeholder="Search" class="field" name="q" id="q"/>
</form>
</li>
</ul>
<li>
<form id="searchform" action="#">
<input type="text" placeholder="Search in documentation..." class="field" name="q" id="q"/>
</form>
</li>
</ul>
</div>
</div>
</div>
81 changes: 81 additions & 0 deletions resources/javascript/searchbar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
(function($) {
var template =
'<div class="aa-dataset-a">' +
'</div>' +
'<div class="branding-footer">' +
'<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>' +
'</div>';

var client = algoliasearch('BH4D9OD16A', '3403ae7bb4cb839fba71e2fae9ab1534');
var index = client.initIndex('scaladocs');
var ATTRIBUTES = ['content', 'h6', 'h5', 'h4', 'h3', 'h2', 'h1' ];

var capitalize = function(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
};

var hitsSource = autocomplete.sources.hits(index, { hitsPerPage: 7, tagFilters: [ 'en' ], numericFilters: 'importance>0' });

autocomplete('#q', {
hint: false,
debug: false,
templates: {
dropdownMenu: template
}
}, [
{
source: function(query, callback) {
hitsSource(query, function(suggestions) {
var categories = {};
suggestions.forEach(function(suggestion) {
var enIndex = suggestion._tags.indexOf('en');
if(enIndex > -1) {
suggestion._tags.splice(enIndex, 1);
}
categories[suggestion._tags[0]] = categories[suggestion._tags[0]] || []
categories[suggestion._tags[0]].push(suggestion)
});

categories = $.map(Object.keys(categories).sort(), function(categoryName) {
var items = categories[categoryName];
items[0].isCategoryHeader = true;
items[0].categoryName = capitalize(categoryName);

return items;
});
callback(categories);
});
},
name: 'a',
displayKey: 'title',
templates: {
suggestion: function(suggestion) {
var html = [];
if(suggestion.isCategoryHeader) {
html.push('<div class="suggestion-category">' + suggestion.categoryName + '</div>');
}

var highlight = suggestion._highlightResult || {};
var snippet = suggestion._snippetResult || {};
var title = highlight.title.value;
var text = '';
for(var i = 0 ; i < ATTRIBUTES.length ; i++) {
if(highlight[ATTRIBUTES[i]] !== undefined) {
text = highlight[ATTRIBUTES[i]].value;
text = (snippet[ATTRIBUTES[i]] || {}).value || text;
break;
}
}

html.push(' <div class="suggestion-content">');
html.push(' <div class="suggestion-title">' + title + '</div>');
html.push(' <div class="suggestion-text">' + text + '</div>');
html.push(' </div>');
return html.join(' ');
}
}
}
]).on('autocomplete:selected', function(event, suggestion) {
window.location.href = suggestion.link;
});
})(jQuery);
Loading