All Projects → humanseelabs → gatsby-plugin-lunr

humanseelabs / gatsby-plugin-lunr

Licence: MIT license
Gatsby plugin for full text search implementation based on lunr client-side index. Supports multilanguage search.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to gatsby-plugin-lunr

gatsby-plugin-apollo-client
📡Inject a Shopify Apollo Client into the browser.
Stars: ✭ 20 (-71.01%)
Mutual labels:  gatsby, gatsbyjs, gatsby-plugin
gatsby-plugin-disqus
💬 A plugin for adding Disqus comments to GatsbyJS
Stars: ✭ 40 (-42.03%)
Mutual labels:  gatsby, gatsbyjs, gatsby-plugin
gatsby-plugin-prettier-build
prettify gatsby build output
Stars: ✭ 30 (-56.52%)
Mutual labels:  gatsby, gatsbyjs, gatsby-plugin
gatsby-attila-theme-ghost
A Gatsby theme plugin for creating blogs from headless Ghost CMS.
Stars: ✭ 16 (-76.81%)
Mutual labels:  gatsby, gatsbyjs, gatsby-plugin
gatsby-typescript-emotion-storybook
Gatsby Starter: TypeScript + Emotion + Storybook + React Intl + SVGR + Jest
Stars: ✭ 63 (-8.7%)
Mutual labels:  gatsby, jamstack, gatsbyjs
gatsby-plugin-dynamic-routes
Creating dynamic routes based on your environment and/or renaming existing routes
Stars: ✭ 14 (-79.71%)
Mutual labels:  gatsby, gatsbyjs, gatsby-plugin
Discord Fork
An open source Discord Bot List made with GatsbyJS
Stars: ✭ 60 (-13.04%)
Mutual labels:  gatsby, jamstack, gatsbyjs
gatsby-source-stripe
Gatsby source plugin for building websites using Stripe as a data source
Stars: ✭ 71 (+2.9%)
Mutual labels:  gatsby, gatsbyjs, gatsby-plugin
Gatsby Starter Foundation
A starter to launch your blazing fast personal website and a blog, Built with Gatsby and Netlify CMS. Made with ❤ by Stackrole
Stars: ✭ 135 (+95.65%)
Mutual labels:  gatsby, jamstack, gatsbyjs
gatsby-theme-deck-n-blog
Create a deck (with mdx-deck) and a blog post from the same MDX
Stars: ✭ 17 (-75.36%)
Mutual labels:  gatsby, gatsbyjs, gatsby-plugin
testimonial
Jamstack app using Gatsby, Netlify, and FaunaDB.
Stars: ✭ 23 (-66.67%)
Mutual labels:  gatsby, jamstack, gatsbyjs
gatsby-graphcms-example
Example of Gatsby source plugin for GraphCMS
Stars: ✭ 32 (-53.62%)
Mutual labels:  gatsby, gatsbyjs, gatsby-plugin
snipcart-gatsby-grav
Grav as Headless CMS Tied to Gatsby with GraphQL Schema
Stars: ✭ 27 (-60.87%)
Mutual labels:  gatsby, jamstack
gatsby-portfolio
Portfolio / Personal Website - Built with Gatsby.js and Published at konstantin.digital
Stars: ✭ 23 (-66.67%)
Mutual labels:  gatsby, gatsbyjs
gatsby-i18n
Gatsby plugin that provides i18n support
Stars: ✭ 25 (-63.77%)
Mutual labels:  gatsby, gatsby-plugin
trailing-slash-guide
Understand and fix your static website trailing slash issues!
Stars: ✭ 255 (+269.57%)
Mutual labels:  gatsby, jamstack
gatsby-starter-typescript
Typescript version of the default Gatsby starter. Uses Gatsby v1.x
Stars: ✭ 58 (-15.94%)
Mutual labels:  gatsby, gatsbyjs
gatsby-source-directus7
Source plugin for pulling data into GatsbyJS from Directus CMS (https://directus.io)
Stars: ✭ 17 (-75.36%)
Mutual labels:  gatsby, gatsby-plugin
gatsby-starter-developer-diary
An official Gatsby blog template designed for web developers. Blazing fast, it includes beautful web developer topic tags and social-media links
Stars: ✭ 76 (+10.14%)
Mutual labels:  gatsby, jamstack
gatsby-starter-fractal
Gatsby.js V2 starter template based on Fractal by HTML5 UP
Stars: ✭ 19 (-72.46%)
Mutual labels:  gatsby, gatsbyjs

Search Plugin for Gatsby

Gatsby plugin for full text search implementation based on Lunr.js client-side index. It supports multilanguage search. Search index is placed into the /public folder during build time and has to be downloaded on client side on run time.

Getting Started

Install gatsby-plugin-lunr

    npm install --save gatsby-plugin-lunr

or

    yarn add gatsby-plugin-lunr

Add gatsby-plugin-lunr configuration to the gatsby-config.js as following:

module.exports = {
    plugins: [
        {
            resolve: `gatsby-plugin-lunr`,
            options: {
                languages: [
                    {
                        // ISO 639-1 language codes. See https://lunrjs.com/guides/language_support.html for details
                        name: 'en',
                        // A function for filtering nodes. () => true by default
                        filterNodes: node => node.frontmatter.lang === 'en',
                        // Add to index custom entries, that are not actually extracted from gatsby nodes
                        customEntries: [{ title: 'Pictures', content: 'awesome pictures', url: '/pictures' }],
                    },
                    {
                        name: 'fr',
                        filterNodes: node => node.frontmatter.lang === 'fr',
                    },
                ],
                // Fields to index. If store === true value will be stored in index file.
                // Attributes for custom indexing logic. See https://lunrjs.com/docs/lunr.Builder.html for details
                fields: [
                    { name: 'title', store: true, attributes: { boost: 20 } },
                    { name: 'content' },
                    { name: 'url', store: true },
                ],
                // How to resolve each field's value for a supported node type
                resolvers: {
                    // For any node of type MarkdownRemark, list how to resolve the fields' values
                    MarkdownRemark: {
                        title: node => node.frontmatter.title,
                        content: node => node.rawMarkdownBody,
                        url: node => node.fields.url,
                    },
                },
                //custom index file name, default is search_index.json
                filename: 'search_index.json',
                //custom options on fetch api call for search_ındex.json
                fetchOptions: {
                    credentials: 'same-origin'
                },
            },
        },
    ],
}

Using plugins

const myPlugin = (lunr) => (builder) => {
  // removing stemmer
  builder.pipeline.remove(lunr.stemmer)
  builder.searchPipeline.remove(lunr.stemmer)
  // or similarity tuning
  builder.k1(1.3)
  builder.b(0)
}

Pass it to the gatsby-config.js: ... languages: [ { name: 'en', ... plugins: [myPlugin] } ] ...

Implementing Search in Your Web UI using Functional Components

The search data will be available on the client side via window.__LUNR__ that is an object with the following fields:

  • index - a lunr index instance
  • store - object where the key is a gatsby node ID and value is a collection of field values.
import React, { useState, useEffect } from 'react'
import { Link } from 'gatsby'

const Search = () => {
  const [query, setQuery] = useState(``)
  const [results, setResults] = useState([])

  useEffect(
    () => {
      if (!query || !window.__LUNR__) {
        setResults([])
        return
      }
      const lunrIndex = window.__LUNR__['en']
      const searchResults = lunrIndex.index.search(query)
      setResults(
        searchResults.map(({ ref }) => {
          return lunrIndex.store[ref]
        })
      )
    },
    [query]
  )

  return (
    <div>
      <input
        type='text'
        defaultValue={query}
        onChange={event => {
          setQuery(event.target.value)
        }}
      />
      <ul>
        {results.map(({ url, title }) => {
          return (
            <li key={url}>
              <Link to={url}>{title}</Link>
            </li>
          )
        })}
      </ul>
    </div>
  )
}

export default Search

Implementing Search in Your Web UI using Class Components

The search data will be available on the client side via window.__LUNR__ that is an object with the following fields:

  • index - a lunr index instance
  • store - object where the key is a gatsby node ID and value is a collection of field values.
import React, { Component } from 'react'

// Search component
export default class Search extends Component {
    constructor(props) {
        super(props)
        this.state = {
            query: ``,
            results: [],
        }
    }

    render() {
        return (
            <div>
                <input type="text" value={this.state.query} onChange={this.search} />
                <ul>{this.state.results.map(page => <li>{page.title}</li>)}</ul>
            </div>
        )
    }

    getSearchResults(query) {
        if (!query || !window.__LUNR__) return []
        const lunrIndex =  window.__LUNR__[this.props.lng];
        const results = lunrIndex.index.search(query) // you can  customize your search , see https://lunrjs.com/guides/searching.html
        return results.map(({ ref }) => lunrIndex.store[ref])
    }

    search = event => {
        const query = event.target.value
        const results = this.getSearchResults(query)
        this.setState(s => {
            return {
                results,
                query,
            }
        })
    }
}

Sample code and example on implementing search within gatsby starter project could be found in the article at: https://medium.com/humanseelabs/gatsby-v2-with-a-multi-language-search-plugin-ffc5e04f73bc

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].