-
Notifications
You must be signed in to change notification settings - Fork 1
/
gatsby-node.js
83 lines (73 loc) · 2.16 KB
/
gatsby-node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const { resolve } = require(`path`)
const path = require(`path`)
const glob = require(`glob`)
const chunk = require(`lodash/chunk`)
const { dd } = require(`dumper.js`)
const helpers = require(`gatsby-source-wordpress-experimental/steps/source-nodes/helpers`)
const getTemplates = () => {
const sitePath = path.resolve(`./`)
return glob.sync(`./src/templates/**/*.js`, { cwd: sitePath })
}
exports.createPages = async ({ actions, graphql, reporter }) => {
const templates = getTemplates()
const {
data: {
wp: { generalSettings },
allWpContentNode: { nodes: contentNodes },
},
} = await graphql(/* GraphQL */ `
query ALL_CONTENT_NODES {
wp {
generalSettings {
title
url
}
}
allWpContentNode(
sort: { fields: modifiedGmt, order: DESC }
filter: { nodeType: { ne: "MediaItem" } }
) {
nodes {
nodeType
uri
id
}
}
}
`)
const contentTypeTemplateDirectory = `./src/templates/single/`
const contentTypeTemplates = templates.filter((path) =>
path.includes(contentTypeTemplateDirectory)
)
await Promise.all(
contentNodes.map(async (node, i) => {
const { nodeType, uri, id } = node
const nodesTypeName = nodeType.charAt(0).toLowerCase() + nodeType.slice(1)
const templatePath = `${contentTypeTemplateDirectory}${nodesTypeName}.js`
const contentTypeTemplate = contentTypeTemplates.find(
(path) => path === templatePath
)
if (!contentTypeTemplate) {
dd(node)
reporter.log(``)
reporter.log(``)
reporter.panic(
`[using-gatsby-source-wordpress] No template found at ${templatePath}\nfor single ${nodesTypeName} ${
node.id
} with path ${
node.uri
}\n\nAvailable templates:\n${contentTypeTemplates.join(`\n`)}`
)
}
await actions.createPage({
component: resolve(contentTypeTemplate),
path: uri,
context: {
id,
nextPage: (contentNodes[i + 1] || {}).id,
previousPage: (contentNodes[i - 1] || {}).id,
},
})
})
)
}