0.01
No commit activity in last 3 years
No release in over 3 years
Create a queryable index of your static site content to use with lunr.js.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

 Project Readme

middleman-lunr

middleman extension for a static indexed search using lunr.js

Create a queryable index of your static site content to use with lunr.js.

A demo can be found at: https://docs.shiftr.io.

Usage

Add gem to your Gemfile:

gem 'middleman-lunr'

Activate the extension in config.rb:

activate :lunr

Mark the files you wanna index in the frontmatter:

index: true

Create a JSON template source/search.json.erb and generate the index and map:

<%= JSON.generate(generate_search_index({data: [:title, :description]}), max_nesting: false) %>
  • The generated json will include the lunr.js index under the index key and a map that translates lunr.js references to your middleman pages under the map key.
  • The data argument is an array of frontmatter variables that you'd like to include in the map.

Reference the lunr.js library in your layouts/layout.html.erb:

<script src="//cdnjs.cloudflare.com/ajax/libs/lunr.js/0.5.9/lunr.min.js" type="text/javascript"></script>

Load and query the index:

var index;
var map;

function loadIndex(){
  $.getJSON('/search.json', function(data){
    index = lunr.Index.load(data.index);
    map = data.map
  });
}

function queryIndex(term) {
  index.search(term).forEach(function(res){
    // use the map to get the page title
    console.log(map[res.ref].title);
  });
}