🚀 DAY 5 OF LAUNCH WEEK: Introducing Socket Firewall Enterprise.Learn more →
Socket
Book a DemoInstallSign in
Socket

gatsby-plugin-lunr-search

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

gatsby-plugin-lunr-search

Generate a search index for Lunr.

latest
Source
npmnpm
Version
1.0.1
Version published
Weekly downloads
3
-57.14%
Maintainers
1
Weekly downloads
 
Created
Source

A plugin for Gatsby to generate a search index that you can use with Lunr.

Inspired by gatsby-plugin-elasticlunr-search.

Getting started

yarn add --dev lunr gatsby-plugin-lunr-search

Usage

// gatsby-config.js
plugins: [
  {
    resolve: 'gatsby-plugin-lunr-search',
    options: {
      // Lunr reference
      ref: 'id',
      
      // Fields to be indexed
      fields: ['title', 'description'],
      
      // Nodes to be indexed
      resolvers: {
        
        // Index all `MarkdownRemark` nodes
        MarkdownRemark: {
          id: node => node.id,
          title: node => node.frontmatter.title,
          description: node => node.frontmatter.description
        }
      }
    }
  }
]

Then you can query for the search index in your pages and components. The search index is a JSON string, so you have to parse the JSON.

import React, { Component } from 'react';
import { Index } from 'lunr';
import { graphql } from 'gatsby';

export default class SomePage extends Component {
  state = {
    results: []
  };
  
  componentDidMount () {
    const { data } = this.props;
    const index = Index.load(JSON.parse(data.searchIndex.index));
    
    this.setState({
      results: index.search('query')
    });
  }
  
  render () {
    const { results } = this.state;
    if (results.length > 0) {
      return results.map(result => 
        <div key={result.ref}>Found {result.ref}</div>
      );
    }
    return <div>Could not find any results.</div>;
  }
}

const query = graphql`
  query SearchIndex {
    searchIndex {
      index
    }
  }
`;

Keywords

gatsby

FAQs

Package last updated on 01 Jul 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts