Skip to content

Commit

Permalink
Move docs node generation to a local plugin (#3113)
Browse files Browse the repository at this point in the history
* Move docs node generation to a local plugin

* Use pluginOptionsSchema API for defaults

* Remove unused import
  • Loading branch information
rogermparent authored Dec 23, 2021
1 parent eb728e6 commit 001205c
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 58 deletions.
1 change: 1 addition & 0 deletions gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const plugins = [
'gatsby-plugin-react-helmet',
'gatsby-plugin-sitemap',
'gatsby-plugin-twitter',
'gatsby-theme-iterative-docs',
{
resolve: 'gatsby-source-filesystem',
options: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const path = require('path')
const GithubSlugger = require('github-slugger')

const slugger = new GithubSlugger()
Expand Down Expand Up @@ -33,18 +32,25 @@ const parseHeadings = text => {
return matches
}

const createPages = async ({ graphql, actions }) => {
// DOCS
const createPages = async (
{ graphql, actions },
{ defaultTemplate, getTemplate, 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 +62,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
38 changes: 38 additions & 0 deletions plugins/gatsby-theme-iterative-docs/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const path = require('path')

const defaultGetTemplate = (template, defaultTemplate) =>
template
? require.resolve(path.resolve('src', 'templates', template + '.tsx'))
: defaultTemplate

exports.pluginOptionsSchema = ({ Joi }) => {
return Joi.object({
disable: Joi.boolean().default(Boolean(process.env.SKIP_DOCS)),
getTemplate: Joi.function().default(() => defaultGetTemplate),
defaultTemplate: Joi.string().default(
path.resolve('src', 'templates', 'doc.tsx')
)
})
}

exports.createSchemaCustomization = async api => {
const {
actions: { createTypes },
schema: { buildObjectType }
} = api
createTypes([
buildObjectType({
name: 'DocsPage',
interfaces: ['Node'],
fields: {
template: 'String',
title: 'String',
description: 'String'
}
})
])
}

exports.createPages = require('./createPages.js')

exports.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
}
}
}
}
`

0 comments on commit 001205c

Please sign in to comment.