Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: define blog slug in frontmatter #48

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion data/blog/2017-04-18--welcoming/index.md
Original file line number Diff line number Diff line change
@@ -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!
Expand Down
12 changes: 11 additions & 1 deletion gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,26 @@ 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}) => {
const {createNodeField} = boundActionCreators;
let slug;
switch (node.internal.type) {
case `MarkdownRemark`:
const frontmatterSlug = get(node, 'frontmatter.slug', null);
const fileNode = getNode(node.parent);
const [basePath, name] = fileNode.relativePath.split('/');
slug = `/${basePath}/${name}/`;
slug = buildSlug(frontmatterSlug, basePath, name);
break;
}
if (slug) {
Expand Down
42 changes: 42 additions & 0 deletions test/__snapshots__/gatsby-node.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand All @@ -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/",
},
],
]
`;
40 changes: 39 additions & 1 deletion test/gatsby-node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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});

Expand Down