-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
183 lines (164 loc) · 4.1 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
const { createFilePath } = require(`gatsby-source-filesystem`);
exports.onCreateNode = ({ node, getNode, actions }) => {
const { createNodeField } = actions;
if (node.internal.type === `MarkdownRemark`) {
const parent = getNode(node.parent);
const collection = parent.sourceInstanceName;
// Create slug for a story
if (collection === 'story') {
const slug = createFilePath({
node,
getNode,
basePath: `content/stories`,
trailingSlash: false
});
const base = slug.split(/-(.+)/)[1];
const date = new Date(node.frontmatter.date);
let value = `/${date.getFullYear()}`;
value += `/${(date.getMonth() + 1).toString().padStart(2, '0')}`;
value += `/${date.getDate().toString().padStart(2, '0')}`;
value += `/${base}`;
createNodeField({
node,
name: `slug`,
value
});
createNodeField({
node,
name: `o_slug`,
value: `${slug.substring(1)}/index`
});
}
// Create slug for an activity
if (collection === 'activity') {
const slug = createFilePath({
node,
getNode,
basePath: `content/activities`,
trailingSlash: false
});
createNodeField({
node,
name: `slug`,
value: `/activities${slug}`
});
createNodeField({
node,
name: `o_slug`,
value: `${slug.substring(1)}/index`
});
}
createNodeField({
node,
name: 'collection',
value: collection
});
}
};
exports.createPages = async ({ actions, graphql, reporter }) => {
const { createPage } = actions;
const Story = require.resolve(`./src/templates/Story.jsx`);
const Activity = require.resolve(`./src/templates/Activity.jsx`);
const result = await graphql(`
{
stories: allMarkdownRemark(filter: { fields: { collection: { eq: "story" } } }) {
edges {
node {
fields {
slug
}
}
}
}
activities: allMarkdownRemark(filter: { fields: { collection: { eq: "activity" } } }) {
edges {
node {
fields {
slug
}
}
}
}
}
`);
// Handle errors
if (result.errors) {
reporter.panicOnBuild(`Error while running GraphQL query.`);
return;
}
const { stories, activities } = result.data;
stories.edges.forEach(({ node }) => {
createPage({
path: node.fields.slug,
component: Story,
context: {
// additional data can be passed via context
slug: node.fields.slug
}
});
});
activities.edges.forEach(({ node }) => {
createPage({
path: node.fields.slug,
component: Activity,
context: {
// additional data can be passed via context
slug: node.fields.slug
}
});
});
};
// Define some fields to avoid breaking the website
exports.createSchemaCustomization = ({ actions }) => {
const { createTypes } = actions;
const typeDefs = `
type MarkdownRemark implements Node {
frontmatter: Frontmatter
}
type Frontmatter {
contact_info: ContactInfo
members: [Members]
tags: [String]
featured_image: FeaturedImage
header: Header
team: [Members]
directors: [Members]
stories: Stories
volunteers: Volunteers
activities: Activities
}
type Volunteers {
featured_image: FeaturedImage
}
type Header {
featured_image: FeaturedImage
}
type Activities {
featured_activities: [MarkdownRemark] @link(by: "fields.o_slug")
}
type Stories {
featured_image: FeaturedImage
featured_stories: [MarkdownRemark] @link(by: "fields.o_slug")
}
type Members {
id: ID
task: String
contact_info: ContactInfo
}
type ContactInfo {
twitter: String
linkedin: String
socials: Socials
}
type Socials {
github: String
twitter: String
facebook: String
linkedin: String
}
type FeaturedImage {
alt: String
}
`;
createTypes(typeDefs);
};