Skip to content

Commit

Permalink
feat: add related resources from swiftype to the right rail
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Aug 13, 2020
1 parent 4ce7c43 commit fdd265e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
9 changes: 9 additions & 0 deletions gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const path = require(`path`);
const { execSync } = require('child_process');

const MAX_RESULTS = 5;

const getFileRelativePath = (absolutePath) =>
absolutePath.replace(`${process.cwd()}/`, '');

Expand All @@ -17,6 +19,9 @@ exports.createPages = async ({ actions, graphql, reporter }) => {
path
template
redirects
resources {
url
}
}
}
}
Expand Down Expand Up @@ -53,6 +58,10 @@ exports.createPages = async ({ actions, graphql, reporter }) => {
frontmatter.template === 'OverviewTemplate'
? `${frontmatter.path}/*`
: undefined,
relatedResourceLimit: Math.max(
MAX_RESULTS - (frontmatter.resources || []).length,
0
),
},
});
});
Expand Down
16 changes: 13 additions & 3 deletions src/components/RelatedContentModules/Resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const SITE_TAGS = {
kubernetes: 'https://kubernetes.io',
youtube: 'https://youtube.com',
discuss: 'https://discuss.newrelic.com',
'newrelic.com': 'https://newrelic.com',
};

const findTag = (resource) =>
Expand All @@ -29,10 +30,11 @@ const normalizeDeveloperUrl = (url) =>

const Resources = ({ page }) => {
const {
relatedResources,
frontmatter: { resources },
} = page;

return resources?.length > 0 ? (
return (
<Section>
<Title>Related resources</Title>
<nav>
Expand All @@ -43,7 +45,7 @@ const Resources = ({ page }) => {
padding: 0;
`}
>
{resources.map((resource) => {
{(resources || []).concat(relatedResources).map((resource) => {
const tag = findTag(resource);
const isDeveloperSite = tag === 'developer';
const LinkElement = isDeveloperSite ? Link : ExternalLink;
Expand Down Expand Up @@ -98,7 +100,7 @@ const Resources = ({ page }) => {
</ul>
</nav>
</Section>
) : null;
);
};

Resources.propTypes = {
Expand All @@ -111,6 +113,10 @@ Resources.propTypes = {
})
),
}).isRequired,
relatedResources: PropTypes.shape({
title: PropTypes.string,
url: PropTypes.string,
}),
}).isRequired,
};

Expand All @@ -122,6 +128,10 @@ export const query = graphql`
url
}
}
relatedResources(limit: $relatedResourceLimit) {
title
url
}
}
`;

Expand Down
2 changes: 1 addition & 1 deletion src/templates/GuideTemplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ GuideTemplate.propTypes = {
};

export const pageQuery = graphql`
query($path: String!) {
query($path: String!, $relatedResourceLimit: Int!) {
mdx(frontmatter: { path: { eq: $path } }) {
body
frontmatter {
Expand Down

0 comments on commit fdd265e

Please sign in to comment.