rickvanlieshout.com/gatsby/on-create-node.js
2018-11-09 20:08:48 +03:00

38 lines
1016 B
JavaScript

'use strict';
const _ = require('lodash');
const { createFilePath } = require('gatsby-source-filesystem');
const onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions;
if (node.internal.type === 'MarkdownRemark') {
if (typeof node.frontmatter.path !== 'undefined') {
createNodeField({
node,
name: 'slug',
value: node.frontmatter.path
});
} else {
const value = createFilePath({ node, getNode });
createNodeField({
name: 'slug',
node,
value,
});
}
if (node.frontmatter.tags) {
const tagSlugs = node.frontmatter.tags.map((tag) => `/tags/${_.kebabCase(tag)}/`);
createNodeField({ node, name: 'tagSlugs', value: tagSlugs });
}
if (node.frontmatter.category) {
const categorySlug = `/categories/${_.kebabCase(node.frontmatter.category)}/`;
createNodeField({ node, name: 'categorySlug', value: categorySlug });
}
}
};
module.exports = onCreateNode;