-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
80 lines (75 loc) · 1.96 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
const path = require('path')
const buildPages = (
{ boundActionCreators, graphql },
component,
regex,
fields = [],
contextFields = []
) => {
const { createPage } = boundActionCreators
return graphql(`
{
allMarkdownRemark(
filter: { frontmatter: { path: { regex: "${regex}" } } }
sort: { order: DESC, fields: [frontmatter___date] }
limit: 1000
) {
edges {
node {
frontmatter {
${fields.join(' ')}
}
}
}
}
}
`).then(result => {
if (result.errors) {
return Promise.reject(result.errors)
}
try {
result.data.allMarkdownRemark.edges.forEach(({ node }) => {
const props = {}
fields.forEach(fieldName => {
props[fieldName] = node.frontmatter[fieldName]
})
const context = {} // additional data can be passed via context
contextFields.forEach(fieldName => {
const fieldValue = node.frontmatter[fieldName]
context[fieldName] = new RegExp(
fieldValue.substr(fieldValue.lastIndexOf('/') + 1)
)
})
createPage({
component,
context,
...props,
})
})
} catch (e) {
console.log(e)
}
})
}
exports.createPages = args => {
const bookTemplate = path.resolve(`src/templates/book.js`)
const exampleTemplate = path.resolve(`src/templates/example.js`)
const newsTemplate = path.resolve(`src/templates/news.js`)
const tutorialTemplate = path.resolve(`src/templates/tutorial.js`)
return Promise.all([
buildPages(
args,
bookTemplate,
/\/books\//,
['path', 'title', 'cover'],
['cover']
),
buildPages(args, exampleTemplate, /\/examples\//, ['path', 'title']),
buildPages(args, newsTemplate, /\/news\//, ['path', 'title']),
buildPages(args, tutorialTemplate, /\/tutorials\//, [
'path',
'date',
'title',
]),
])
}