Skip to content

Instantly share code, notes, and snippets.

@manbearwolf
Created October 21, 2017 14:07
Show Gist options
  • Select an option

  • Save manbearwolf/073b7c47c96c38f38698e533176a1ee0 to your computer and use it in GitHub Desktop.

Select an option

Save manbearwolf/073b7c47c96c38f38698e533176a1ee0 to your computer and use it in GitHub Desktop.
---
---
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>');
}
});
}
});
---
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 %}
}
@manbearwolf

manbearwolf commented Oct 21, 2017

Copy link
Copy Markdown
Author
<!-- Search lunr taken from https://github.com/projectpages/project-pages -->
<script type="text/javascript" async defer src="https://cdnjs.cloudflare.com/ajax/libs/lunr.js/0.7.0/lunr.min.js"></script>

<!-- custom js file for lunr 0.7.0 -->
<script type="text/javascript" async defer src="/js/search.js"></script>

<------ in body tag ------>

@manbearwolf

manbearwolf commented Oct 21, 2017

Copy link
Copy Markdown
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 ------------------>

@manbearwolf

manbearwolf commented Oct 21, 2017

Copy link
Copy Markdown
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...

@manbearwolf

manbearwolf commented Nov 1, 2017

Copy link
Copy Markdown
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

@manbearwolf

manbearwolf commented Nov 1, 2017

Copy link
Copy Markdown
Author

@manbearwolf

manbearwolf commented Nov 1, 2017

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment