This repository has been archived on 2026-10-23. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
dns2019/gatsby-node.js
T
2019-01-17 03:02:17 +01:00

223 lines
5.4 KiB
JavaScript

const _ = require('lodash')
const path = require('path')
const { createFilePath } = require('gatsby-source-filesystem')
const { paginate } = require('gatsby-awesome-pagination')
const getOnlyPublished = edges =>
_.filter(edges, ({ node }) => node.status === 'publish')
exports.createPages = ({ actions, graphql }) => {
const { createPage } = actions
return graphql(`
{
allWordpressPage {
edges {
node {
id
slug
status
}
}
}
}
`)
.then(result => {
if (result.errors) {
result.errors.forEach(e => console.error(e.toString()))
return Promise.reject(result.errors)
}
const pageTemplate = path.resolve(`./src/templates/page.js`)
// Only publish pages with a `status === 'publish'` in production. This
// excludes drafts, future posts, etc. They will appear in development,
// but not in a production build.
const allPages = result.data.allWordpressPage.edges
const pages =
process.env.NODE_ENV === 'production'
? getOnlyPublished(allPages)
: allPages
// Call `createPage()` once per WordPress page
_.each(pages, ({ node: page }) => {
createPage({
path: `/${page.slug}/`,
component: pageTemplate,
context: {
id: page.id,
},
})
})
})
.then(() => {
return graphql(`
{
allWordpressPost {
edges {
node {
id
slug
status
}
}
}
}
`)
})
.then(result => {
if (result.errors) {
result.errors.forEach(e => console.error(e.toString()))
return Promise.reject(result.errors)
}
const postTemplate = path.resolve(`./src/templates/post.js`)
const blogTemplate = path.resolve(`./src/templates/blog.js`)
// In production builds, filter for only published posts.
const allPosts = result.data.allWordpressPost.edges
const posts =
process.env.NODE_ENV === 'production'
? getOnlyPublished(allPosts)
: allPosts
// Iterate over the array of posts
_.each(posts, ({ node: post }) => {
// Create the Gatsby page for this WordPress post
createPage({
path: `/${post.slug}/`,
component: postTemplate,
context: {
id: post.id,
},
})
})
// Create a paginated blog, e.g., /, /page/2, /page/3
paginate({
createPage,
items: posts,
itemsPerPage: 10,
pathPrefix: ({ pageNumber }) => (pageNumber === 0 ? `/` : `/page`),
component: blogTemplate,
})
})
.then(() => {
return graphql(`
{
allWordpressCategory(filter: { count: { gt: 0 } }) {
edges {
node {
id
name
slug
}
}
}
}
`)
})
.then(result => {
if (result.errors) {
result.errors.forEach(e => console.error(e.toString()))
return Promise.reject(result.errors)
}
const categoriesTemplate = path.resolve(`./src/templates/category.js`)
// Create a Gatsby page for each WordPress Category
_.each(result.data.allWordpressCategory.edges, ({ node: cat }) => {
createPage({
path: `/categories/${cat.slug}/`,
component: categoriesTemplate,
context: {
name: cat.name,
slug: cat.slug,
},
})
})
})
.then(() => {
return graphql(`
{
allWordpressTag(filter: { count: { gt: 0 } }) {
edges {
node {
id
name
slug
}
}
}
}
`)
})
.then(result => {
if (result.errors) {
result.errors.forEach(e => console.error(e.toString()))
return Promise.reject(result.errors)
}
const tagsTemplate = path.resolve(`./src/templates/tag.js`)
// Create a Gatsby page for each WordPress tag
_.each(result.data.allWordpressTag.edges, ({ node: tag }) => {
createPage({
path: `/tags/${tag.slug}/`,
component: tagsTemplate,
context: {
name: tag.name,
slug: tag.slug,
},
})
})
})
.then(() => {
return graphql(`
{
allWordpressWpUsers {
edges {
node {
id
slug
}
}
}
}
`)
})
.then(result => {
if (result.errors) {
result.errors.forEach(e => console.error(e.toString()))
return Promise.reject(result.errors)
}
const authorTemplate = path.resolve(`./src/templates/author.js`)
_.each(result.data.allWordpressWpUsers.edges, ({ node: author }) => {
createPage({
path: `/author/${author.slug}`,
component: authorTemplate,
context: {
id: author.id,
},
})
})
})
}
exports.onCreateNode = ({ node, actions, getNode }) => {
const { createNodeField } = actions
if (node.internal.type === `MarkdownRemark`) {
const value = createFilePath({ node, getNode })
createNodeField({
name: `slug`,
node,
value,
})
}
}