-
Notifications
You must be signed in to change notification settings - Fork 8
/
gatsby-node.js
77 lines (71 loc) · 2.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
const axios = require('axios');
const get = endpoint =>
axios.get(`https://api.nextrelease.io${endpoint}/?sort=-created`, {
headers: {
Authorization: 'Token ' + process.env.API_TOKEN,
'Content-Type': 'application/vnd.api+json',
'X-Organization': process.env.X_ORGANIZATION
}
});
const getPosts = names =>
Promise.all(
names.map(async name => {
const { data: post } = await get(`/${name}`);
if (typeof post.results === 'undefined') {
return post;
} else {
return post.results;
}
})
).catch(error => {
return [];
});
exports.createPages = async ({ actions: { createPage }, graphql }) => {
const pages = ['blog', 'product', 'kb', 'performance-notes', 'hiring'];
const posts = await getPosts(['reddit_releases']);
let releases = posts[0];
createPage({
path: `/reddit-community/showcase`,
component: require.resolve(
'./src/templates/reddit-community/showcase.js'
),
context: { releases }
});
const community_stats = await getPosts(['community_stats']);
let stats = community_stats[0];
createPage({
path: `/communities`,
component: require.resolve('./src/templates/communities.js'),
context: { stats }
});
pages.forEach(page => {
PageMaker(page, createPage, graphql);
});
};
const PageMaker = async (page, createPage, graphql) => {
const results = await graphql(`
{
allMarkdownRemark(filter: {fileAbsolutePath: {regex: "/(${page})/"}}) {
edges {
node {
id
frontmatter {
path
}
}
}
}
}
`).then(res => {
res.data.allMarkdownRemark.edges.forEach(function({ node }) {
const { path, image } = node.frontmatter;
createPage({
path,
component: require.resolve(`./src/templates/${page}.js`),
context: {
id: node.id
}
});
});
});
};