Skip to content

Commit a7749f9

Browse files
authored
Ability to add custom docs to search (#624)
1 parent d03feaa commit a7749f9

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

.changeset/fresh-files-melt.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@primer/gatsby-theme-doctocat': minor
3+
---
4+
5+
Ability to add custom docs to search

theme/gatsby-node.js

+11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ const mdx = require(`gatsby-plugin-mdx/utils/mdx`)
99

1010
const CONTRIBUTOR_CACHE = new Map()
1111

12+
exports.createSchemaCustomization = async ({actions}) => {
13+
const typeDefs = `
14+
type CustomSearchDoc implements Node {
15+
path: String!
16+
title: String!
17+
rawBody: String!
18+
}
19+
`
20+
actions.createTypes(typeDefs)
21+
}
22+
1223
exports.createPages = async ({graphql, actions}, themeOptions) => {
1324
const repo = getPkgRepo(readPkgUp.sync().package)
1425

theme/src/use-search.js

+24-12
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ function useSearch(query) {
1010
const workerRef = React.useRef()
1111

1212
const data = useStaticQuery(graphql`
13-
{
13+
query {
1414
allMdx {
1515
nodes {
1616
fileAbsolutePath
@@ -26,20 +26,32 @@ function useSearch(query) {
2626
}
2727
}
2828
}
29+
30+
allCustomSearchDoc {
31+
nodes {
32+
path
33+
title
34+
rawBody
35+
}
36+
}
2937
}
3038
`)
3139

32-
const list = React.useMemo(
33-
() =>
34-
data.allMdx.nodes.map(node => ({
35-
path: ensureAbsolute(
36-
path.join(node.parent.relativeDirectory, node.parent.name === 'index' ? '/' : node.parent.name)
37-
),
38-
title: node.frontmatter.title,
39-
rawBody: node.rawBody
40-
})),
41-
[data]
42-
)
40+
const list = React.useMemo(() => {
41+
const results = data.allMdx.nodes.map(node => ({
42+
path: ensureAbsolute(
43+
path.join(node.parent.relativeDirectory, node.parent.name === 'index' ? '/' : node.parent.name)
44+
),
45+
title: node.frontmatter.title,
46+
rawBody: node.rawBody
47+
}))
48+
49+
if (data.allCustomSearchDoc.nodes) {
50+
results.push(...data.allCustomSearchDoc.nodes)
51+
}
52+
53+
return results
54+
}, [data])
4355

4456
const [results, setResults] = React.useState(list)
4557

0 commit comments

Comments
 (0)