Last active
April 28, 2019 19:10
-
-
Save danilowoz/b0610841325e52bc3623a83485e8ba31 to your computer and use it in GitHub Desktop.
Creating the pages programmatically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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