From d17a557738700bfc9a744724d4dd1ce72a71afca Mon Sep 17 00:00:00 2001 From: Josh Date: Wed, 14 Feb 2018 23:37:23 -0600 Subject: [PATCH 1/2] feature: define blog slug in frontmatter Modify gatsby-node.js that reads in frontmatter slug and uses it for blog URL. If frontmatter is not defined or slug is not defined it will default to file path for slug. --- gatsby-node.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gatsby-node.js b/gatsby-node.js index e65fa7c4a..3f9b26036 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -36,9 +36,10 @@ exports.onCreateNode = ({node, boundActionCreators, getNode}) => { let slug; switch (node.internal.type) { case `MarkdownRemark`: + const fmSlug = (node.frontmatter && node.frontmatter.slug) ? node.frontmatter.slug : false const fileNode = getNode(node.parent); const [basePath, name] = fileNode.relativePath.split('/'); - slug = `/${basePath}/${name}/`; + slug = fmSlug ? `/${basePath}/${fmSlug}/` : `/${basePath}/${name}/`; break; } if (slug) { From 7c2ffd22371d724fbf629cd0b940f64ce14f799a Mon Sep 17 00:00:00 2001 From: Joshua Kornblum Date: Thu, 15 Feb 2018 01:22:50 -0600 Subject: [PATCH 2/2] add unit tests and fix linting issues --- data/blog/2017-04-18--welcoming/index.md | 3 +- gatsby-node.js | 13 ++++++- test/__snapshots__/gatsby-node.test.js.snap | 42 +++++++++++++++++++++ test/gatsby-node.test.js | 40 +++++++++++++++++++- 4 files changed, 94 insertions(+), 4 deletions(-) diff --git a/data/blog/2017-04-18--welcoming/index.md b/data/blog/2017-04-18--welcoming/index.md index b253d3130..6b2a8a45b 100644 --- a/data/blog/2017-04-18--welcoming/index.md +++ b/data/blog/2017-04-18--welcoming/index.md @@ -1,13 +1,14 @@ --- title: Welcoming createdDate: '2017-04-18' -updatedDate: '2017-05-06' +updatedDate: '2018-02-15' author: Fabien BERNARD tags: - starter - gatsby image: pexels-photo-253092.jpeg draft: false +slug: 2017-04-18/welcoming --- Welcome to gatsby-starter! diff --git a/gatsby-node.js b/gatsby-node.js index 3f9b26036..9c15f82d0 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -29,6 +29,15 @@ exports.modifyWebpackConfig = ({config, stage}) => { } }; +// If slug defined in frontmatter use it +// otherwise use the filepath as slug +const buildSlug = (frontmatterSlug, basePath, name) => { + if (frontmatterSlug) { + return `/${basePath}/${frontmatterSlug}/`; + } + return `/${basePath}/${name}/`; +}; + // Create slugs for files. // Slug will used for blog page path. exports.onCreateNode = ({node, boundActionCreators, getNode}) => { @@ -36,10 +45,10 @@ exports.onCreateNode = ({node, boundActionCreators, getNode}) => { let slug; switch (node.internal.type) { case `MarkdownRemark`: - const fmSlug = (node.frontmatter && node.frontmatter.slug) ? node.frontmatter.slug : false + const frontmatterSlug = get(node, 'frontmatter.slug', null); const fileNode = getNode(node.parent); const [basePath, name] = fileNode.relativePath.split('/'); - slug = fmSlug ? `/${basePath}/${fmSlug}/` : `/${basePath}/${name}/`; + slug = buildSlug(frontmatterSlug, basePath, name); break; } if (slug) { diff --git a/test/__snapshots__/gatsby-node.test.js.snap b/test/__snapshots__/gatsby-node.test.js.snap index 524f0bf9f..23c92dccc 100644 --- a/test/__snapshots__/gatsby-node.test.js.snap +++ b/test/__snapshots__/gatsby-node.test.js.snap @@ -193,6 +193,18 @@ Array [ Object { "name": "slug", "node": Object { + "frontmatter": Object { + "author": "Fabien BERNARD", + "createdDate": "2017-04-18", + "draft": false, + "image": "pexels-photo-253092.jpeg", + "tags": Array [ + "starter", + "gatsby", + ], + "title": "Welcoming", + "updatedDate": "2017-05-06", + }, "internal": Object { "type": "MarkdownRemark", }, @@ -203,3 +215,33 @@ Array [ ], ] `; + +exports[`gatsby-node onCreateNode should use frontmatter slug for MarkdownRemark file when defined 1`] = ` +Array [ + Array [ + Object { + "name": "slug", + "node": Object { + "frontmatter": Object { + "author": "Fabien BERNARD", + "createdDate": "2017-04-18", + "draft": false, + "image": "pexels-photo-253092.jpeg", + "slug": "2017-04-18/welcoming", + "tags": Array [ + "starter", + "gatsby", + ], + "title": "Welcoming", + "updatedDate": "2017-05-06", + }, + "internal": Object { + "type": "MarkdownRemark", + }, + "parent": "parent", + }, + "value": "/blog/2017-04-18/welcoming/", + }, + ], +] +`; diff --git a/test/gatsby-node.test.js b/test/gatsby-node.test.js index 2600683cf..fc3c679d8 100644 --- a/test/gatsby-node.test.js +++ b/test/gatsby-node.test.js @@ -196,11 +196,49 @@ describe('gatsby-node', () => { relativePath: 'blog/2017-04-18--welcoming/index.md' } ); + const node = { internal: { type: 'MarkdownRemark' }, - parent: 'parent' + parent: 'parent', + frontmatter: { + title: 'Welcoming', + createdDate: '2017-04-18', + updatedDate: '2017-05-06', + author: 'Fabien BERNARD', + tags: ['starter', 'gatsby'], + image: 'pexels-photo-253092.jpeg', + draft: false + } + }; + onCreateNode({node, boundActionCreators, getNode}); + + expect(boundActionCreators.createNodeField.mock.calls).toMatchSnapshot(); + }); + + it('should use frontmatter slug for MarkdownRemark file when defined', () => { + getNode.mockReturnValue( + { + relativePath: 'blog/2017-04-18--welcoming/index.md' + } + ); + + const node = { + internal: { + type: 'MarkdownRemark' + }, + parent: 'parent', + frontmatter: { + title: 'Welcoming', + createdDate: '2017-04-18', + updatedDate: '2017-05-06', + author: 'Fabien BERNARD', + tags: ['starter', 'gatsby'], + image: 'pexels-photo-253092.jpeg', + draft: false, + slug: '2017-04-18/welcoming' + } }; onCreateNode({node, boundActionCreators, getNode});