gatsby-node.js 974 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. const {createFilePath} = require('gatsby-source-filesystem')
  2. const path = require('path')
  3. exports.onCreateNode = ({node, getNode, actions}) => {
  4. const {createNodeField} = actions
  5. if (node.internal.type === 'MarkdownRemark') {
  6. const slug = createFilePath({node, getNode, basePath: 'content', trailingSlash: false})
  7. createNodeField({
  8. node,
  9. name: 'slug',
  10. value: slug
  11. })
  12. }
  13. }
  14. exports.createPages = ({graphql, actions}) => {
  15. const {createPage} = actions
  16. return graphql(`
  17. {
  18. allMarkdownRemark {
  19. edges {
  20. node {
  21. id
  22. fields {
  23. slug
  24. }
  25. html
  26. }
  27. }
  28. }
  29. }
  30. `).then(result => {
  31. result.data.allMarkdownRemark.edges.forEach(({node}) => {
  32. createPage({
  33. path: node.fields.slug,
  34. component: path.resolve('./src/templates/Page.js'),
  35. context: {
  36. slug: node.fields.slug
  37. }
  38. })
  39. })
  40. })
  41. }