Skip to content

Commit b8ae280

Browse files
Merge pull request #32 from commitd/cf-21-search
2 parents 35a7c8e + 8d691c4 commit b8ae280

File tree

11 files changed

+278
-72
lines changed

11 files changed

+278
-72
lines changed

README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ A theme for writing documentation sites in Markdown.
2626
- emojis using :shortcodes:
2727
- SEO friendly
2828
- Fully customizable
29+
- 🔍 Search
2930

3031
## 🔗 Live Demo and Instructions
3132

@@ -47,14 +48,14 @@ module.exports = {
4748
title: "Title",
4849
author: "Author",
4950
description: "Description",
50-
siteUrl: "https://your.site.url"
51+
siteUrl: "https://your.site.url",
5152
},
5253
plugins: [
5354
{
5455
resolve: `@committed/gatsby-theme-docs`,
55-
options: {}
56-
}
57-
]
56+
options: {},
57+
},
58+
],
5859
};
5960
```
6061

@@ -66,6 +67,16 @@ gatsby develop
6667

6768
Visit `http://localhost:8000/` to view the site.
6869

70+
Full text search is provided, but you MUST clean first to reindex the content:
71+
72+
```bash
73+
gatsby clean
74+
# Then:
75+
gatsby build
76+
# Or
77+
gatbsy develop
78+
```
79+
6980
For full instructions see the [live demo](https://committed.software/theme).
7081

7182
## 🤖 SEO friendly

example/docs/instructions.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ module.exports = {
2323
title: "Title",
2424
author: "Author",
2525
description: "Description",
26-
siteUrl: "https://your.site.url"
26+
siteUrl: "https://your.site.url",
2727
},
2828
plugins: [
2929
{
3030
resolve: `@committed/gatsby-theme-docs`,
31-
options: {}
32-
}
33-
]
31+
options: {},
32+
},
33+
],
3434
};
3535
```
3636

@@ -68,6 +68,10 @@ You can also navigate with the keyboard using `left` and `right` arrows for prev
6868
An additional page on `/print` is provided in order to print (or print to pdf) the docs.
6969
Use keyboard shortcut `shift+p` to navigate to the print page (then `cmd+p` to print), `esc` to go back home.
7070

71+
## Search
72+
73+
Search is added by default but can be turned off in the config by setting search `false`.
74+
7175
## Configuration
7276

7377
Options can be supplied to configure the docs.
@@ -106,7 +110,9 @@ module.exports = {
106110
print: {
107111
classification: null
108112
reference: "https://committed.software/docs"
109-
}
113+
},
114+
// can omit search when true, as true is the default
115+
search: false
110116
}
111117
}
112118
]

example/gatsby-config.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { NODE_ENV } = process.env;
22

3-
const pathPrefix = env => {
3+
const pathPrefix = (env) => {
44
return env === "production" ? "/docs" : "";
55
};
66

@@ -11,7 +11,7 @@ module.exports = {
1111
author: "Committed",
1212
description: "Documentation built with mdx.",
1313
siteUrl: "https://committed.software/docs",
14-
docsLocation: "https://github.com/commitd/docs-starter/tree/master/content"
14+
docsLocation: "https://github.com/commitd/docs-starter/tree/master/content",
1515
},
1616
plugins: [
1717
{
@@ -24,21 +24,21 @@ module.exports = {
2424
helpUrl: "",
2525
links: [
2626
{ text: "example", link: "/example1" },
27-
{ text: "mdx", link: "/example2" }
28-
]
27+
{ text: "mdx", link: "/example2" },
28+
],
2929
},
3030
sidebar: {
3131
ignoreIndex: false,
32-
links: [{ text: "Example", link: "https://committed.io" }]
32+
links: [{ text: "Example", link: "https://committed.io" }],
3333
},
3434
checkLinks: {
35-
exceptions: ["/broken"]
35+
exceptions: ["/broken"],
3636
},
3737
print: {
3838
classification: "OFFICIAL",
39-
reference: "https://committed.software/docs"
40-
}
41-
}
39+
reference: "https://committed.software/docs",
40+
},
41+
},
4242
},
4343
{
4444
resolve: `gatsby-plugin-manifest`,
@@ -49,11 +49,11 @@ module.exports = {
4949
background_color: `#3E3E3E`,
5050
theme_color: `#FFBB00`,
5151
display: `browser`,
52-
icon: `${__dirname}/Avatar.png` // This path is relative to the root of the site.
53-
}
54-
}
52+
icon: `${__dirname}/Avatar.png`, // This path is relative to the root of the site.
53+
},
54+
},
5555
// this (optional) plugin enables Progressive Web App + Offline functionality
5656
// To learn more, visit: https://gatsby.dev/offline
5757
// `gatsby-plugin-offline`,
58-
]
58+
],
5959
};

theme/gatsby-config.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module.exports = ({
66
sidebar,
77
print,
88
checkLinks = {},
9+
search = true,
910
}) => ({
1011
siteMetadata: {
1112
title: 'Docs',
@@ -34,12 +35,31 @@ module.exports = ({
3435
},
3536
print
3637
),
38+
search,
3739
},
3840
plugins: [
3941
`gatsby-plugin-typescript`,
4042
`gatsby-plugin-material-ui`,
4143
`gatsby-plugin-react-helmet`,
4244
`gatsby-plugin-sharp`,
45+
{
46+
resolve: `@gatsby-contrib/gatsby-plugin-elasticlunr-search`,
47+
options: {
48+
// don't index anything if search is off
49+
// this retains schema entries but stops the effort.
50+
fields: search ? [`title`, `description`, `content`] : [],
51+
resolvers: {
52+
Docs: {
53+
title: (node) => node.title,
54+
description: (node) => node.metaDescription,
55+
// TODO: This is the full raw body, including front matter
56+
content: (node) => node.rawBody,
57+
slug: (node) => node.slug,
58+
},
59+
},
60+
// TODO: Optional filter here, which would be useful for drafts?
61+
},
62+
},
4363
{
4464
resolve: `gatsby-plugin-layout`,
4565
options: {

theme/gatsby-node.js

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,65 +20,68 @@ exports.sourceNodes = ({ actions, schema }) => {
2020
fields: {
2121
id: { type: `ID!` },
2222
slug: {
23-
type: 'String!'
23+
type: 'String!',
2424
},
2525
title: {
26-
type: 'String!'
26+
type: 'String!',
2727
},
2828
metaTitle: {
29-
type: 'String!'
29+
type: 'String!',
3030
},
3131
metaDescription: {
32-
type: 'String!'
32+
type: 'String!',
3333
},
3434
order: {
35-
type: 'String!'
35+
type: 'String!',
3636
},
3737
tableOfContents: {
3838
type: 'Json!',
3939
resolve(source, args, context, info) {
4040
const type = info.schema.getType(`Mdx`)
4141
const mdxNode = context.nodeModel.getNodeById({
42-
id: source.parent
42+
id: source.parent,
4343
})
4444
const resolver = type.getFields()['tableOfContents'].resolve
4545
return resolver(mdxNode, {}, context, {
46-
fieldName: 'tableOfContents'
46+
fieldName: 'tableOfContents',
4747
})
48-
}
48+
},
4949
},
5050
body: {
5151
type: 'String!',
5252
resolve(source, args, context, info) {
5353
const type = info.schema.getType(`Mdx`)
5454
const mdxNode = context.nodeModel.getNodeById({
55-
id: source.parent
55+
id: source.parent,
5656
})
5757
const resolver = type.getFields()['body'].resolve
5858
return resolver(mdxNode, {}, context, {
59-
fieldName: 'body'
59+
fieldName: 'body',
6060
})
61-
}
62-
}
61+
},
62+
},
63+
rawBody: {
64+
type: 'String!',
65+
},
6366
},
64-
interfaces: [`Node`]
67+
interfaces: [`Node`],
6568
}),
6669
schema.buildObjectType({
6770
name: `Menu`,
6871
fields: {
6972
id: { type: `ID!` },
7073
data: {
71-
type: 'Json!'
72-
}
74+
type: 'Json!',
75+
},
7376
},
74-
interfaces: [`Node`]
75-
})
77+
interfaces: [`Node`],
78+
}),
7679
])
7780
}
7881

7982
exports.createPages = async (
8083
{ actions, graphql, reporter, createContentDigest },
81-
options
84+
{ sidebar, search = true }
8285
) => {
8386
const { createPage, createNode } = actions
8487
return new Promise((resolve, reject) => {
@@ -98,16 +101,13 @@ exports.createPages = async (
98101
}
99102
}
100103
`
101-
).then(result => {
104+
).then((result) => {
102105
if (result.errors) {
103106
reporter.panicOnBuild(`Error while running GraphQL query.`)
104107
return
105108
}
106109

107-
const treeData = calculateTreeData(
108-
options.sidebar,
109-
result.data.allDocs.edges
110-
)
110+
const treeData = calculateTreeData(sidebar, result.data.allDocs.edges)
111111

112112
const data = flattenTree(treeData)
113113

@@ -135,8 +135,8 @@ exports.createPages = async (
135135
current: item,
136136
id: item.id,
137137
previous,
138-
next
139-
}
138+
next,
139+
},
140140
})
141141
})
142142

@@ -145,12 +145,23 @@ exports.createPages = async (
145145
component: path.resolve(`${__dirname}/src/layout/print.tsx`),
146146
context: {
147147
layout: 'print',
148-
data
149-
}
148+
data,
149+
},
150150
})
151151

152+
if (search) {
153+
createPage({
154+
path: '/search',
155+
component: path.resolve(`${__dirname}/src/layout/search.tsx`),
156+
context: {
157+
layout: 'docs',
158+
data,
159+
},
160+
})
161+
}
162+
152163
const fieldData = {
153-
data: treeData
164+
data: treeData,
154165
}
155166

156167
createNode({
@@ -162,8 +173,8 @@ exports.createPages = async (
162173
type: `Menu`,
163174
content: JSON.stringify(fieldData),
164175
description: `Menu`,
165-
contentDigest: createContentDigest(fieldData)
166-
}
176+
contentDigest: createContentDigest(fieldData),
177+
},
167178
})
168179
})
169180
)
@@ -189,7 +200,7 @@ exports.onCreateNode = ({
189200
getNode,
190201
createNodeId,
191202
createContentDigest,
192-
reporter
203+
reporter,
193204
}) => {
194205
const { createNode, createParentChildLink } = actions
195206
if (node.internal.type === `Mdx`) {
@@ -213,7 +224,8 @@ exports.onCreateNode = ({
213224
metaTitle: node.frontmatter.metaTitle || title,
214225
metaDescription: node.frontmatter.metaDescription || '',
215226
order: node.frontmatter.order || title,
216-
tableOfContents: node.tableOfContents
227+
tableOfContents: node.tableOfContents,
228+
rawBody: node.rawBody,
217229
}
218230

219231
createNode({
@@ -226,13 +238,23 @@ exports.onCreateNode = ({
226238
type: `Docs`,
227239
content: JSON.stringify(fieldData),
228240
description: `Documentation`,
229-
contentDigest: createContentDigest(fieldData)
230-
}
241+
contentDigest: createContentDigest(fieldData),
242+
},
231243
})
232244
createParentChildLink({
233245
parent: parent,
234-
child: node
246+
child: node,
235247
})
236248
}
237249
}
238250
}
251+
252+
exports.createSchemaCustomization = ({ actions }) => {
253+
const { createTypes } = actions
254+
const typeDefs = `
255+
type AuthorJson implements Node {
256+
joinedAt: Date
257+
}
258+
`
259+
createTypes(typeDefs)
260+
}

theme/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"@material-ui/icons": "^4.9.1",
3737
"@mdx-js/mdx": "^1.6.1",
3838
"@mdx-js/react": "^1.6.1",
39+
"@gatsby-contrib/gatsby-plugin-elasticlunr-search": "^2.3.0",
3940
"gatsby-plugin-layout": "^1.3.0",
4041
"gatsby-plugin-material-ui": "^2.1.8",
4142
"gatsby-plugin-mdx": "^1.2.4",

0 commit comments

Comments
 (0)