From 001205ced010dd5d48b0cdb8a6624bb8f27749f5 Mon Sep 17 00:00:00 2001 From: Roger Date: Thu, 23 Dec 2021 15:42:03 -0500 Subject: [PATCH] Move docs node generation to a local plugin (#3113) * Move docs node generation to a local plugin * Use pluginOptionsSchema API for defaults * Remove unused import --- gatsby-config.js | 1 + .../createPages.js | 27 ++++++++----- .../gatsby-node.js | 38 +++++++++++++++++++ .../onCreateNode.js | 23 ++++++++--- .../gatsby-theme-iterative-docs/package.json | 11 ++++++ src/gatsby/models.js | 2 - .../models/docs/createSchemaCustomization.js | 23 ----------- src/gatsby/models/docs/index.js | 13 ------- src/templates/doc.tsx | 20 +++++++--- 9 files changed, 100 insertions(+), 58 deletions(-) rename {src/gatsby/models/docs => plugins/gatsby-theme-iterative-docs}/createPages.js (77%) create mode 100644 plugins/gatsby-theme-iterative-docs/gatsby-node.js rename src/gatsby/models/docs/onCreateMarkdownContentNode.js => plugins/gatsby-theme-iterative-docs/onCreateNode.js (61%) create mode 100644 plugins/gatsby-theme-iterative-docs/package.json delete mode 100644 src/gatsby/models/docs/createSchemaCustomization.js delete mode 100644 src/gatsby/models/docs/index.js diff --git a/gatsby-config.js b/gatsby-config.js index dd6b8c1381..90edba25a9 100644 --- a/gatsby-config.js +++ b/gatsby-config.js @@ -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: { diff --git a/src/gatsby/models/docs/createPages.js b/plugins/gatsby-theme-iterative-docs/createPages.js similarity index 77% rename from src/gatsby/models/docs/createPages.js rename to plugins/gatsby-theme-iterative-docs/createPages.js index 2491d3b1ed..50595b62b8 100644 --- a/src/gatsby/models/docs/createPages.js +++ b/plugins/gatsby-theme-iterative-docs/createPages.js @@ -1,4 +1,3 @@ -const path = require('path') const GithubSlugger = require('github-slugger') const slugger = new GithubSlugger() @@ -33,8 +32,11 @@ 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( ` { @@ -42,9 +44,13 @@ const createPages = async ({ graphql, actions }) => { edges { node { id - rawMarkdownBody slug template + parent { + ... on MarkdownRemark { + rawMarkdownBody + } + } } } } @@ -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, diff --git a/plugins/gatsby-theme-iterative-docs/gatsby-node.js b/plugins/gatsby-theme-iterative-docs/gatsby-node.js new file mode 100644 index 0000000000..f9c6c849fc --- /dev/null +++ b/plugins/gatsby-theme-iterative-docs/gatsby-node.js @@ -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') diff --git a/src/gatsby/models/docs/onCreateMarkdownContentNode.js b/plugins/gatsby-theme-iterative-docs/onCreateNode.js similarity index 61% rename from src/gatsby/models/docs/onCreateMarkdownContentNode.js rename to plugins/gatsby-theme-iterative-docs/onCreateNode.js index 3754167e26..3a812e6e98 100644 --- a/src/gatsby/models/docs/onCreateMarkdownContentNode.js +++ b/plugins/gatsby-theme-iterative-docs/onCreateNode.js @@ -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' @@ -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 diff --git a/plugins/gatsby-theme-iterative-docs/package.json b/plugins/gatsby-theme-iterative-docs/package.json new file mode 100644 index 0000000000..baa489d3ce --- /dev/null +++ b/plugins/gatsby-theme-iterative-docs/package.json @@ -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" +} diff --git a/src/gatsby/models.js b/src/gatsby/models.js index 4bf6902caa..96e1729009 100644 --- a/src/gatsby/models.js +++ b/src/gatsby/models.js @@ -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') @@ -9,7 +8,6 @@ const pruneCache = require('./models/prune-cache') const models = [ markdownContent, - docs, blog, authors, imageSourcePaths, diff --git a/src/gatsby/models/docs/createSchemaCustomization.js b/src/gatsby/models/docs/createSchemaCustomization.js deleted file mode 100644 index d7ec66acf8..0000000000 --- a/src/gatsby/models/docs/createSchemaCustomization.js +++ /dev/null @@ -1,23 +0,0 @@ -const markdownParentFields = require('../markdown-content/fields.js') - -async function createSchemaCustomization(api) { - const { - actions: { createTypes }, - schema: { buildObjectType } - } = api - const typeDefs = [ - buildObjectType({ - name: 'DocsPage', - interfaces: ['Node'], - fields: { - ...markdownParentFields, - template: 'String', - title: 'String', - description: 'String' - } - }) - ] - createTypes(typeDefs) -} - -module.exports = createSchemaCustomization diff --git a/src/gatsby/models/docs/index.js b/src/gatsby/models/docs/index.js deleted file mode 100644 index c420f82d61..0000000000 --- a/src/gatsby/models/docs/index.js +++ /dev/null @@ -1,13 +0,0 @@ -const skipDocs = Boolean(process.env.SKIP_DOCS) - -const base = { - createSchemaCustomization: require('./createSchemaCustomization.js') -} - -module.exports = skipDocs - ? base - : { - ...base, - createPages: require('./createPages.js'), - onCreateMarkdownContentNode: require('./onCreateMarkdownContentNode.js') - } diff --git a/src/templates/doc.tsx b/src/templates/doc.tsx index a657f74fb2..35a20ffd61 100644 --- a/src/templates/doc.tsx +++ b/src/templates/doc.tsx @@ -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: { @@ -26,7 +28,11 @@ const DocPage: React.FC = ({ pageContext: { slug, headings } }) => { const { - page: { htmlAst, title, description } + page: { + description, + title, + parent: { htmlAst } + } } = data const { label } = getItemByPath(slug) @@ -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 + } + } } } `