npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

docpad-plugin-lunr

v2.1.0

Published

Adds support for client-side search with Lunr, full-text and faceted.

Downloads

9

Readme

DocPad Lunr Plugin

Generates Lunr search indexes from your Docpad collections, and provides helpers for a client-side full-text and faceted search.

Getting Started

npm install --save docpad-plugin-lunr

or

docpad install lunr

Configure a collection to index in your docpad.coffee:

plugins:
  lunr:
    indexes:
      myIndex:
        collection: 'myCollection'

To create an index which includes multiple collections you can pass an array of collection names:

plugins:
  lunr:
    indexes:
      myIndex:
        collection: ['firstCollection','secondCollection']

Make your search page itself, eg my-search-page.html.eco:

<%- @getLunrSearchPage('myIndex') %>

Make a "search bar" on other pages that redirects users to your search page above, like in a layout, eg. default.html.eco:

<%- @getLunrSearchBlock('my-search-page.html') %>

Customization

To control the fields that get indexed and their "boost" levels (ie, relevance), add to the index's configuration in docpad.coffee:

indexFields: [
  {name: "body", boost: 1}
  {name: "title", boost: 10}
  {name: "tags", boost: 100}
]

To control the fields that are available in search results, add to the lunr configuration in docpad.coffee:

contentFields: [
  {name: "title"}
  {name: "url"}
  {name: "date"}
]

For faceted search, add to the lunr configuration in docpad.coffee:

facetFields: [
  {name: "tags", label: "Filter by tag"}
  {name: "type", label: "Filter by type"}
]

(Note: the faceted search is not part of Lunr, just my own poor-man's version. Also, you may want to put in a bit of CSS to highlight the "active" facet filters, such as: <style>.active:after{content:'*';}</style>.)

To provide an Eco template for the search-results, add to the lunr configuration in docpad.coffee:

resultsTemplate: 'src/partials/teaser.html.eco'

Then in src/partials/teaser.html.eco:

<div>
  <a href="<%= post.url %>"><%= post.title %></a>
  <span>posted on <%= post.date %></span>
</div>

Or provide a template function for search results directly in docpad.coffee:

resultsTemplate: (context) ->
  post = context.post
  return """
  <div>
    <a href="#{post.url}">#{post.title}</a>
    <span>posted on #{post.date}</span>
  </div>
  """

To add your own "stopwords" to prevent certain words from being indexed, add to the index's configuration in docpad.coffee

stopWords: ['an','array','of','words']

Advanced usage

If you want to make your own UI (and your own implementation of facets), here are the basics:

<script src="/lunr/lunr.min.js" type="text/javascript"></script>
<script src="/lunr/lunr-data-myindex.js" type="text/javascript"></script>
<script type="text/javascript">
  lunrdoc.idx = lunr.Index.load(lunrdoc.indexJson);
  var results = lunrdoc.idx.search('this is your user input');
  for (var i in results) {
    var itemData = lunrdoc.content[results[i].ref];
    console.log(itemData);
    var renderedItem = lunrdoc.template({post: itemData});
    console.log(renderedItem);
  }
</script>

Future

  • Currently this plugin is geared towards static sites, and brings everything client-side. Need to allow for dynamic sites to take advantage of having a back-end - ie, keep the index, searching, and content on the back-end.
  • Hopefully if faceted search is added to Lunr proper, re-implement using that
  • Failing that, add more facet types, like dates and numeric ranges
  • Allow for custom pipeline functions