forked from getsentry/sentry-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.js
235 lines (218 loc) · 5.15 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
const path = require("path");
const { createFilePath } = require("gatsby-source-filesystem");
exports.onCreateWebpackConfig = ({ stage, actions }) => {
actions.setWebpackConfig({
resolve: {
alias: {
"~src": path.join(path.resolve(__dirname, "src")),
},
},
});
};
// TODO(dcramer): move frontmatter out of ApiDoc and into Frontmatter
exports.createSchemaCustomization = ({ actions, schema }) => {
const { createTypes } = actions;
const typeDefs = [
`
type MarkdownRemark implements Node {
frontmatter: Frontmatter
fields: Fields
}
type Mdx implements Node {
frontmatter: Frontmatter
fields: Fields
}
type Fields {
slug: String!
legacy: Boolean
}
type ApiParam {
type: String!
name: String!
description: String
}
type ApiDoc implements Node {
sidebar_order: Int
title: String!
fields: Fields
api_path: String!
authentication: String
description: String
example_request: String
example_response: String
method: String!
parameters: [ApiParam]
path_parameters: [ApiParam]
query_parameters: [ApiParam]
warning: String
}
`,
schema.buildObjectType({
name: "Frontmatter",
fields: {
title: {
type: "String!",
},
keywords: {
type: "[String!]",
},
draft: {
type: "Boolean",
},
sidebar_order: {
type: "Int",
resolve(source, args, context, info) {
// For a more generic solution, you could pick the field value from
// `source[info.fieldName]`
return source[info.fieldName] !== null
? source[info.fieldName]
: 10;
},
},
// wizard fields
// TODO(dcramer): move to a diff schema/type
support_level: {
type: "String",
},
type: {
type: "String",
},
doc_link: {
type: "String",
},
name: {
type: "String",
},
},
}),
];
createTypes(typeDefs);
};
exports.onCreateNode = ({
node,
actions,
getNode,
createContentDigest,
createNodeId,
}) => {
const { createNodeField, createNode } = actions;
if (node.internal.type === "Mdx" || node.internal.type === "MarkdownRemark") {
const value = createFilePath({ node, getNode });
createNodeField({
name: "slug",
node,
value,
});
createNodeField({
name: "legacy",
node,
value: value.indexOf("/clients/") === 0,
});
} else if (node.internal.type === "ApiDoc") {
const value = createFilePath({ node, getNode });
createNodeField({
name: "slug",
node,
value: `/api${value}`,
});
createNodeField({
name: "legacy",
node,
value: false,
});
const markdownNode = {
id: createNodeId(`${node.id} >>> MarkdownRemark`),
children: [],
parent: node.id,
internal: {
content: node.description,
contentDigest: createContentDigest(node.description),
mediaType: `text/markdown`,
type: `ApiDocMarkdown`,
},
};
createNode(markdownNode);
createNodeField({
node,
name: "description___NODE",
value: markdownNode.id,
});
}
};
exports.createPages = async function({ actions, graphql, reporter }) {
const createApi = async () => {
const { data, errors } = await graphql(`
query {
allApiDoc {
nodes {
id
title
fields {
slug
}
}
}
}
`);
if (errors) {
reporter.panicOnBuild('🚨 ERROR: Loading "createApi" query');
}
const component = require.resolve(`./src/components/pages/api.js`);
data.allApiDoc.nodes.forEach(node => {
actions.createPage({
path: node.fields.slug,
component,
context: {
id: node.id,
title: node.title,
},
});
});
};
const createDocs = async () => {
const { data, errors } = await graphql(`
query {
allFile(filter: { sourceInstanceName: { eq: "docs" } }) {
nodes {
id
childMarkdownRemark {
frontmatter {
title
}
fields {
slug
}
}
childMdx {
frontmatter {
title
}
fields {
slug
}
}
}
}
}
`);
if (errors) {
reporter.panicOnBuild('🚨 ERROR: Loading "createDocs" query');
}
const component = require.resolve(`./src/components/pages/doc.js`);
data.allFile.nodes.forEach(node => {
const child = node.childMarkdownRemark || node.childMdx;
if (child && child.fields) {
actions.createPage({
path: child.fields.slug,
component,
context: {
id: node.id,
title: child.frontmatter.title,
},
});
}
});
};
await createApi();
await createDocs();
};