diff --git a/.changeset/fresh-files-melt.md b/.changeset/fresh-files-melt.md new file mode 100644 index 00000000..770edc17 --- /dev/null +++ b/.changeset/fresh-files-melt.md @@ -0,0 +1,5 @@ +--- +'@primer/gatsby-theme-doctocat': minor +--- + +Ability to add custom docs to search diff --git a/theme/gatsby-node.js b/theme/gatsby-node.js index 56f612da..780dc7e2 100644 --- a/theme/gatsby-node.js +++ b/theme/gatsby-node.js @@ -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) diff --git a/theme/src/use-search.js b/theme/src/use-search.js index 08578462..933df975 100644 --- a/theme/src/use-search.js +++ b/theme/src/use-search.js @@ -10,7 +10,7 @@ function useSearch(query) { const workerRef = React.useRef() const data = useStaticQuery(graphql` - { + query { allMdx { nodes { fileAbsolutePath @@ -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)