Skip to content

Commit

Permalink
feat(embeds): create embed pages
Browse files Browse the repository at this point in the history
  • Loading branch information
roadlittledawn committed Mar 29, 2021
1 parent 13e7eef commit e84f1e8
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ module.exports = {
},
},
'gatsby-plugin-meta-redirect',
'gatsby-plugin-embed-pages',
{
resolve: 'gatsby-plugin-gdpr-tracking',
options: {
Expand Down
45 changes: 45 additions & 0 deletions plugins/gatsby-plugin-embed-pages/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const path = require('path');

exports.createPages = async ({ actions, graphql, reporter }) => {
const { createPage } = actions;

const { data } = await graphql(`
query MyQuery {
allMdx(filter: { frontmatter: { template: { eq: "GuideTemplate" } } }) {
nodes {
fields {
fileRelativePath
slug
}
frontmatter {
title
path
}
body
}
}
}
`);

data.allMdx.nodes.forEach((node) => {
const {
frontmatter,
fields: { fileRelativePath, slug },
body,
} = node;
const nodePath = frontmatter.path || slug;
const pagePath = path.join(nodePath, 'embed');
const contentSourcePath = nodePath;

createPage({
path: pagePath,
component: path.resolve(`src/templates/embedPage.js`),
context: {
slug,
fileRelativePath,
contentSourcePath,
layout: 'EmbedLayout',
},
});
});
};
1 change: 1 addition & 0 deletions plugins/gatsby-plugin-embed-pages/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
13 changes: 13 additions & 0 deletions src/layouts/EmbedLayout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Layout } from '@newrelic/gatsby-theme-newrelic';
import PropTypes from 'prop-types';

const EmbedLayout = ({ children, pageContext }) => {
return <Layout.Main>{children}</Layout.Main>;
};

EmbedLayout.propTypes = {
children: PropTypes.node,
pageContext: PropTypes.object,
};

export default EmbedLayout;
1 change: 1 addition & 0 deletions src/layouts/MainLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { useLocation } from '@reach/router';
import pages from '../data/nav.yml';

const MainLayout = ({ children, pageContext }) => {
const isEmbed = pageContext.layout && pageContext.layout === 'embed';
const location = useLocation();
const [searchTerm, setSearchTerm] = useState('');
const { fileRelativePath } = pageContext;
Expand Down
5 changes: 5 additions & 0 deletions src/layouts/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import React from 'react';
import MainLayout from './MainLayout';
import EmbedLayout from './EmbedLayout';
import PropTypes from 'prop-types';

const Layout = ({ children, pageContext }) => {
const isEmbed = pageContext.layout && pageContext.layout === 'EmbedLayout';
if (pageContext.fileRelativePath.match(/404/)) {
return children;
}
if (isEmbed) {
return <EmbedLayout pageContext={pageContext}>{children}</EmbedLayout>;
}
return <MainLayout pageContext={pageContext}>{children}</MainLayout>;
};

Expand Down
34 changes: 34 additions & 0 deletions src/templates/embedPage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React from 'react';
import { graphql } from 'gatsby';
import PropTypes from 'prop-types';
import PageLayout from '../components/PageLayout';

const EmbedPage = ({ data }) => {
const { mdx } = data;
const { frontmatter, body } = mdx;
const { title } = frontmatter;

return (
<>
<h1>{title}</h1>
<PageLayout.MarkdownContent>{body}</PageLayout.MarkdownContent>
</>
);
};

export const pageQuery = graphql`
query($contentSourcePath: String!) {
mdx(frontmatter: { path: { eq: $contentSourcePath } }) {
body
frontmatter {
title
}
}
}
`;

EmbedPage.propTypes = {
data: PropTypes.object,
};

export default EmbedPage;

0 comments on commit e84f1e8

Please sign in to comment.