-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
153 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
--- | ||
title: Lunr index | ||
permalink: /scripts/lunr_docs.json | ||
--- | ||
{% assign idx = 0 %} | ||
{% assign post_count = collections.posts.pages | size %} | ||
[ | ||
{% for post in collections.posts.pages %} | ||
{% assign idx = idx | plus: 1 %} | ||
{ | ||
"title" : "{{ post.title }}", | ||
|
||
"href" : "{{ post.permalink }}", | ||
"content" : "{{ post.content | strip_html | strip_newlines | replace: "\", "\\" }}" | ||
}{% if idx < post_count %},{% endif %} | ||
{% endfor %} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
--- | ||
layout: default.liquid | ||
title: Search | ||
permalink: /search | ||
--- | ||
|
||
<search> | ||
<h1>Search</h1> | ||
<input id="search" type="text" size="25" placeholder="search for stuff here..." autofocus><br /> | ||
|
||
<h2>Search results</h2> | ||
|
||
<ul id="results"> | ||
</ul> | ||
|
||
<script type="text/javascript" src="https://code.jquery.com/jquery-2.1.3.min.js"></script> | ||
<script type="text/javascript" src="https://unpkg.com/lunr/lunr.js"></script> | ||
<script type="text/javascript"> | ||
var lunrIndex, | ||
$results, | ||
pagesIndex; | ||
// This is pretty much a copy of | ||
// https://gist.github.com/sebz/efddfc8fdcb6b480f567 | ||
// Initialize lunrjs using our generated index file | ||
function initLunr() { | ||
// First retrieve the index file | ||
$.getJSON("/scripts/lunr_docs.json") | ||
.done(function(index) { | ||
pagesIndex = index; | ||
// Set up lunrjs by declaring the fields we use | ||
// Also provide their boost level for the ranking | ||
lunrIndex = lunr(function() { | ||
this.field("title", { | ||
boost: 10 | ||
}); | ||
this.field("content"); | ||
// ref is the result item identifier (I chose the page URL) | ||
this.ref("href"); | ||
// Feed lunr with each file and let lunr actually index them | ||
pagesIndex.forEach(function(page) { | ||
this.add(page) | ||
}, this); | ||
}); | ||
}) | ||
.fail(function(jqxhr, textStatus, error) { | ||
var err = textStatus + ", " + error; | ||
console.error("Error getting cobalt index file:", err); | ||
}); | ||
} | ||
// Nothing crazy here, just hook up a listener on the input field | ||
function initUI() { | ||
$results = $("#results"); | ||
$("#search").keyup(function() { | ||
$results.empty(); | ||
// Only trigger a search when 2 chars. at least have been provided | ||
var query = $(this).val(); | ||
if (query.length < 2) { | ||
return; | ||
} | ||
var results = search(query); | ||
renderResults(results); | ||
}); | ||
} | ||
function search(query) { | ||
// Find the item in our index corresponding to the lunr one to have more | ||
// info | ||
// Lunr result: | ||
// {ref: "/section/page1", score: 0.2725657778206127} | ||
// Our result: | ||
// {title:"Page1", href:"/section/page1", ...} | ||
return lunrIndex.search(query).map(function(result) { | ||
return pagesIndex.filter(function(page) { | ||
return page.href === result.ref; | ||
})[0]; | ||
}); | ||
} | ||
function renderResults(results) { | ||
if (!results.length) { | ||
return; | ||
} | ||
results.forEach(function(result) { | ||
var $result = $("<li style=\"list-style:none;\">"); // FUUUUUU! | ||
$result.append($("<a>", { | ||
href: "/"+result.href, | ||
text: "» " + result.title | ||
})); | ||
$results.append($result); | ||
}); | ||
} | ||
// Let's get started | ||
initLunr(); | ||
$(document).ready(function() { | ||
initUI(); | ||
}); | ||
</script> | ||
</search> |