gatsby-plugin-local-search
Version:
Gatsby plugin for providing client-side search for data available in Gatsby's GraphQL layer using a variety of engines
3 lines (2 loc) • 3.25 kB
JavaScript
function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}var r,t=e(require("path")),n=e(require("fs")),i=e(require("lunr")),o=e(require("flexsearch")),a=require("lodash"),c=require("pascal-case");!function(e){e.LocalSearch="LocalSearch"}(r||(r={}));var s=function(e){return"gatsby-plugin-local-search - "+e};exports.createPages=function(e,t){try{var n=e.reporter,u=e.createNodeId,d=e.createContentDigest,l=e.actions.createNode,f=t.name,h=t.engine,p=t.ref,g=void 0===p?"id":p,v=t.store,x=t.normalizer;return Promise.resolve((0,e.graphql)(t.query)).then(function(p){if(!p.errors)return Promise.resolve(Promise.resolve(x(p))).then(function(p){p.length<1&&n.warn(s('The query for index "'+f+'" returned no nodes. The index and store will be empty.'));var x=p.filter(function(e){return null!=e[g]}),y=function(e,r,t){var n=t.reporter,c=r.name;switch(r.engine){case"flexsearch":return function(e,r){var t=r.ref,n=void 0===t?"id":t,i=r.index,c=o.create(r.engineOptions);return e.forEach(function(e){var r=JSON.stringify(i?a.pick(e,i):e);c.add(e[n],r)}),c.export()}(e,r);case"lunr":return function(e,r){var t=r.ref,n=void 0===t?"id":t,o=r.index,a=null!=o?o:e.length>0?Object.keys(e[0]):[],c=i(function(){var r=this;this.ref(n),a.forEach(function(e){return r.field(e)}),e.forEach(function(e){return r.add(e)})});return JSON.stringify(c)}(e,r);default:n.error(s('The engine option for index "'+c+'" is invalid. It must be one of: flexsearch, lunr. The index will not be created.'))}}(x,t,e);if(y){var S=x.reduce(function(e,r){return e[String(r[g])]=v?a.pick(r,v):r,e},{}),m=c.pascalCase(r.LocalSearch+" "+f),b={id:u(f),name:f,engine:h,index:y,store:S,internal:{type:m,contentDigest:d({index:y,store:S})}};l(b)}});n.error(s("The provided GraphQL query contains errors. The index will not be created."),p.errors[0])})}catch(e){return Promise.reject(e)}},exports.createSchemaCustomization=function(e,i){try{var o=e.schema,a=e.reporter,u=e.pathPrefix,d=e.actions.createTypes,l=i.name,f=c.pascalCase(r.LocalSearch+" "+l);return d([o.buildObjectType({name:f,fields:{name:{type:"String!",description:"The name of the index."},engine:{type:"String!",description:"The search engine used to create the index."},index:{type:"String!",description:"The search index created using the selected engine."},store:{type:"JSON!",description:"A JSON object used to map search results to their data."},publicIndexURL:{type:"String!",description:"Save the index to the site's static directory and return a public URL to it.",resolve:function(e){var r=e.internal.contentDigest+".index.txt",i=t.join(process.cwd(),"public","static",r);return n.existsSync(i)||n.writeFile(i,e.index,function(e){e&&a.error(s('Could not save the index for "'+l+'" to '+i))}),u+"/static/"+r}},publicStoreURL:{type:"String!",description:"Save the store to the site's static directory and return a public URL to it.",resolve:function(e){var r=e.internal.contentDigest+".store.json",i=t.join(process.cwd(),"public","static",r);return n.existsSync(i)||n.writeFile(i,JSON.stringify(e.store),function(e){e&&a.error(s('Could not save the store for "'+l+'" to '+i))}),u+"/static/"+r}}},interfaces:["Node"]})]),Promise.resolve()}catch(e){return Promise.reject(e)}};
//# sourceMappingURL=gatsby-node.js.map