Skip to content

Commit 8d691c4

Browse files
committed
Make search optional
Tried to just to optionally remove the plugin, page and components but gatsby would still fail the search query, even though the page wasn't created anywhere. So have changed the indexed fields to empty if off, then the schema still gets updated and the query works. Also made the text field get focus and updtes the docs
1 parent 2cb27bd commit 8d691c4

File tree

9 files changed

+85
-63
lines changed

9 files changed

+85
-63
lines changed

README.md

Lines changed: 2 additions & 1 deletion
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

@@ -66,7 +67,7 @@ gatsby develop
6667

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

69-
Full text search is provided, but you MUST before a clean first to reindex the content:
70+
Full text search is provided, but you MUST clean first to reindex the content:
7071

7172
```bash
7273
gatsby clean

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: 5 additions & 2 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,17 +35,19 @@ 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`,
43-
// Add search
4445
{
4546
resolve: `@gatsby-contrib/gatsby-plugin-elasticlunr-search`,
4647
options: {
47-
fields: [`title`, `description`, `content`],
48+
// don't index anything if search is off
49+
// this retains schema entries but stops the effort.
50+
fields: search ? [`title`, `description`, `content`] : [],
4851
resolvers: {
4952
Docs: {
5053
title: (node) => node.title,

theme/gatsby-node.js

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ exports.sourceNodes = ({ actions, schema }) => {
8181

8282
exports.createPages = async (
8383
{ actions, graphql, reporter, createContentDigest },
84-
options
84+
{ sidebar, search = true }
8585
) => {
8686
const { createPage, createNode } = actions
8787
return new Promise((resolve, reject) => {
@@ -107,10 +107,7 @@ exports.createPages = async (
107107
return
108108
}
109109

110-
const treeData = calculateTreeData(
111-
options.sidebar,
112-
result.data.allDocs.edges
113-
)
110+
const treeData = calculateTreeData(sidebar, result.data.allDocs.edges)
114111

115112
const data = flattenTree(treeData)
116113

@@ -152,14 +149,16 @@ exports.createPages = async (
152149
},
153150
})
154151

155-
createPage({
156-
path: '/search',
157-
component: path.resolve(`${__dirname}/src/layout/search.tsx`),
158-
context: {
159-
layout: 'docs',
160-
data,
161-
},
162-
})
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+
}
163162

164163
const fieldData = {
165164
data: treeData,
@@ -249,3 +248,13 @@ exports.onCreateNode = ({
249248
}
250249
}
251250
}
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/src/components/Header.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const Header = () => {
88
site: {
99
siteMetadata: {
1010
header: { title, helpUrl, logo, links },
11+
search,
1112
},
1213
},
1314
} = useStaticQuery(
@@ -27,6 +28,7 @@ export const Header = () => {
2728
text
2829
}
2930
}
31+
search
3032
}
3133
}
3234
}
@@ -58,11 +60,13 @@ export const Header = () => {
5860
</Button>
5961
</ClearLink>
6062
))}
61-
<ClearLink href="/search">
62-
<Button color="inherit" variant="text">
63-
<Icons.Search />
64-
</Button>
65-
</ClearLink>
63+
{search ? (
64+
<ClearLink href="/search">
65+
<Button color="inherit" variant="text">
66+
<Icons.Search />
67+
</Button>
68+
</ClearLink>
69+
) : null}
6670
{helpUrl !== '' ? (
6771
<a href={helpUrl}>
6872
<Icons.Help />

theme/src/components/Layout.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { PreviousNext } from './PreviousNext'
1212
import { SEO, SEOProps } from './SEO'
1313
import { Sidebar } from './Sidebar'
1414

15-
export interface LayoutProps extends SEOProps {
15+
export interface LayoutProps extends Omit<SEOProps, 'title'> {
1616
id: string
1717
pageContext: PageContext
1818
location: any
@@ -39,9 +39,9 @@ export const Layout = ({
3939
useHotkeys('shift+home', () => navigate('/'))
4040
useHotkeys('shift+p', () => navigate('/print'))
4141

42-
const navigateTo = url => {
42+
const navigateTo = (url) => {
4343
navigate(url, {
44-
state: collapsed
44+
state: collapsed,
4545
})
4646
}
4747

@@ -51,20 +51,20 @@ export const Layout = ({
5151
pathname: location.pathname,
5252
navigate: navigateTo,
5353
collapsed,
54-
setCollapsed
54+
setCollapsed,
5555
}}
5656
>
5757
<ThemeProvider
5858
fonts={{
5959
display: {
6060
fontFamily:
6161
'Dosis, "Helvetica Neue", "Segoe UI", Helvetica, Arial, sans-serif',
62-
fontWeight: 700
62+
fontWeight: 700,
6363
},
6464
text: {
6565
fontFamily:
66-
'Lato, -apple-system, BlinkMacSystemFont, "San Francisco", Roboto, "Segoe UI", "Helvetica Neue"'
67-
}
66+
'Lato, -apple-system, BlinkMacSystemFont, "San Francisco", Roboto, "Segoe UI", "Helvetica Neue"',
67+
},
6868
}}
6969
>
7070
{pageContext.current && pageContext.current.title && (
@@ -73,7 +73,7 @@ export const Layout = ({
7373
<Root
7474
style={{ minHeight: '100vh' }}
7575
config={{
76-
collapsible: false
76+
collapsible: false,
7777
}}
7878
>
7979
<LayoutHeader>
@@ -83,7 +83,7 @@ export const Layout = ({
8383
header={
8484
// you can provide fixed header inside nav
8585
// change null to some react element
86-
ctx => null
86+
(ctx) => null
8787
}
8888
>
8989
<Sidebar location={location} current={id} />

theme/src/components/Search.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export const Search: FC<SearchProps> = (props: SearchProps) => {
6969
label="Query"
7070
value={query}
7171
onChange={(e) => setQuery(e.target.value)}
72+
autoFocus
7273
/>
7374
<Button color="primary">Search</Button>
7475
</Form>

theme/src/layout/search.tsx

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
import { graphql } from 'gatsby'
1+
import { StaticQuery, graphql } from 'gatsby'
22
import React from 'react'
33
import { Search } from '../components/Search'
44
import '../style/mermaid.css'
55

6-
export default ({ data }) => {
7-
const {
8-
siteSearchIndex: { index },
9-
} = data
10-
return <Search index={index} />
11-
}
12-
13-
export const pageQuery = graphql`
14-
query SearchIndexQuery {
15-
siteSearchIndex {
16-
index
17-
}
18-
}
19-
`
6+
export default () => (
7+
<StaticQuery
8+
query={graphql`
9+
query SearchIndexQuery {
10+
siteSearchIndex {
11+
index
12+
}
13+
}
14+
`}
15+
render={({ siteSearchIndex: { index } }) => <Search index={index} />}
16+
/>
17+
)

0 commit comments

Comments
 (0)