Skip to content

Commit

Permalink
Update Gatsby Node API implementations to handle landing page assets
Browse files Browse the repository at this point in the history
This allows to query for images (raster) and videos for port project
landing pages.

GH-140
  • Loading branch information
arcticicestudio committed May 4, 2019
1 parent d0670fd commit e9795e3
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 5 deletions.
58 changes: 55 additions & 3 deletions .gatsby/onCreateNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ const {
nodeFields,
optionalBlogPostImages,
optionalBlogPostVideos,
sourceInstanceTypes,
requiredBlogPostImages
requiredBlogPostImages,
sourceInstanceTypes
} = require("../src/config/internal/nodes");
const { BASE_DIR_CONTENT, NODE_TYPE_MDX, REGEX_BLOG_POST_DATE } = require("../src/config/internal/constants");
const {
BASE_DIR_CONTENT,
NODE_TYPE_IMAGE_SHARP,
NODE_TYPE_MDX,
REGEX_BLOG_POST_DATE
} = require("../src/config/internal/constants");
const { ROUTE_BLOG, ROUTE_DOCS } = require("../src/config/routes/mappings");

/**
Expand Down Expand Up @@ -51,6 +56,7 @@ const extractBlogPostDateFromPath = path => {
*/
const onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions;

if (node.internal.type === NODE_TYPE_MDX) {
const contentFileResourceSlug = createFilePath({
node,
Expand Down Expand Up @@ -163,6 +169,52 @@ const onCreateNode = ({ node, getNode, actions }) => {
});
}
}

if (node.internal.type === NODE_TYPE_IMAGE_SHARP) {
const contentFileResourceSlug = createFilePath({
node,
getNode,
basePath: `${BASE_DIR_CONTENT}`,
trailingSlash: false
});
const { relativeDirectory, sourceInstanceName } = getNode(node.parent);

if (sourceInstanceName === sourceInstanceTypes.images.id) {
createNodeField({
node,
name: `${nodeFields.contentSourceType.name}`,
value: `${sourceInstanceTypes.images.id}`
});
createNodeField({
node,
name: `${nodeFields.relativeDirectory.name}`,
value: `${relativeDirectory}`
});
createNodeField({
node,
name: `${nodeFields.slug.name}`,
value: contentFileResourceSlug
});
}

if (sourceInstanceName === sourceInstanceTypes.videos.id) {
createNodeField({
node,
name: `${nodeFields.contentSourceType.name}`,
value: `${sourceInstanceTypes.videos.id}`
});
createNodeField({
node,
name: `${nodeFields.relativeDirectory.name}`,
value: `${relativeDirectory}`
});
createNodeField({
node,
name: `${nodeFields.slug.name}`,
value: contentFileResourceSlug
});
}
}
};

module.exports = onCreateNode;
10 changes: 9 additions & 1 deletion gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const {
const {
BASE_DIR_CONTENT,
BASE_DIR_ASSETS_IMAGES,
BASE_DIR_ASSETS_VIDEOS,
BASE_DIR_CONFIG,
BASE_DIR_PAGES
} = require("./src/config/internal/constants");
Expand Down Expand Up @@ -89,10 +90,17 @@ module.exports = {
{
resolve: "gatsby-source-filesystem",
options: {
name: "images",
name: `${sourceInstanceTypes.images.id}`,
path: `${__dirname}/${BASE_DIR_ASSETS_IMAGES}`
}
},
{
resolve: "gatsby-source-filesystem",
options: {
name: `${sourceInstanceTypes.videos.id}`,
path: `${__dirname}/${BASE_DIR_ASSETS_VIDEOS}`
}
},
{
resolve: "gatsby-source-filesystem",
options: {
Expand Down
21 changes: 21 additions & 0 deletions src/config/internal/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ const BASE_DIR_ASSETS = `${BASE_DIR_SRC}/assets`;
*/
const BASE_DIR_ASSETS_IMAGES = `${BASE_DIR_ASSETS}/images`;

/**
* The relative path of the assets directory for videos starting from the project root.
*
* @constant {string}
* @since 0.12.0
*/
const BASE_DIR_ASSETS_VIDEOS = `${BASE_DIR_ASSETS}/videos`;

/**
* The relative path of the build base directory starting from the project root.
*
Expand Down Expand Up @@ -126,6 +134,17 @@ const BLOG_POST_IMAGE_COVER_MIN_WIDTH = BLOG_POST_IMAGE_MIN_HEIGHT * 0.85;
*/
const BLOG_POST_IMAGE_HERO_MIN_WIDTH = BLOG_POST_IMAGE_MIN_HEIGHT * 1.8;

/**
* The internal type for Gatsby sharp images.
*
* @constant {string}
* @see https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-transformer-sharp
* @see https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-sharp
* @see https://github.com/lovell/sharp
* @since 0.12.0
*/
const NODE_TYPE_IMAGE_SHARP = "ImageSharp";

/**
* The internal type for MDX nodes.
*
Expand All @@ -146,6 +165,7 @@ const REGEX_BLOG_POST_DATE = /([0-9]+)\/([0-9]+)\/([0-9]+)\/(.+)/;
module.exports = {
BASE_DIR_ASSETS,
BASE_DIR_ASSETS_IMAGES,
BASE_DIR_ASSETS_VIDEOS,
BASE_DIR_BUILD,
BASE_DIR_BUILD_REPORTS,
BASE_DIR_BUILD_REPORTS_COVERAGE,
Expand All @@ -157,6 +177,7 @@ module.exports = {
BLOG_POST_IMAGE_BANNER_MIN_WIDTH,
BLOG_POST_IMAGE_COVER_MIN_WIDTH,
BLOG_POST_IMAGE_HERO_MIN_WIDTH,
NODE_TYPE_IMAGE_SHARP,
NODE_TYPE_MDX,
REGEX_BLOG_POST_DATE
};
4 changes: 3 additions & 1 deletion src/config/internal/nodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ const requiredBlogPostImages = {
*/
const sourceInstanceTypes = {
blog: { id: "blog", path: "blog" },
docs: { id: "docs", path: "docs" }
docs: { id: "docs", path: "docs" },
images: { id: "images", path: "images" },
videos: { id: "videos", path: "videos" }
};

/**
Expand Down
1 change: 1 addition & 0 deletions src/data/graphql/fragmentPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const contentMdxMediaFilePropTypes = {
extension: PropTypes.string,
name: PropTypes.string,
publicURL: PropTypes.string,
relativeDirectory: PropTypes.string,
relativePath: PropTypes.string
};

Expand Down
1 change: 1 addition & 0 deletions src/data/graphql/fragments.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const gqlFragmentContentBlogPostMediaFile = graphql`
extension
name
publicURL
relativeDirectory
relativePath
}
`;
Expand Down

0 comments on commit e9795e3

Please sign in to comment.