forked from liferay-design/liferay.design
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
250 lines (226 loc) · 4.88 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
const path = require(`path`)
const { createFilePath } = require(`gatsby-source-filesystem`)
const _ = require('lodash')
const moment = require(`moment`)
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions
if (node.internal.type === 'Mdx') {
const slug = createFilePath({ node, getNode }).replace('/markdown/', '/')
createNodeField({
node,
name: 'slug',
value: slug,
})
}
}
exports.createPages = ({ actions, graphql }) => {
const { createPage } = actions
const markdown = graphql(`
{
development: allMdx {
edges {
node {
id
fields {
slug
}
frontmatter {
tags
}
body
}
}
}
production: allMdx(filter: { frontmatter: { publish: { ne: false } } }) {
edges {
node {
id
fields {
slug
}
frontmatter {
tags
}
body
}
}
}
}
`).then(({ data, errors }) => {
if (errors) {
console.log('Error creating markdown pages in `createPages` call ==>', errors)
throw errors
}
const environment = process.env.NODE_ENV
const pages = data[environment].edges
pages.forEach(({ node }) => {
const template = node.fields.slug.split('/')[1]
const templateFile = path.resolve(
`./src/components/templates/${capFirstLetter(template)}/index.js`,
)
createPage({
path: node.fields.slug,
component: templateFile,
context: {
slug: node.fields.slug,
},
})
})
const tagTemplate = path.resolve('src/components/templates/Tags/index.js')
// Tag pages:
let tags = []
// Iterate through each post, putting all found tags into `tags`
_.each(pages, 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 => {
createPage({
path: `/tags/${_.kebabCase(tag)}/`,
component: tagTemplate,
context: {
tag,
},
})
})
})
const newsletters = graphql(`
{
mailchimp: allNewsletters(
filter: {
settings: { title: { regex: "/Newsletter/" } }
emails_sent: { gt: 20 }
}
) {
edges {
node {
archive_url
archive_html
settings {
title
subject_line
preview_text
}
emails_sent
send_time
}
}
}
}
`).then(({ data, errors }) => {
if (errors) {
console.log(
'Error creating newsletter pages in `createPages` call ==>',
errors,
)
return Promise.reject(errors)
}
const newsletters = data.mailchimp.edges
newsletters.forEach(({ node }) => {
const templateFile = path.resolve(
`./src/components/templates/Newsletters/index.js`,
)
const slug = `${moment(node.send_time).format('YYYY-MM')}`
createPage({
path: '/newsletter/' + slug,
component: templateFile,
context: {
slug: slug,
send_time: node.send_time,
},
})
})
})
return Promise.all([markdown, newsletters])
}
function capFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1)
}
// Called after every page is created.
// This section is used for client only gated routes like blueprints pages
exports.onCreatePage = async ({ page, actions }) => {
const { createPage } = actions
var pageMap = {
['blueprints']: ['blueprints'],
['handbook']: ['handbook'],
}
for (key in pageMap)
if (page.path.match(/^\/pageMap[key]/)) {
page.matchPath = '/key/*'
// Update the page.
createPage(page)
}
}
if (!process.env.MAILCHIMP_KEY) {
exports.createSchemaCustomization = ({ actions, schema }) => {
const { createTypes } = actions
const typeDefs = [
schema.buildObjectType({
fields: {
edges: '[EdgesNewsletter]',
},
interfaces: ['Node'],
name: 'Newsletter',
}),
schema.buildObjectType({
fields: {
next: 'NodeNewsletter',
node: 'NodeNewsletter',
previous: 'NodeNewsletter',
},
name: 'EdgesNewsletter',
}),
schema.buildObjectType({
fields: {
archive_html: 'String',
archive_url: 'String',
emails_sent: 'String',
send_time: {
args: { formatString: 'String' },
type: 'Date',
},
settings: 'NodeSettings',
},
name: 'NodeNewsletter',
}),
schema.buildObjectType({
fields: {
preview_text: 'String',
subject_line: 'String',
title: 'String',
},
name: 'NodeSettings',
}),
schema.buildInputObjectType({
fields: {
emails_sent: 'JSON',
send_time: 'JSON',
settings: 'JSON',
},
name: 'InputFilter',
}),
]
createTypes(typeDefs)
}
exports.createResolvers = ({ createResolvers }) => {
const resolvers = {
Query: {
allNewsletters: {
type: 'Newsletter',
args: {
filter: 'InputFilter',
sort: 'JSON',
},
resolve() {
return { edges: [] }
},
},
},
}
createResolvers(resolvers)
}
}