diff --git a/gatsby-config.js b/gatsby-config.js index 6add018..bf893de 100644 --- a/gatsby-config.js +++ b/gatsby-config.js @@ -56,8 +56,5 @@ module.exports = { }, }, `gatsby-plugin-sass`, - // this (optional) plugin enables Progressive Web App + Offline functionality - // To learn more, visit: https://gatsby.dev/offline - // `gatsby-plugin-offline`, ], } diff --git a/gatsby-node.js b/gatsby-node.js index 2f42665..95e67b5 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -1,7 +1,55 @@ -/** - * Implement Gatsby's Node APIs in this file. - * - * See: https://www.gatsbyjs.org/docs/node-apis/ - */ +const path = require(`path`) +const { createFilePath } = require(`gatsby-source-filesystem`) -// You can delete this file if you're not using it +exports.onCreateNode = ({ node, getNode, actions }) => { + const { createNodeField } = actions + if (node.internal.type === `MarkdownRemark`) { + const slug = createFilePath({ node, getNode, basePath: `pages` }) + createNodeField({ + node, + name: `slug`, + value: slug, + }) + } +} + +exports.createPages = async ({ graphql, actions, reporter }) => { + const { createPage } = actions + + const result = await graphql( + ` + { + allMarkdownRemark(filter: {frontmatter: {draft: {ne: true}}}) { + edges { + node { + fields { + slug + } + } + } + } + } + ` + ) + + // Handle errors + if (result.errors) { + reporter.panicOnBuild(`Error while running GraphQL query.`) + return + } + + // Create pages for each markdown file. + const blogPostTemplate = path.resolve(`src/templates/blog-post.js`) + result.data.allMarkdownRemark.edges.forEach(({ node }) => { + console.log(node); + createPage({ + path: `/posts${node.fields.slug}`, + component: blogPostTemplate, + // In your blog post template's graphql query, you can use pagePath + // as a GraphQL variable to query for data from the markdown file. + context: { + slug: node.fields.slug, + }, + }) + }) + } \ No newline at end of file diff --git a/src/components/post-link.js b/src/components/post-link.js index 4cce7b0..29fbbde 100644 --- a/src/components/post-link.js +++ b/src/components/post-link.js @@ -7,13 +7,13 @@ const PostLink = ({ post }) => { const postDate = new Date(post.frontmatter.date) const options = { year: 'numeric', month: 'long', day: 'numeric' }; const postDateString = postDate.toLocaleDateString('en', options); - const postUrl = "/posts/" + post.fileAbsolutePath.split('/').pop().split(".")[0] + const postUrl = "/posts" + post.fields.slug return (