• This repository has been archived on 19/Sep/2019
  • Stars
    star
    549
  • Rank 80,592 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 11 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

[UNSUPPORTED] Jekyll + lunr.js = static websites with powerful full-text search using JavaScript

Jekyll + lunr.js = Static websites with powerful full-text search using JavaScript

Use lunr.js to provide simple full-text search, using JavaScript in your browser, for your Jekyll static website.

Inspired by Pascal Widdershoven's Jekyll + indextank and Michael Levin's Sitemap.xml Generator plugins.

This Jekyll plugin handles the generation of a lunr.js compatible .json index file. Runtime search configuration is provided by a simple jQuery plugin.

It allows full-text search of all your Jekyll pages and posts. Executed by the client without any server-side processing (outside of serving static files).

How to use

1. Install the plugin

Choose to install as either a Ruby Gem, or by copying the pre-built plugin file to your Jekyll site.

1a. Install as a Ruby Gem

  1. Install the jekyll-lunr-js-search Ruby Gem.

     gem install jekyll-lunr-js-search
    
  2. Modify your Jekyll _config.yml file to include the Gem.

     gems: ['jekyll-lunr-js-search']
    

1b. Install by copying the plugin to your Jekyll site.

  1. Place build/jekyll_lunr_js_search.rb inside the _plugins folder in the root of your Jekyll site.

The content from all Jekyll posts and pages will be indexed to a js/index.json file ready for lunr.js to consume. This happens each time the site is generated.

A jQuery plugin is provided in js/jquery.lunr.search.js to handle the configuration of lunr.js with the search index JSON data generated by this plugin.

Dependencies for the jQuery plugin are as follows.

A pre-built version of the jQuery plugin, along with all of the above dependencies, concatenated and minified is available from at build/search.min.js.

2. Copy the jQuery plugin and add a script reference.

2a. Using the pre-built, minified plugin from the gem.

The plugin will automatically add the minified JavaScript file js/search.min.js to your _site.

To use it, you must add a script reference to the bottom of your nominated search page.

    <script src="/js/search.min.js" type="text/javascript" charset="utf-8"></script>

2b. Using the jQuery plugin and managing its dependencies yourself.

  1. Copy js/jquery.lunr.search.js to your Jekyll site's JavaScript directory.

  2. Add a script reference to the bottom of your nominated search page for jquery.lunr.search.js and each of the dependencies outlined above.

     <script src="/js/jquery-1.9.1.min.js" type="text/javascript" charset="utf-8"></script>
     <script src="/js/lunr.min.js" type="text/javascript" charset="utf-8"></script>
     <script src="/js/mustache.js" type="text/javascript" charset="utf-8"></script>
     <script src="/js/date.format.js" type="text/javascript" charset="utf-8"></script>
     <script src="/js/URI.min.js" type="text/javascript" charset="utf-8"></script>
     <script src="/js/jquery.lunr.search.js" type="text/javascript" charset="utf-8"></script>
    

Make sure you use the same version of lunr.js as the gem. The Jekyll log output includes the version used.

Ideally you would concatenate, minify and optimise these six .js files using uglify/Google closure/etc to produce a single search.min.js file to reference (or use the pre-built script as described in 2a above).

<script src="/js/search.min.js" type="text/javascript" charset="utf-8"></script>

4. Add a search form with a query input as shown.

<form action="/search" method="get">
  <input type="text" id="search-query" name="q" placeholder="Search" autocomplete="off">
</form>

Search happens as you type, once at least three characters have been entered.

Providing the form action and specifying the get method allows the user to hit return/enter to also submit the search. Amend the form's action URL as necessary for the search page on your own site.

5. Add an element to contain the list of search result entries.

<section id="search-results" style="display: none;"> </section>

This may be initially hidden as the plugin will show the element when searching.

6. Create a Mustache template to display the search results.

{% raw %}
<script id="search-results-template" type="text/mustache">
  {{#entries}}
    <article>
      <h3>
        {{#date}}<small><time datetime="{{pubdate}}" pubdate>{{displaydate}}</time></small>{{/date}}
        <a href="{{url}}">{{title}}</a>
      </h3>
      {{#is_post}}
      <ul>
        {{#tags}}<li>{{.}} </li>{{/tags}}
      </ul>
      {{/is_post}}
    </article>
  {{/entries}}
</script>
{% endraw %}

Note the use of {% raw %} and {% endraw %} to ensure the Mustache tags are not stripped out by Jekyll.

The fields available to display are as follows.

entries

List of search result entries (mandatory).

date

Raw published date for posts, or null for pages. Can be used to toggle display of the following dates in the template {{#date}}has a date{{/date}} {{#!date}}no date{{/date}}.

pubdate

Post published date, formatted as 'yyyy-mm-dd', to be used in a html5 <time datetime="{{pubdate}}"> element (posts only).

displaydate

Post published date, formatted as 'mmm dd, yyyy', such as Oct 12, 2012 (posts only)

title

Title of the Jekyll page or post.

url

URL of the Jekyll page or post that can be used to create a hyperlink <a href="{{url}}">{{title}}</a>.

categories

Categories (array) of the Jekyll page or post, can be used in a loop {{#categories}}{{.}} {{/categories}} to list them.

tags

Tags (array) of the Jekyll page or post, can be used in a loop {{#tags}}{{.}} {{/tags}} to list them.

is_post

Booelan value, true if current result element is a post. Can be used to toggle display of specific elements in the template {{#is_post}}is a post{{/is_post}}

7. Configure the jQuery plugin for the search input field.

<script type="text/javascript">
  $(function() {
    $('#search-query').lunrSearch({
      indexUrl  : '/js/index.json',           // url for the .json file containing search index data
      results   : '#search-results',          // selector for containing search results element
      template  : '#search-results-template', // selector for Mustache.js template
      titleMsg  : '<h1>Search results<h1>',   // message attached in front of results (can be empty)
      emptyMsg  : '<p>Nothing found.</p>'     // shown message if search returns no results
    });
  });
</script>

8. To exclude pages from the search index.

Add the following exclude_from_search setting to any page's YAML config.

exclude_from_search: true

Or add an array of exclusions (as individual regular expressions) to the site's _config.yml file.

lunr_search:
  excludes: [rss.xml, atom.xml]

9. Stop Words

You can also configure a stopwords file, and a minimum length of word to be included in the index file. This can be done by adding a search block to _config.yml. The default values are:

lunr_search:
  stopwords: "stopwords.txt"
  min_length: 3

The stopwords file must consist of one word per line, in lowercase, without punctuation.

10. Alternate data directory

You can choose to store index.json, search.min.js and lunr.min.js in a different directory like this:

lunr_search:
  js_dir: "javascript"

Demo

Search plugin is deployed to 10consulting.com/search. Some example search queries are /search/?q=git, /search/?q=cqrs.

It also features on-demand loading of the search plugin .js when focusing into the search field on the homepage. Look at the browser network requests clicking into the search input.

Building

To build the single jekyll_lunr_js_search.rb plugin file.

Requirements

Install Bundler and then run the following.

bundle install

Install Bower.

To build the plugin.

rake build

Then copy build/jekyll_lunr_js_search.rb to your Jekyll site's _plugins folder and the build/*.min.js files to your site's js folder.

If you include the .js and .js.map files your browser developer console will link to the unminified code.

More Repositories

1

awesome-elixir-cqrs

A curated list of awesome Elixir and Command Query Responsibility Segregation (CQRS) resources.
704
star
2

conduit

RealWorld example backend implementing the CQRS/ES pattern in Elixir and Phoenix
Elixir
347
star
3

faker-cs

C# port of the Ruby Faker gem (http://faker.rubyforge.org/)
C#
241
star
4

node-ledger-web

Web front-end to access ledger cli data.
JavaScript
144
star
5

saas-startup-checklist

SaaS Startup Checklist
128
star
6

phoenix-react-redux-example

Phoenix framework example using React and Redux
JavaScript
110
star
7

eventsourced

Functional domain models with event sourcing in Elixir
Elixir
104
star
8

rcqrs

Ruby CQRS with Event Sourcing library
Ruby
98
star
9

cqrs-best-practices

Best practices, guidance, and anti-patterns to avoid when building an application following CQRS/ES principles
95
star
10

node-pipes-and-filters

Pipes and Filters for Node.js to divide a larger processing task into a sequence of smaller, independent processing steps (Filters) that are connected by channels (Pipes).
JavaScript
80
star
11

segment-challenge

Segment Challenge is an Elixir Phoenix web application built using Commanded
Elixir
76
star
12

node-ledger

API for the ledger command-line interface (ledger-cli.org).
JavaScript
72
star
13

strava

Elixir wrapper for the Strava API (v3)
Elixir
46
star
14

til

Today I Learned
Elixir
44
star
15

eventstore-migrator

Copy & transform migration strategy for Elixir EventStore
Elixir
31
star
16

node-ledger-rest

REST web service to access the Ledger command-line interface (http://ledger-cli.org/).
JavaScript
27
star
17

stateless

[DEPRECATED] A C# Hierarchical State Machine
C#
23
star
18

Treaty

Rules Engine written in TypeScript implementing the Rete algorithm
20
star
19

domain-driven-js

Domain-driven JavaScript
20
star
20

gift-card-demo

Commanded demo application focused around a simple giftcard domain.
Elixir
20
star
21

implementing-cqrs-in-elixir

An introduction to implementing Command Query Responsibility Segregation (CQRS) in Elixir applications.
17
star
22

node-ledger-import

Import accounting transactions from CSV files to Ledger format.
JavaScript
11
star
23

cqrs-journey-pdf

CQRS Journey Guide converted to PDF for eBook reading.
Ruby
11
star
24

rcqrs-rails

Rails 3 plugin to use the RCQRS library
Ruby
9
star
25

rcqrs-blog

Rails 3 blog app using the RCQRS library
JavaScript
9
star
26

typescript-intro-presentation

Introduction to TypeScript presentation
JavaScript
2
star
27

Duplicity

Duplicate file system changes from one directory to another.
C#
2
star
28

kv

Key/value store using Elixir and Commanded CQRS/ES
Elixir
2
star
29

calculator

Attempt to define supported operations using `before_compile` macro
Elixir
2
star
30

DotNetFlow

An ASP.NET MVC web application for sharing links that demonstrates using CQRS with event sourcing. Inspired by RubyFlow.
JavaScript
2
star
31

eventstore-export

Export events stored in an EventStore database to disk
Elixir
2
star
32

notice

Notice board for tasks
JavaScript
1
star
33

ledger-import

Ruby
1
star
34

commanded_aggregate_race

Elixir
1
star
35

broadway-show

Broadway experiment using a custom `GenStage` producer with partitioning which stops processing on message failure
Elixir
1
star
36

nerves_agile_octopus

Display Agile Octopus electricity prices on an Inky pHAT display connected to a Raspberry Pi using Nerves.
Elixir
1
star