Skip to content
This repository was archived by the owner on Apr 13, 2023. It is now read-only.

Commit 1fedd23

Browse files
author
Justin
committed
feat: add local search component
1 parent 5259e40 commit 1fedd23

File tree

10 files changed

+16951
-0
lines changed

10 files changed

+16951
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# gatsby-theme-apollo-local-search
2+
3+
A Gatsby theme extension that allows local search.
4+
5+
## Installation
6+
7+
8+
```bash
9+
npm install gatsby-theme-apollo-local-search
10+
```
11+
12+
## Usage
13+
14+
Add `gatsby-theme-apollo-local-search` as a plugin in your Gatsby config. Configure content
15+
to match your contentDir and root to match the root dir of your gatsby theme.
16+
17+
```js
18+
// gatsby-config.js
19+
module.exports = {
20+
plugins: ['gatsby-theme-apollo-local-search', {options:{root:__dirname, contentDir:'content'}}]
21+
};
22+
```
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//noop

0 commit comments

Comments
 (0)