Skip to content

Commit

Permalink
Ability to add custom docs to search (#624)
Browse files Browse the repository at this point in the history
  • Loading branch information
camertron authored Aug 18, 2023
1 parent d03feaa commit a7749f9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/fresh-files-melt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@primer/gatsby-theme-doctocat': minor
---

Ability to add custom docs to search
11 changes: 11 additions & 0 deletions theme/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ const mdx = require(`gatsby-plugin-mdx/utils/mdx`)

const CONTRIBUTOR_CACHE = new Map()

exports.createSchemaCustomization = async ({actions}) => {
const typeDefs = `
type CustomSearchDoc implements Node {
path: String!
title: String!
rawBody: String!
}
`
actions.createTypes(typeDefs)
}

exports.createPages = async ({graphql, actions}, themeOptions) => {
const repo = getPkgRepo(readPkgUp.sync().package)

Expand Down
36 changes: 24 additions & 12 deletions theme/src/use-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function useSearch(query) {
const workerRef = React.useRef()

const data = useStaticQuery(graphql`
{
query {
allMdx {
nodes {
fileAbsolutePath
Expand All @@ -26,20 +26,32 @@ function useSearch(query) {
}
}
}
allCustomSearchDoc {
nodes {
path
title
rawBody
}
}
}
`)

const list = React.useMemo(
() =>
data.allMdx.nodes.map(node => ({
path: ensureAbsolute(
path.join(node.parent.relativeDirectory, node.parent.name === 'index' ? '/' : node.parent.name)
),
title: node.frontmatter.title,
rawBody: node.rawBody
})),
[data]
)
const list = React.useMemo(() => {
const results = data.allMdx.nodes.map(node => ({
path: ensureAbsolute(
path.join(node.parent.relativeDirectory, node.parent.name === 'index' ? '/' : node.parent.name)
),
title: node.frontmatter.title,
rawBody: node.rawBody
}))

if (data.allCustomSearchDoc.nodes) {
results.push(...data.allCustomSearchDoc.nodes)
}

return results
}, [data])

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

Expand Down

0 comments on commit a7749f9

Please sign in to comment.