forked from gatsbyjs/gatsby
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
125 lines (117 loc) Β· 4.12 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
const path = require('path')
exports.onCreateNode = ({ node, boundActionCreators, getNode }) => {
const { createNodeField } = boundActionCreators
let slug
if (node.internal.type === `MarkdownRemark` || node.internal.type === `JSFrontmatter`) {
const fileNode = getNode(node.parent)
const parsedFilePath = path.parse(fileNode.relativePath)
if (parsedFilePath.name !== `index` && parsedFilePath.dir !== ``) {
slug = `/${parsedFilePath.dir}/${parsedFilePath.name}/`
} else if (parsedFilePath.dir === ``) {
slug = `/${parsedFilePath.name}/`
} else {
slug = `/${parsedFilePath.dir}/`
}
// Add slug as a field on the node.
createNodeField({ node, name: `slug`, value: slug })
}
}
exports.createPages = ({ graphql, boundActionCreators }) => {
const { createPage } = boundActionCreators
return new Promise((resolve, reject) => {
const pages = []
const markdownTemplate = path.resolve("src/templates/markdown.js")
const jsTemplate = path.resolve("src/templates/javascript.js")
// Query for all markdown "nodes" and for the slug we previously created.
resolve(
graphql(
`
{
allMarkdownRemark {
edges {
node {
frontmatter {
layoutType
path
}
fields {
slug
}
}
}
}
allJsFrontmatter {
edges {
node {
fileAbsolutePath
data {
layoutType
path
}
fields {
slug
}
}
}
}
}
`
).then(result => {
if (result.errors) {
console.log(result.errors)
console.log(result)
reject(result.errors)
}
// Create from markdown
result.data.allMarkdownRemark.edges.forEach(edge => {
let frontmatter = edge.node.frontmatter;
// ideally we would want to use layoutType to
// decide which (nested) layout to use, but
// gatsby currently doesnt support this.
if (frontmatter.layoutType === 'post' ||
frontmatter.layoutType === 'page') {
createPage({
path: frontmatter.path, // required
component: markdownTemplate,
context: {
layoutType: frontmatter.layoutType,
slug: edge.node.fields.slug,
},
})
}
})
// Create pages from javascript
// Gatsby will, by default, createPages for javascript in the
// /pages directory. We purposely don't have a folder with this name
// so that we can go full manual mode.
result.data.allJsFrontmatter.edges.forEach(edge => {
let frontmatter = edge.node.data;
// see above
if (frontmatter.layoutType === 'post' ||
frontmatter.layoutType === 'page') {
createPage({
path: frontmatter.path, // required
// Note, we can't have a template, but rather require the file directly.
// Templates are for converting non-react into react. jsFrontmatter
// picks up all of the javascript files. We have only written these in react.
component: path.resolve(edge.node.fileAbsolutePath),
context: {
layoutType: frontmatter.layoutType,
slug: edge.node.fields.slug,
},
})
} else if (edge.node.fields.slug === '/index/') {
createPage({
path: '/', // required, we don't have frontmatter for this page hence separate if()
component: path.resolve(edge.node.fileAbsolutePath),
context: {
slug: edge.node.fields.slug,
},
})
}
})
return
})
)
})
}