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

41 lines
1.1 KiB
JavaScript
Raw Normal View History

2018-11-09 18:08:48 +01:00
'use strict';
const _ = require('lodash');
const { createFilePath } = require('gatsby-source-filesystem');
2018-11-11 12:19:06 +01:00
const { fmImagesToRelative } = require('gatsby-remark-relative-images');
2018-11-09 18:08:48 +01:00
const onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions;
2018-11-11 12:19:06 +01:00
fmImagesToRelative(node);
2018-11-09 18:08:48 +01:00
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) {
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;