Skip to content

Instantly share code, notes, and snippets.

@danilowoz
Last active April 28, 2019 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danilowoz/b0610841325e52bc3623a83485e8ba31 to your computer and use it in GitHub Desktop.
Save danilowoz/b0610841325e52bc3623a83485e8ba31 to your computer and use it in GitHub Desktop.
Creating the pages programmatically
const { createFilePath } = require(`gatsby-source-filesystem`)
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions
if (node.internal.type === `MarkdownRemark`) {
const value = createFilePath({ node, getNode })
const [month, day, year] = new Date(node.frontmatter.date)
.toLocaleDateString("en-EN", {
year: "numeric",
month: "2-digit",
day: "2-digit",
})
.split("/")
const slug = value.replace("/blog/", "").replace(/\/$/, "")
const url = `/blog/${year}/${month}/${day}${slug}`
createNodeField({
name: `slug`,
node,
value: url,
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment