-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
104 lines (89 loc) · 2.46 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
const path = require(`path`);
const { fetchPosts, fetchProjects, fetchProfile, fetchReadme } = require('./gatsby/services/github');
const createProfile = (profile, createNode, createContentDigest) => {
createNode({
id: `${profile.id}`,
name: profile.name,
username: profile.login,
image: profile.avatar_url,
bio: profile.bio,
website: profile.blog,
location: profile.location,
twitter: profile.twitter_username,
background: '',
internal: {
type: `profile`,
contentDigest: createContentDigest(profile),
},
});
};
const createAbout = (about, createNode, createContentDigest) => {
createNode({
id: `readme`,
html: about,
internal: {
type: `about`,
contentDigest: createContentDigest(about),
},
});
};
exports.sourceNodes = async ({ actions: { createNode }, createContentDigest }) => {
const { GATSBY_GH_ORGANISATION, GATSBY_GH_REPOSITORY } = process.env;
const profile = await fetchProfile(GATSBY_GH_ORGANISATION);
const readme = await fetchReadme(GATSBY_GH_ORGANISATION);
const posts = await fetchPosts(GATSBY_GH_ORGANISATION, GATSBY_GH_REPOSITORY);
const projects = await fetchProjects(GATSBY_GH_ORGANISATION);
createAbout(readme, createNode, createContentDigest);
createProfile(profile, createNode, createContentDigest);
posts.forEach((post) => {
const { id, ...rest } = post;
createNode({
id: `${id}`,
parent: null,
children: [],
internal: {
type: `post`,
contentDigest: createContentDigest(post),
},
...rest,
});
});
projects.forEach((project, index) => {
const { id, ...rest } = project;
createNode({
id: `${index}-${id}`,
parent: null,
internal: {
type: `project`,
contentDigest: createContentDigest(project),
},
...rest,
});
});
};
exports.createPages = async ({ actions, graphql, reporter }) => {
const { createPage } = actions;
const blogPostTemplate = path.resolve(`src/templates/post.jsx`);
const result = await graphql(`
{
allPost {
nodes {
title
path
}
}
}
`);
// Handle errors
if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`);
return;
}
result.data.allPost.nodes.forEach((post) => {
createPage({
path: post.path,
component: blogPostTemplate,
context: {}, // additional data can be passed via context
});
});
};