Commit 18cd1048 authored by Robert Lord's avatar Robert Lord

Add better search results in a sliding search results area

parent 51e537ac
...@@ -15,9 +15,9 @@ ...@@ -15,9 +15,9 @@
$(bind); $(bind);
function populate() { function populate() {
$('h1').each(function() { $('h1, h2').each(function() {
var title = $(this); var title = $(this);
var body = title.nextUntil('h1'); var body = title.nextUntil('h1, h2');
index.add({ index.add({
id: title.prop('id'), id: title.prop('id'),
title: title.text(), title: title.text(),
...@@ -31,13 +31,7 @@ ...@@ -31,13 +31,7 @@
darkBox = $('.dark-box'); darkBox = $('.dark-box');
searchResults = $('.search-results'); searchResults = $('.search-results');
$('#input-search').on('keyup', function(e) { $('#input-search').on('keyup', search);
if ($(this).val() === "") {
inactive(e);
} else {
search(e);
}
});
} }
function search(event) { function search(event) {
...@@ -48,24 +42,24 @@ ...@@ -48,24 +42,24 @@
if (event.keyCode === 27) this.value = ''; if (event.keyCode === 27) this.value = '';
if (this.value) { if (this.value) {
var results = index.search(this.value); var results = index.search(this.value).filter(function(r) {
return r.score > 0.0001;
});
if (results.length) { if (results.length) {
searchResults.empty();
$.each(results, function (index, result) { $.each(results, function (index, result) {
var header = $('.tocify-item[data-unique=' + result.ref + ']').closest('.tocify-header'); searchResults.append("<li><a href='#" + result.ref + "'>" + $('#'+result.ref).text() + "</a></li>");
if (header.length > 0) header = header[0];
if (header) $("#" + header.id + " li a").append("<span>" + result.score + "</span>");
}); });
highlight.call(this); highlight.call(this);
} else { } else {
searchResults.text('No Results Found for "' + this.value + '"').show(); searchResults.html('<li>No Results Found for "' + this.value + '"</li>');
} }
} } else {
}
function inactive() {
unhighlight(); unhighlight();
searchResults.removeClass('visible'); searchResults.removeClass('visible');
} }
}
function highlight() { function highlight() {
if (this.value) content.highlight(this.value, highlightOpts); if (this.value) content.highlight(this.value, highlightOpts);
......
...@@ -64,7 +64,10 @@ under the License. ...@@ -64,7 +64,10 @@ under the License.
<div class="search"> <div class="search">
<input type="text" class="search" id="input-search" placeholder="Search"> <input type="text" class="search" id="input-search" placeholder="Search">
</div> </div>
<div class="search-results"></div> <ul class="search-results">
<li><a href="#">Introduction</a></li>
<li><a href="#">Cats</a></li>
</ul>
<% end %> <% end %>
<div id="toc"> <div id="toc">
</div> </div>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment