Created
October 21, 2017 14:07
-
-
Save manbearwolf/073b7c47c96c38f38698e533176a1ee0 to your computer and use it in GitHub Desktop.
Search lunr taken from https://github.com/projectpages/project-pages
This file contains hidden or 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
| --- | |
| --- | |
| jQuery(function() { | |
| // Initialize lunr with the fields to be searched, plus the boost. | |
| window.idx = lunr(function () { | |
| this.field('id'); | |
| this.field('title'); | |
| this.field('content', { boost: 10 }); | |
| this.field('author'); | |
| this.field('categories'); | |
| }); | |
| // Get the generated search_data.json file so lunr.js can search it locally. | |
| window.data = $.getJSON('{{ site.baseurl }}/search/search_data.json'); | |
| // Wait for the data to load and add it to lunr | |
| window.data.then(function(loaded_data){ | |
| $.each(loaded_data, function(index, value){ | |
| window.idx.add( | |
| $.extend({ "id": index }, value) | |
| ); | |
| }); | |
| }); | |
| // Event when the form is submitted | |
| $("#site_search").submit(function(event){ | |
| event.preventDefault(); // RTH: per Google, preventDefault() might be the culprit in Firefox | |
| var query = $("#search_box").val(); // Get the value for the text field | |
| var results = window.idx.search(query); // Get lunr to perform a search | |
| display_search_results(results); // Hand the results off to be displayed | |
| }); | |
| function display_search_results(results) { | |
| var $search_results = $("#search_results"); | |
| // Wait for data to load | |
| window.data.then(function(loaded_data) { | |
| // Are there any results? | |
| if (results.length) { | |
| $search_results.empty(); // Clear any old results | |
| // Iterate over the results | |
| results.forEach(function(result) { | |
| var item = loaded_data[result.ref]; | |
| // Build a snippet of HTML for this result | |
| var appendString = '<li><a href="' + item.url + '">' + item.title + '</a></li>'; | |
| // Add the snippet to the collection of results. | |
| $search_results.append(appendString); | |
| }); | |
| } else { | |
| // If there are no results, let the user know. | |
| $search_results.html('<li>No results found.<br/>Please check spelling, spacing, yada...</li>'); | |
| } | |
| }); | |
| } | |
| }); |
This file contains hidden or 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
| --- | |
| layout: null | |
| --- | |
| { | |
| {% for post in site.posts %} | |
| "{{ post.url | slugify }}": { | |
| "title": "{{ post.title | xml_escape }}", | |
| "content" : "{{post.content | strip_html | strip_newlines | remove: " " | escape | remove: "\"}}", | |
| "url": "{{ site.baseurl }}{{ post.url | xml_escape }}", | |
| "author": "{{ post.author | xml_escape }}", | |
| "categories": "{% for category in post.categories %}{{ category }}{% unless forloop.last %}, {% endunless %}{% endfor %}" | |
| } | |
| {% unless forloop.last %},{% endunless %} | |
| {% endfor %} | |
| {% for page in site.pages %} | |
| {% if page.url == '/speaking/index.html' or page.url == '/if-rudyard-kipling/index.html' or page.url == '/about/index.html' or page.url == '/contact/index.html' or page.url == '/sudo-disclaimer/index.html' %} | |
| , | |
| "{{ page.url | slugify }}": { | |
| "title": "{{ page.title | xml_escape }}", | |
| "content" : "{{page.content | strip_html | strip_newlines | remove: " " | escape | remove: "\"}}", | |
| "url": "{{ site.baseurl }}{{ page.url | xml_escape }}" | |
| } | |
| {% endif %} | |
| {% endfor %} | |
| } |
Author
Author
<h3 class="no-anchor">Search</h3>
<form action="get" id="site_search" autocomplete="on">
<strong>By keyword or tag:</strong><br />
<input style="font-size:20px;" type="text" id="search_box">
<input style="font-size:20px;font-weight: bold;" type="submit" value="Search">
</form>
<br />
<ul id="search_results"></ul>
<-------------- somewhere in html page / post ------------------>
Author
this only runs with lunr version 0.7.0 as far as I know...
https://lunrjs.com/
Files are separate (I couldn't combine them inside the js folder in custom js, for some reason)...
Works! But is maybe Old...
Author
Updated... Works in Custom.js (search.js fits under doc-ready...)
$(document).ready(function(){
// Responsive video embeds
$('.entry-content').fitVids();
// Initialize lunr with the fields to be searched, plus the boost.
window.idx = lunr(function () {
this.field('id');
this.field('title');
this.field('content', { boost: 10 });
this.field('author');
this.field('categories');
});
// Get the generated search_data.json file so lunr.js can search it locally.
window.data = $.getJSON('/js/search_data.json');
// Wait for the data to load and add it to lunr
window.data.then(function(loaded_data){
$.each(loaded_data, function(index, value){
window.idx.add(
$.extend({ "id": index }, value)
);
});
});
// Event when the form is submitted
$("#site_search").submit(function(event){
event.preventDefault(); // RTH: per Google, preventDefault() might be the culprit in Firefox
var query = $("#search_box").val(); // Get the value for the text field
var results = window.idx.search(query); // Get lunr to perform a search
display_search_results(results); // Hand the results off to be displayed
});
function display_search_results(results) {
var $search_results = $("#search_results");
// Wait for data to load
window.data.then(function(loaded_data) {
// Are there any results?
if (results.length) {
$search_results.empty(); // Clear any old results
// Iterate over the results
results.forEach(function(result) {
var item = loaded_data[result.ref];
// Build a snippet of HTML for this result
var appendString = '<li><a href="' + item.url + '">' + item.title + '</a></li>';
// Add the snippet to the collection of results.
$search_results.append(appendString);
});
} else {
// If there are no results, let the user know.
$search_results.html('<li>No results found.<br/>Please check spelling, spacing, yada...</li>');
}
});
}
make sure {{ site.baseurl }} is gone... in window-data
Author
This code works up to version 1.0.0 Lunr. JS
http://rayhightower.com/blog/2016/01/04/how-to-make-lunrjs-jekyll-work-together/
Author
Want a Better Search? Take a look at this one... (It is Better than LunrJS!)...
https://github.com/christian-fei/Simple-Jekyll-Search
https://blog.webjeda.com/instant-jekyll-search/
https://gist.github.com/manbearwolf/f86195a237ba5d8f5d5f67e147acdbe1
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
<------ in body tag ------>