Skip to content

Instantly share code, notes, and snippets.

@domtaylor
Created June 3, 2020 14:59
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 domtaylor/2665ff381823b4f67337ada2ddd75fea to your computer and use it in GitHub Desktop.
Save domtaylor/2665ff381823b4f67337ada2ddd75fea to your computer and use it in GitHub Desktop.
const _ = require('lodash')
const path = require('path')
const { createFilePath } = require('gatsby-source-filesystem')
const { fmImagesToRelative } = require('gatsby-remark-relative-images')
const createPaginatedPages = require('gatsby-paginate')
// Remove trailing slashes unless it's only "/", then leave it as it is
const replaceTrailing = _path => (_path === `/` ? _path : _path.replace(/\/$/, ``))
exports.createPages = ({ actions, graphql }) => {
const { createPage } = actions
return graphql(`
{
allMarkdownRemark(limit: 1000) {
edges {
node {
id
fields {
slug
}
frontmatter {
tags
templateKey
}
}
}
}
}
`).then(result => {
if (result.errors) {
result.errors.forEach(e => console.error(e.toString()))
return Promise.reject(result.errors)
}
const postsAndPages = result.data.allMarkdownRemark.edges
// Post pages:
let posts = []
// Iterate through each post/page, putting all found posts (where templateKey = blog-post) into `posts`
postsAndPages.forEach(edge => {
if (_.isMatch(edge.node.frontmatter, { templateKey: 'blog-post' })) {
posts = posts.concat(edge)
}
})
createPaginatedPages({
edges: posts,
createPage: createPage,
pageTemplate: 'src/pages/blog/index.js',
pageLength: 10,
pathPrefix: 'blog',
context: {
}
})
/*
Array.from({ length: numPages }).forEach((posts, i) => {
createPage({
edges: posts,
path: i === 0 ? `/` : `/${i + 1}`,
component: path.resolve("src/pages/blog/index.js"),
context: {
limit: postsPerPage,
skip: i * postsPerPage,
posts
}
})
}) */
postsAndPages.forEach(edge => {
const id = edge.node.id
createPage({
path: edge.node.fields.slug,
tags: edge.node.frontmatter.tags,
component: path.resolve(
`src/templates/${String(edge.node.frontmatter.templateKey)}.js`
),
// additional data can be passed via context
context: {
id,
},
})
})
// Tag pages:
let tags = []
// Iterate through each post, putting all found tags into `tags`
postsAndPages.forEach(edge => {
if (_.get(edge, `node.frontmatter.tags`)) {
tags = tags.concat(edge.node.frontmatter.tags)
}
})
// Eliminate duplicate tags
tags = _.uniq(tags)
// Make tag pages
tags.forEach(tag => {
const tagPath = `/tags/${_.kebabCase(tag)}/`
createPage({
path: tagPath,
component: path.resolve(`src/templates/tags.js`),
context: {
tag,
},
})
})
})
}
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions
fmImagesToRelative(node) // convert image paths for gatsby images
if (node.internal.type === `MarkdownRemark`) {
const value = createFilePath({ node, getNode })
createNodeField({
name: `slug`,
node,
value,
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment