Search code examples
reactjsgraphqlgatsbysanitylunrjs

Lunr - gatsby-plugin-lunr - Can I alter data / index on build?


I've got a Gatsy-Sanity project that needs a search component. For this I though of using gatsby-plugin-lunr. I run into a problem that my nodes are multilingual. For example one of my fields is constructed like:

         "title": {
            "_type": "localeString",
            "nl": "Begin ",
            "en": "Home "
         },

(This parser is, in short, like following. If has key _type that starts with 'locale*', than return only value of key en or nl. This is passed by a var)

I could make a parser that splits/strips the data. I've got this sort of working (not yet succesfull) inside the component that runs the search query from the search-index. But that would mean it parses it each search. Is there a way to do this on build in gatsby-node.js with a lunr plugin? I also need this since I would need to add a language prefix on the slug/path of the result.

const SearchProcess = lunr => builder => {
  // how to pre-process data
}

Solution

  • I'm going with a different gatsby plugin. gatsby-plugin-local-search This plugin is able to alter the data before saving it with normalizer Now I can call a method to conditional alter the data per language.