Permalink
Browse files

rearranging some search scripts

  • Loading branch information...
davidtmiller committed Aug 24, 2017
1 parent 0609ce4 commit f6d2564dc547ad1c823a478c452a79069cfc8052
Showing with 60 additions and 58 deletions.
  1. +1 −1 _includes/javascript.html
  2. +50 −46 assets/js/search.js
  3. +9 −11 search_data.json
@@ -1,5 +1,5 @@
<script src="https://cdnjs.cloudflare.com/ajax/libs/lunr.js/1.0.0/lunr.min.js" integrity="sha256-pFs1YPpT5gvlVt91nLsJiVFDFo67ly0bWUp4AM356/k=" crossorigin="anonymous"></script>
<script src="/assets/vendor/jquery/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lunr.js/1.0.0/lunr.min.js" integrity="sha256-pFs1YPpT5gvlVt91nLsJiVFDFo67ly0bWUp4AM356/k=" crossorigin="anonymous"></script>
<script src="/assets/js/search.js"></script>
<script src="/assets/vendor/popper/popper.min.js"></script>
<script src="/assets/vendor/bootstrap/js/bootstrap.min.js"></script>
View
@@ -1,58 +1,62 @@
jQuery(function() {
// Initalize lunr with the fields it will be searching on. I've given title
// a boost of 10 to indicate matches on this field are more important.
window.idx = lunr(function() {
this.field('id');
this.field('title', { boost: 10 });
this.field('author');
this.field('categories');
this.field('url');
this.field('description');
// Initalize lunr with the fields it will be searching on. I've given title
// a boost of 10 to indicate matches on this field are more important.
window.idx = lunr(function() {
this.field('id');
this.field('title', {
boost: 10
});
// Download the data from the JSON file we generated
window.data = $.getJSON('/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)
);
});
this.field('author');
this.field('categories');
this.field('url');
this.field('description');
});
// Download the data from the JSON file we generated
window.data = $.getJSON('/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.preventDefault();
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
});
// Event when the form is submitted
$("#site_search").submit(function() {
event.preventDefault();
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");
function display_search_results(results) {
var $search_results = $("#search_results");
// Wait for data to load
window.data.then(function(loaded_data) {
// 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
// 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];
// 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> - ' + item.description + '</li>';
// Build a snippet of HTML for this result
var appendString = '<li><a href="' + item.url + '">' + item.title + '</a> - ' + item.description + '</li>';
// Add it to the results
$search_results.append(appendString);
});
} else {
$search_results.html('<li>No results found</li>');
}
// Add it to the results
$search_results.append(appendString);
});
}
} else {
$search_results.html('<li>No results found</li>');
}
});
}
});
View
@@ -2,8 +2,15 @@
layout: null
---
{
{% for template in site.templates %}
"{{ template.url | slugify }}": {
"title": "{{ template.title | xml_escape }}",
"url": " {{ template.url | xml_escape }}",
"description": "{{ template.description | xml_escape }}"
}
,
{% endfor %}
{% for post in site.posts %}
"{{ post.url | slugify }}": {
"title": "{{ post.title | xml_escape }}",
"url": " {{ post.url | xml_escape }}",
@@ -12,15 +19,6 @@ layout: null
"source": "{{ post.source | xml_escape }}",
"description": "{{ post.description | xml_escape }}"
}
,
{% endfor %}
{% for template in site.templates %}
"{{ template.url | slugify }}": {
"title": "{{ template.title | xml_escape }}",
"url": " {{ template.url | xml_escape }}",
"description": "{{ template.description | xml_escape }}"
}
{% unless forloop.last %},{% endunless %}
{% endfor %}
}
}

0 comments on commit f6d2564

Please sign in to comment.