Skip to content

Commit 16fc4b8

Browse files
Otto Chronsochrons
Otto Chrons
authored andcommitted
Implemented local search with lunr.js
JS loading is deferred until after page render. Note: .json compression must be enabled before merging this due to the large size of the search index.md
1 parent c6f057d commit 16fc4b8

File tree

12 files changed

+2190
-29
lines changed

12 files changed

+2190
-29
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
language: ruby
22
rvm: 2.2.3
33
install:
4-
- gem install -N jekyll:3.0.1 jekyll-paginate:1.1.0 jekyll-assets:2.1.2 sass:3.4.20
4+
- gem install -N jekyll:3.0.1 jekyll-paginate:1.1.0 jekyll-assets:2.1.2 sass:3.4.20 execjs:2.6.0 uglifier:2.7.2
55
script:
66
- jekyll build
77
after_success:

_assets/css/style.scss

+34-6
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ a:focus {
182182
color: $primary-color;
183183
}
184184

185+
.navbar-default .navbar-nav > li > a:hover, .navbar-default .navbar-nav > li > a:focus,
185186
.navbar-default .navbar-nav > .active > a, .navbar-default .navbar-nav > .active > a:hover, .navbar-default .navbar-nav > .active > a:focus {
186187
color: $primary-color;
187188
background-color: transparent;
@@ -272,7 +273,7 @@ a:focus {
272273

273274
.hline-w {
274275
border-bottom: 2px solid #ffffff;
275-
margin-bottom: 25px;
276+
margin-bottom: 12px;
276277
}
277278

278279
#headerwrap {
@@ -438,13 +439,11 @@ a:focus {
438439
}
439440

440441
#footerwrap i {
441-
font-size: 30px;
442-
color: $footer-content;
443-
padding-right: 25px;
442+
padding-left: 5px;
444443
}
445444

446-
#footerwrap i:hover {
447-
color: $primary-color;
445+
#footerwrap a:hover {
446+
color: $primary-color !important;
448447
}
449448

450449
#blue {
@@ -715,3 +714,32 @@ a[name]:before {
715714
margin-top: 10px;
716715
margin-bottom: 10px !important;
717716
}
717+
718+
#search-view {
719+
position: relative;
720+
}
721+
722+
#search-box {
723+
display: none;
724+
width: 300px;
725+
height: 420px;
726+
position: absolute;
727+
padding: 10px 10px;
728+
top: 50px;
729+
left: auto;
730+
right: 30px;
731+
background: white;
732+
z-index: 100;
733+
box-shadow: 0px 10px 31px -15px rgba(0,0,0,1);
734+
}
735+
736+
@media (max-width: 767px) {
737+
#search-box {
738+
position: relative;
739+
width: 100%;
740+
height: 100%;
741+
top: 0;
742+
left: 0;
743+
right: auto;
744+
}
745+
}

_assets/js/bundle.js

+1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
//= require jquery.min
33
//= require bootstrap.min
44
//= require highlight.pack
5+
//= require lunr
56
//= require custom

_assets/js/custom.js

+88-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,100 @@
11
// any link that is not part of the current domain is modified
22

3-
(function() {
3+
(function () {
44
var links = document.links;
55
for (var i = 0; i < links.length; i++) {
6-
if (links[i].hostname != window.location.hostname && links[i].target == "" ) {
6+
if (links[i].hostname != window.location.hostname && links[i].target == "") {
77
links[i].target = '_blank';
88
}
99
}
1010
})();
1111

12+
$(document).ready(function () {
13+
var index, store;
14+
15+
$('pre code').each(function(i, block) {
16+
hljs.highlightBlock(block);
17+
});
18+
19+
$(".show-snippet-link").click(function (e) {
20+
e.preventDefault();
21+
var language = $(this).data("language");
22+
$(".snippet").hide();
23+
$(".snippet-" + language).show();
24+
$(".show-snippet-tab").removeClass("active");
25+
$(this.parentNode).addClass("active");
26+
});
27+
28+
function switchSearchBox() {
29+
if ($("#search-box").is(":visible")) {
30+
$("#search-box").fadeOut('fast');
31+
} else {
32+
$("#search-box").fadeIn('fast');
33+
$("#search-term").val("").focus();
34+
$("#search-results").empty().hide();
35+
}
36+
}
37+
38+
// show search view when icon is clicked
39+
$("#search-icon").on("click", function (e) {
40+
e.stopPropagation();
41+
$(this).blur();
42+
switchSearchBox();
43+
});
44+
45+
$("#search-term").on('keyup', function (e) {
46+
if (e.keyCode == 27) {
47+
switchSearchBox();
48+
return;
49+
}
50+
if (index) {
51+
var query = this.value;
52+
var result = index.search(query);
53+
var resultDiv = $("#search-results");
54+
resultDiv.empty();
55+
if (result.length == 0) {
56+
resultDiv.append("<p class='csmall'>No results</p>");
57+
} else {
58+
resultDiv.append("<p class='csmall'>Results</p>");
59+
for (var i in result.slice(0, 9)) {
60+
var ref = result[i].ref;
61+
var searchItem = "<p><a href='" + store[ref].href + "'>" + store[ref].title + "</a></p>";
62+
resultDiv.append(searchItem);
63+
}
64+
resultDiv.show();
65+
}
66+
}
67+
});
68+
69+
// hide search-box when clicked outside it
70+
$(document).click(function (event) {
71+
if (!$(event.target).closest('#search-box').length) {
72+
if ($("#search-box").is(":visible")) {
73+
$("#search-box").fadeOut('fast');
74+
}
75+
}
76+
});
77+
78+
$(".navbar-toggle").on("click", function (e) {
79+
// check if menu is collapsed and hide search window as well
80+
var btn = $(this);
81+
window.setTimeout(function () {
82+
// delayed execution to allow Bootstrap code to do its thing first
83+
if (btn.hasClass("collapsed")) {
84+
$("#search-box").fadeOut();
85+
}
86+
}, 0);
87+
});
88+
89+
// load search index
90+
$.getJSON('/search_data.json', function (response) {
91+
// Create index
92+
index = lunr.Index.load(response.index);
93+
// Create store
94+
store = response.store;
95+
});
96+
});
97+
1298
hljs.initHighlightingOnLoad();
1399

14100
$(document).ready(function () {

0 commit comments

Comments
 (0)