rickvanlieshout.com/gatsby/on-create-node.js

38 lines
1012 B
JavaScript
Raw Normal View History

2018-11-09 18:08:48 +01:00
'use strict';
const _ = require('lodash');
const { createFilePath } = require('gatsby-source-filesystem');
const onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions;
if (node.internal.type === 'MarkdownRemark') {
2018-11-12 18:38:36 +01:00
if (typeof node.frontmatter.slug !== 'undefined') {
2018-11-09 18:08:48 +01:00
createNodeField({
node,
name: 'slug',
2018-11-11 17:32:51 +01:00
value: node.frontmatter.slug
2018-11-09 18:08:48 +01:00
});
} else {
const value = createFilePath({ node, getNode });
createNodeField({
node,
2018-11-12 22:28:02 +01:00
name: 'slug',
value
2018-11-09 18:08:48 +01:00
});
}
if (node.frontmatter.tags) {
2018-11-11 12:19:06 +01:00
const tagSlugs = node.frontmatter.tags.map((tag) => `/tag/${_.kebabCase(tag)}/`);
2018-11-09 18:08:48 +01:00
createNodeField({ node, name: 'tagSlugs', value: tagSlugs });
}
if (node.frontmatter.category) {
2018-11-11 12:19:06 +01:00
const categorySlug = `/category/${_.kebabCase(node.frontmatter.category)}/`;
2018-11-09 18:08:48 +01:00
createNodeField({ node, name: 'categorySlug', value: categorySlug });
}
}
};
module.exports = onCreateNode;