|
1 | | -const path = require("path"); |
2 | | -const { createFilePath } = require("gatsby-source-filesystem"); |
3 | | - |
4 | | -exports.onCreateWebpackConfig = ({ stage, actions }) => { |
5 | | - actions.setWebpackConfig({ |
6 | | - resolve: { |
7 | | - alias: { |
8 | | - "~src": path.join(path.resolve(__dirname, "src")), |
9 | | - }, |
10 | | - }, |
11 | | - }); |
12 | | -}; |
13 | | - |
14 | | -// TODO(dcramer): move frontmatter out of ApiDoc and into Frontmatter |
15 | | -exports.createSchemaCustomization = ({ actions, schema }) => { |
16 | | - const { createTypes } = actions; |
17 | | - const typeDefs = [ |
18 | | - ` |
19 | | - type MarkdownRemark implements Node { |
20 | | - frontmatter: Frontmatter |
21 | | - fields: Fields |
22 | | - } |
23 | | -
|
24 | | - type Mdx implements Node { |
25 | | - frontmatter: Frontmatter |
26 | | - fields: Fields |
27 | | - } |
28 | | -
|
29 | | - type Fields { |
30 | | - slug: String! |
31 | | - legacy: Boolean |
32 | | - } |
33 | | -
|
34 | | - type ApiParam { |
35 | | - type: String! |
36 | | - name: String! |
37 | | - description: String |
38 | | - } |
39 | | -
|
40 | | - type ApiDoc implements Node { |
41 | | - sidebar_order: Int |
42 | | - title: String! |
43 | | - fields: Fields |
44 | | -
|
45 | | - api_path: String! |
46 | | - authentication: String |
47 | | - description: String |
48 | | - example_request: String |
49 | | - example_response: String |
50 | | - method: String! |
51 | | - parameters: [ApiParam] |
52 | | - path_parameters: [ApiParam] |
53 | | - query_parameters: [ApiParam] |
54 | | - warning: String |
55 | | - } |
56 | | - `, |
57 | | - schema.buildObjectType({ |
58 | | - name: "Frontmatter", |
59 | | - fields: { |
60 | | - title: { |
61 | | - type: "String!", |
62 | | - }, |
63 | | - keywords: { |
64 | | - type: "[String!]", |
65 | | - }, |
66 | | - draft: { |
67 | | - type: "Boolean", |
68 | | - }, |
69 | | - redirect_from: { |
70 | | - type: "[String!]", |
71 | | - }, |
72 | | - noindex: { |
73 | | - type: "Boolean", |
74 | | - }, |
75 | | - sidebar_order: { |
76 | | - type: "Int", |
77 | | - resolve(source, args, context, info) { |
78 | | - // For a more generic solution, you could pick the field value from |
79 | | - // `source[info.fieldName]` |
80 | | - return source[info.fieldName] !== null |
81 | | - ? source[info.fieldName] |
82 | | - : 10; |
83 | | - }, |
84 | | - }, |
85 | | - |
86 | | - // wizard fields |
87 | | - // TODO(dcramer): move to a diff schema/type |
88 | | - support_level: { |
89 | | - type: "String", |
90 | | - }, |
91 | | - type: { |
92 | | - type: "String", |
93 | | - }, |
94 | | - doc_link: { |
95 | | - type: "String", |
96 | | - }, |
97 | | - name: { |
98 | | - type: "String", |
99 | | - }, |
100 | | - }, |
101 | | - }), |
102 | | - ]; |
103 | | - createTypes(typeDefs); |
104 | | -}; |
105 | | - |
106 | | -exports.onCreateNode = ({ |
107 | | - node, |
108 | | - actions, |
109 | | - getNode, |
110 | | - createContentDigest, |
111 | | - createNodeId, |
112 | | -}) => { |
113 | | - const { createNodeField, createNode } = actions; |
114 | | - if (node.internal.type === "Mdx" || node.internal.type === "MarkdownRemark") { |
115 | | - const value = createFilePath({ node, getNode }); |
116 | | - createNodeField({ |
117 | | - name: "slug", |
118 | | - node, |
119 | | - value, |
120 | | - }); |
121 | | - |
122 | | - createNodeField({ |
123 | | - name: "legacy", |
124 | | - node, |
125 | | - value: value.indexOf("/clients/") === 0, |
126 | | - }); |
127 | | - } else if (node.internal.type === "ApiDoc") { |
128 | | - const value = createFilePath({ node, getNode }); |
129 | | - createNodeField({ |
130 | | - name: "slug", |
131 | | - node, |
132 | | - value: `/api${value}`, |
133 | | - }); |
134 | | - createNodeField({ |
135 | | - name: "legacy", |
136 | | - node, |
137 | | - value: false, |
138 | | - }); |
139 | | - |
140 | | - const markdownNode = { |
141 | | - id: createNodeId(`${node.id} >>> MarkdownRemark`), |
142 | | - children: [], |
143 | | - parent: node.id, |
144 | | - internal: { |
145 | | - content: node.description, |
146 | | - contentDigest: createContentDigest(node.description), |
147 | | - mediaType: `text/markdown`, |
148 | | - type: `ApiDocMarkdown`, |
149 | | - }, |
150 | | - }; |
151 | | - createNode(markdownNode); |
152 | | - |
153 | | - createNodeField({ |
154 | | - node, |
155 | | - name: "description___NODE", |
156 | | - value: markdownNode.id, |
157 | | - }); |
158 | | - } |
159 | | -}; |
160 | | - |
161 | | -exports.createPages = async function({ actions, graphql, reporter }) { |
162 | | - const createApi = async () => { |
163 | | - const { data, errors } = await graphql(` |
164 | | - query { |
165 | | - allApiDoc { |
166 | | - nodes { |
167 | | - id |
168 | | - title |
169 | | - fields { |
170 | | - slug |
171 | | - } |
172 | | - } |
173 | | - } |
174 | | - } |
175 | | - `); |
176 | | - |
177 | | - if (errors) { |
178 | | - reporter.panicOnBuild('🚨 ERROR: Loading "createApi" query'); |
179 | | - } |
180 | | - const component = require.resolve(`./src/components/pages/api.js`); |
181 | | - await Promise.all( |
182 | | - data.allApiDoc.nodes.map(async node => { |
183 | | - actions.createPage({ |
184 | | - path: node.fields.slug, |
185 | | - component, |
186 | | - context: { |
187 | | - id: node.id, |
188 | | - title: node.title, |
189 | | - }, |
190 | | - }); |
191 | | - }) |
192 | | - ); |
193 | | - }; |
194 | | - |
195 | | - const createDocs = async () => { |
196 | | - const { data, errors } = await graphql(` |
197 | | - query { |
198 | | - allFile(filter: { sourceInstanceName: { eq: "docs" } }) { |
199 | | - nodes { |
200 | | - id |
201 | | - childMarkdownRemark { |
202 | | - frontmatter { |
203 | | - title |
204 | | - } |
205 | | - fields { |
206 | | - slug |
207 | | - } |
208 | | - } |
209 | | - childMdx { |
210 | | - frontmatter { |
211 | | - title |
212 | | - } |
213 | | - fields { |
214 | | - slug |
215 | | - } |
216 | | - } |
217 | | - } |
218 | | - } |
219 | | - } |
220 | | - `); |
221 | | - |
222 | | - if (errors) { |
223 | | - reporter.panicOnBuild('🚨 ERROR: Loading "createDocs" query'); |
224 | | - } |
225 | | - const component = require.resolve(`./src/components/pages/doc.js`); |
226 | | - await Promise.all( |
227 | | - data.allFile.nodes.map(async node => { |
228 | | - const child = node.childMarkdownRemark || node.childMdx; |
229 | | - if (child && child.fields) { |
230 | | - actions.createPage({ |
231 | | - path: child.fields.slug, |
232 | | - component, |
233 | | - context: { |
234 | | - id: node.id, |
235 | | - title: child.frontmatter.title, |
236 | | - }, |
237 | | - }); |
238 | | - } |
239 | | - }) |
240 | | - ); |
241 | | - }; |
242 | | - |
243 | | - await Promise.all([createApi(), createDocs()]); |
244 | | -}; |
| 1 | +exports.onCreateNode = require("./src/gatsby/onCreateNode").default; |
| 2 | +exports.onCreateWebpackConfig = require("./src/gatsby/onCreateWebpackConfig").default; |
| 3 | +exports.createPages = require("./src/gatsby/createPages").default; |
| 4 | +exports.createSchemaCustomization = require("./src/gatsby/createSchemaCustomization").default; |
0 commit comments