From 6a93b44acada261b0a5523ba309c0ecd307efe66 Mon Sep 17 00:00:00 2001 From: Valeriy Date: Mon, 4 Feb 2019 03:46:27 +0300 Subject: [PATCH] feat(client): use custom components for guide articles --- client/gatsby-node.js | 8 ++- client/src/templates/Guide/GuideArticle.js | 51 +++----------- .../Guide/components/ArticleLayout.js | 69 +++++++++++++++++++ client/utils/gatsby/guidePageCreator.js | 10 ++- 4 files changed, 91 insertions(+), 47 deletions(-) create mode 100644 client/src/templates/Guide/components/ArticleLayout.js diff --git a/client/gatsby-node.js b/client/gatsby-node.js index 13fe801eeea..b1833655388 100644 --- a/client/gatsby-node.js +++ b/client/gatsby-node.js @@ -30,12 +30,15 @@ exports.onCreateNode = function onCreateNode({ node, actions, getNode }) { } if (node.internal.type === 'MarkdownRemark') { - let slug = createFilePath({ node, getNode }); + const slug = createFilePath({ node, getNode }); if (!slug.includes('LICENSE')) { + const { + frontmatter: { component = '' } + } = node; createNodeField({ node, name: 'slug', value: slug }); + createNodeField({ node, name: 'component', value: component }); } } - }; exports.createPages = function createPages({ graphql, actions }) { @@ -75,6 +78,7 @@ exports.createPages = function createPages({ graphql, actions }) { fields { slug nodeIdentity + component } frontmatter { block diff --git a/client/src/templates/Guide/GuideArticle.js b/client/src/templates/Guide/GuideArticle.js index 1f856b0d803..54c234155a1 100644 --- a/client/src/templates/Guide/GuideArticle.js +++ b/client/src/templates/Guide/GuideArticle.js @@ -1,58 +1,28 @@ -import React, { Fragment } from 'react'; +import React from 'react'; import PropTypes from 'prop-types'; import { graphql } from 'gatsby'; -import Helmet from 'react-helmet'; -import Breadcrumbs from './components/Breadcrumbs'; +import ArticleLayout from './components/ArticleLayout'; const propTypes = { - data: PropTypes.object, - location: PropTypes.object, - pageContext: PropTypes.shape({ - meta: PropTypes.objectOf(PropTypes.string) - }) + data: PropTypes.object }; const GuideArticle = props => { const { - location: { pathname }, data: { - markdownRemark: { - html, - fields: { slug }, - frontmatter: { title } - } - }, - pageContext: { meta } + markdownRemark: { html } + } } = props; return ( - - - {`${title} | freeCodeCamp Guide`} - - - - - - - - +
- + ); }; @@ -65,12 +35,7 @@ export const pageQuery = graphql` query ArticleById($id: String!) { markdownRemark(id: { eq: $id }) { html - fields { - slug - } - frontmatter { - title - } + ...ArticleLayout } } `; diff --git a/client/src/templates/Guide/components/ArticleLayout.js b/client/src/templates/Guide/components/ArticleLayout.js new file mode 100644 index 00000000000..9f42aaf8e89 --- /dev/null +++ b/client/src/templates/Guide/components/ArticleLayout.js @@ -0,0 +1,69 @@ +import React, { Fragment } from 'react'; +import PropTypes from 'prop-types'; +import { graphql } from 'gatsby'; +import Helmet from 'react-helmet'; + +import Breadcrumbs from './Breadcrumbs'; + +const propTypes = { + children: PropTypes.any, + data: PropTypes.object, + location: PropTypes.object, + pageContext: PropTypes.shape({ + meta: PropTypes.objectOf(PropTypes.string) + }) +}; + +const ArticleLayout = props => { + const { + children, + location: { pathname }, + data: { + markdownRemark: { + fields: { slug }, + frontmatter: { title } + } + }, + pageContext: { meta } + } = props; + return ( + + + {`${title} | freeCodeCamp Guide`} + + + + + + + + + {children} + + ); +}; + +ArticleLayout.displayName = 'ArticleLayout'; +ArticleLayout.propTypes = propTypes; + +export default ArticleLayout; + +export const fragmentQuery = graphql` + fragment ArticleLayout on MarkdownRemark { + fields { + slug + } + frontmatter { + title + } + } +`; diff --git a/client/utils/gatsby/guidePageCreator.js b/client/utils/gatsby/guidePageCreator.js index bebf6e3c210..85a21a14b2c 100644 --- a/client/utils/gatsby/guidePageCreator.js +++ b/client/utils/gatsby/guidePageCreator.js @@ -13,7 +13,7 @@ exports.createGuideArticlePages = createPage => ({ node: { htmlAst, excerpt, - fields: { slug }, + fields: { slug, component }, id } }) => { @@ -32,7 +32,13 @@ exports.createGuideArticlePages = createPage => ({ return createPage({ path: `/guide${slug}`, - component: guideArticle, + component: !component + ? guideArticle + : path.resolve( + __dirname, + '../../src/templates/Guide/components/', + component + ), context: { id, meta