Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move docs node generation to a local plugin #3113

Merged
merged 4 commits into from
Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ const plugins = [
'gatsby-plugin-react-helmet',
'gatsby-plugin-sitemap',
'gatsby-plugin-twitter',
{
resolve: 'gatsby-theme-iterative-docs',
options: {
docComponent: require.resolve('./src/templates/doc.tsx'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is the docComponent being used in the theme? I'm not seeing it being used anywhere 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops! I renamed this to defaultTemplate in createPages but didn't end up updating it here.

disable: Boolean(process.env.SKIP_DOCS)
}
},
{
resolve: 'gatsby-source-filesystem',
options: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,34 @@ const parseHeadings = text => {
return matches
}

const createPages = async ({ graphql, actions }) => {
// DOCS
const defaultGetTemplate = (template, defaultTemplate) =>
template
? require.resolve(path.resolve('src', 'templates', template + '.tsx'))
: defaultTemplate

const createPages = async (
{ graphql, actions },
{
defaultTemplate = path.resolve('src', 'templates', 'doc.tsx'),
getTemplate = defaultGetTemplate,
disable
}
) => {
if (disable) return
const docsResponse = await graphql(
`
{
docs: allDocsPage(limit: 9999) {
edges {
node {
id
rawMarkdownBody
slug
template
parent {
... on MarkdownRemark {
rawMarkdownBody
}
}
}
}
}
Expand All @@ -56,19 +72,20 @@ const createPages = async ({ graphql, actions }) => {
throw docsResponse.errors
}

const docComponent = require.resolve('../../../templates/doc.tsx')

docsResponse.data.docs.edges.forEach(doc => {
const {
node: { id, slug, rawMarkdownBody, template }
node: {
id,
slug,
template,
parent: { rawMarkdownBody }
}
} = doc
const headings = parseHeadings(rawMarkdownBody)

if (slug) {
actions.createPage({
component: template
? require.resolve(path.resolve('src', 'templates', template + '.tsx'))
: docComponent,
component: getTemplate(template, defaultTemplate),
path: slug,
context: {
id,
Expand Down
21 changes: 21 additions & 0 deletions plugins/gatsby-theme-iterative-docs/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module.exports = {
createSchemaCustomization: async function createSchemaCustomization(api) {
const {
actions: { createTypes },
schema: { buildObjectType }
} = api
createTypes([
buildObjectType({
name: 'DocsPage',
interfaces: ['Node'],
fields: {
template: 'String',
title: 'String',
description: 'String'
}
})
])
},
createPages: require('./createPages.js'),
onCreateNode: require('./onCreateNode.js')
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
const path = require('path')

async function createMarkdownDocsNode(api, { parentNode, createChildNode }) {
const splitDir = parentNode.relativeDirectory.split('/')
function onCreateNode(
{
node,
getNode,
createNodeId,
createContentDigest,
actions: { createNode, createParentChildLink }
},
{ disable }
) {
if (disable || node.internal.type !== 'MarkdownRemark') {
return
}
const parentNode = getNode(node.parent)
const splitDir = parentNode.relativeDirectory.split(path.sep)
if (splitDir[0] !== 'docs') return

const { node, createNodeId, createContentDigest } = api
const { name, relativePath } = parentNode
splitDir[0] = 'doc'

Expand All @@ -30,7 +42,8 @@ async function createMarkdownDocsNode(api, { parentNode, createChildNode }) {
}
}

return createChildNode(docNode)
createNode(docNode)
createParentChildLink({ parent: node, child: docNode })
}

module.exports = createMarkdownDocsNode
module.exports = onCreateNode
11 changes: 11 additions & 0 deletions plugins/gatsby-theme-iterative-docs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "gatsby-theme-iterative-docs",
"version": "0.0.1",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "MIT"
}
2 changes: 0 additions & 2 deletions src/gatsby/models.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const markdownContent = require('./models/markdown-content')
const docs = require('./models/docs')
const blog = require('./models/blog')
const authors = require('./models/authors')
const imageSourcePaths = require('./models/image-source-paths')
Expand All @@ -9,7 +8,6 @@ const pruneCache = require('./models/prune-cache')

const models = [
markdownContent,
docs,
blog,
authors,
imageSourcePaths,
Expand Down
23 changes: 0 additions & 23 deletions src/gatsby/models/docs/createSchemaCustomization.js

This file was deleted.

13 changes: 0 additions & 13 deletions src/gatsby/models/docs/index.js

This file was deleted.

20 changes: 15 additions & 5 deletions src/templates/doc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import Documentation from '../components/Documentation'
interface IDocPageProps {
data: {
page: {
htmlAst: Node
title?: string
description?: string
title?: string
parent: {
htmlAst: Node
}
}
}
pageContext: {
Expand All @@ -26,7 +28,11 @@ const DocPage: React.FC<IDocPageProps> = ({
pageContext: { slug, headings }
}) => {
const {
page: { htmlAst, title, description }
page: {
description,
title,
parent: { htmlAst }
}
} = data

const { label } = getItemByPath(slug)
Expand All @@ -44,9 +50,13 @@ export default DocPage
export const pageQuery = graphql`
query DocPage($id: String!) {
page: docsPage(id: { eq: $id }) {
title
description
htmlAst
title
parent {
... on MarkdownRemark {
htmlAst
}
}
}
}
`