gatsby-node.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. const path = require(`path`);
  2. const _ = require("lodash");
  3. const { createFilePath } = require(`gatsby-source-filesystem`);
  4. exports.onCreateNode = ({ node, getNode, actions }, pluginOptions) => {
  5. const { createNodeField } = actions;
  6. if (node.internal.type === `MarkdownRemark`) {
  7. const slug = createFilePath({ node, getNode, basePath: `pages` });
  8. const parent = getNode(node.parent);
  9. const idName = _.kebabCase(node.frontmatter.title || parent.name);
  10. createNodeField({
  11. node,
  12. name: `slug`,
  13. value: slug
  14. });
  15. createNodeField({
  16. node,
  17. name: `idName`,
  18. value: idName
  19. });
  20. // save the file's directory so it can be used by the Template
  21. // component to group data in a GraphQL query
  22. createNodeField({
  23. node,
  24. name: `parentRelativeDirectory`,
  25. value: parent.relativeDirectory
  26. });
  27. // set a version field on pages so they can be queried
  28. // appropriately in the Template component
  29. let version = pluginOptions.currentVersion;
  30. if (parent.gitRemote___NODE) {
  31. const { sourceInstanceName } = getNode(parent.gitRemote___NODE);
  32. version = sourceInstanceName;
  33. }
  34. createNodeField({
  35. node,
  36. name: `version`,
  37. value: version
  38. });
  39. }
  40. };
  41. exports.createPages = async ({ actions, graphql }, pluginOptions) => {
  42. actions.createPage({
  43. path: `/`,
  44. component: path.resolve(`./src/components/template.js`)
  45. });
  46. };