|
| 1 | +const {join, relative} = require('path'); |
| 2 | +module.exports = ({root, contentDir = 'content'}) => ({ |
| 3 | + plugins: [ |
| 4 | + // You can have multiple instances of this plugin to create indexes with |
| 5 | + // different names or engines. For example, multi-lingual sites could create |
| 6 | + // an index for each language. |
| 7 | + { |
| 8 | + resolve: 'gatsby-plugin-local-search', |
| 9 | + options: { |
| 10 | + // A unique name for the search index. This should be descriptive of |
| 11 | + // what the index contains. This is required. |
| 12 | + name: 'pages', |
| 13 | + |
| 14 | + // Set the search engine to create the index. This is required. |
| 15 | + // The following engines are supported: flexsearch, lunr |
| 16 | + engine: 'flexsearch', |
| 17 | + |
| 18 | + // Provide options to the engine. This is optional and only recommended |
| 19 | + // for advanced users. |
| 20 | + // |
| 21 | + // Note: Only the flexsearch engine supports options. |
| 22 | + engineOptions: 'speed', |
| 23 | + |
| 24 | + // GraphQL query used to fetch all data for the search index. This is |
| 25 | + // required. |
| 26 | + query: ` |
| 27 | + { |
| 28 | + allMarkdownRemark { |
| 29 | + nodes { |
| 30 | + excerpt(format: PLAIN, pruneLength: 100, truncate: true) |
| 31 | + id |
| 32 | + fileAbsolutePath |
| 33 | + rawMarkdownBody |
| 34 | + frontmatter { |
| 35 | + description |
| 36 | + subtitle |
| 37 | + title |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | +} |
| 42 | + `, |
| 43 | + |
| 44 | + // Field used as the reference value for each document. |
| 45 | + // Default: 'id'. |
| 46 | + ref: 'id', |
| 47 | + |
| 48 | + // List of keys to index. The values of the keys are taken from the |
| 49 | + // normalizer function below. |
| 50 | + // Default: all fields |
| 51 | + index: ['title', 'body'], |
| 52 | + |
| 53 | + // List of keys to store and make available in your UI. The values of |
| 54 | + // the keys are taken from the normalizer function below. |
| 55 | + // Default: all fields |
| 56 | + store: ['id', 'path', 'title', 'excerpt'], |
| 57 | + |
| 58 | + // Function used to map the result from the GraphQL query. This should |
| 59 | + // return an array of items to index in the form of flat objects |
| 60 | + // containing properties to index. The objects must contain the `ref` |
| 61 | + // field above (default: 'id'). This is required. |
| 62 | + normalizer({ |
| 63 | + data: { |
| 64 | + allMarkdownRemark: {nodes} |
| 65 | + } |
| 66 | + }) { |
| 67 | + //relative to the content. |
| 68 | + const relTo = join(root, contentDir); |
| 69 | + return nodes.map( |
| 70 | + ({ |
| 71 | + id, |
| 72 | + frontmatter, |
| 73 | + fileAbsolutePath, |
| 74 | + rawMarkdownBody: body, |
| 75 | + excerpt |
| 76 | + }) => ({ |
| 77 | + id, |
| 78 | + body, |
| 79 | + excerpt, |
| 80 | + path: relative(relTo, fileAbsolutePath), |
| 81 | + title: frontmatter && frontmatter.title |
| 82 | + }) |
| 83 | + ); |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + ] |
| 88 | +}); |
0 commit comments