-
Notifications
You must be signed in to change notification settings - Fork 438
/
gatsby-node.js
204 lines (174 loc) · 5.9 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
// eslint-disable-next-line import/no-extraneous-dependencies
require('dotenv').config({
path: `.env.${process.env.NODE_ENV}`,
});
const path = require('path');
const { createFilePath } = require(`gatsby-source-filesystem`);
const sourceDXData = require('./src/utils/source-dx-data');
const config = require('./gatsby-config');
const defaultLanguage = 'en';
const defaultFramework = 'react';
// slug starts and ends with '/' so parts[0] and parts[-1] will be empty
const getSlugParts = (slug) => slug.split('/').filter((p) => !!p);
const onCreateGuideNode = ({ actions, node, slug }) => {
const { createNodeField } = actions;
const parts = getSlugParts(slug);
const [guide] = parts;
createNodeField({ node, name: 'guide', value: guide });
createNodeField({ node, name: 'slug', value: slug });
createNodeField({ node, name: 'permalink', value: `${config.siteMetadata.permalink}${slug}` });
createNodeField({ node, name: 'pageType', value: 'guide' });
};
const onCreateNonFrameworkChapterNode = ({ actions, node, slug }) => {
const { createNodeField } = actions;
const parts = getSlugParts(slug);
const [guide, language, chapter] = parts;
createNodeField({ node, name: 'guide', value: guide });
createNodeField({ node, name: 'slug', value: slug });
createNodeField({ node, name: 'permalink', value: `${config.siteMetadata.permalink}${slug}` });
createNodeField({ node, name: 'language', value: language });
createNodeField({ node, name: 'chapter', value: chapter });
createNodeField({ node, name: 'pageType', value: 'chapter' });
createNodeField({ node, name: 'isDefaultTranslation', value: language === defaultLanguage });
};
const onCreateFrameworkChapterNode = ({ actions, node, slug }) => {
const { createNodeField } = actions;
const parts = getSlugParts(slug);
const [guide, framework, language, chapter] = parts;
createNodeField({ node, name: 'guide', value: guide });
createNodeField({ node, name: 'slug', value: slug });
createNodeField({ node, name: 'permalink', value: `${config.siteMetadata.permalink}${slug}` });
createNodeField({ node, name: 'framework', value: framework });
createNodeField({ node, name: 'language', value: language });
createNodeField({ node, name: 'chapter', value: chapter });
createNodeField({ node, name: 'pageType', value: 'chapter' });
createNodeField({
node,
name: 'isDefaultTranslation',
value: language === defaultLanguage && framework === defaultFramework,
});
const { siteMetadata } = config;
const { storybookVersion, tutorialStatus } = siteMetadata;
const currentGuide = tutorialStatus[guide];
const currentFramework = currentGuide[framework];
// checks if the framework is defined
if (!currentFramework) {
return;
}
// gets and checks if the translation is defined
const currentVersion = currentFramework[language];
if (!currentVersion) {
return;
}
createNodeField({ node, name: 'tutorialUpToDate', value: storybookVersion === currentVersion });
};
exports.onCreateNode = ({ node, getNode, actions }) => {
if (node.internal.type === 'MarkdownRemark') {
const slug = createFilePath({
node,
getNode,
basePath: 'content',
});
// slug starts and ends with '/' so parts[0] and parts[-1] will be empty
const parts = slug.split('/').filter((p) => !!p);
if (parts.length === 1) {
onCreateGuideNode({ actions, node, slug });
return;
}
if (parts.length === 3) {
onCreateNonFrameworkChapterNode({ actions, node, slug });
return;
}
if (parts.length === 4) {
onCreateFrameworkChapterNode({ actions, node, slug });
return;
}
throw new Error(`Unexpected node path of length !== 1 || !== 4: ${slug}`);
}
};
const createChapterPage = ({ createPage, node }) => {
const { guide, slug, framework, language, chapter, pageType, isDefaultTranslation, permalink } =
node.fields;
createPage({
path: slug,
component: path.resolve(`./src/components/screens/ChapterScreen/index.js`),
context: {
// Data passed to context is available in page queries as GraphQL variables.
guide,
slug,
framework,
language,
chapter,
pageType,
permalink,
isDefaultTranslation,
},
});
};
const createGuidePage = ({ createPage, node }) => {
const { guide, slug, pageType, permalink } = node.fields;
createPage({
path: slug,
component: path.resolve(`./src/components/screens/GuideScreen/index.js`),
context: {
// Data passed to context is available in page queries as GraphQL variables.
guide,
slug,
permalink,
pageType,
},
});
};
exports.createPages = ({ graphql, actions }) => {
const { createPage } = actions;
return new Promise((resolve) => {
graphql(`
{
pages: allMarkdownRemark {
edges {
node {
fields {
guide
slug
framework
language
chapter
pageType
}
}
}
}
}
`).then(
({
data: {
pages: { edges },
},
}) => {
edges.forEach(({ node }) => {
const { pageType, slug } = node.fields;
if (pageType === 'chapter') {
createChapterPage({ createPage, node });
return;
}
if (pageType === 'guide') {
createGuidePage({ createPage, node });
return;
}
throw new Error(`Unexpected pageType !== 'chapter' || !== 'guide': ${slug}`);
});
resolve();
}
);
});
};
exports.sourceNodes = async ({ actions, createNodeId, createContentDigest }) => {
// Skip DX data retrieval if SKIP_DX_DATA environment variable is set
const isSkipDataEnabled = process.env.SKIP_DX_DATA;
await sourceDXData({
actions,
createNodeId,
createContentDigest,
isDxDataEnabled: isSkipDataEnabled,
});
};