@@ -32,14 +32,12 @@ exports.onCreatePage = async ({ page, actions }) => {
32
32
const languageNameSlug = pathArr [ languageNameSlugIndex ]
33
33
const languageName = slugMap [ languageNameSlug ]
34
34
codeData . Languages = codeData . Languages || { }
35
- codeData . Languages [ languageName ] =
36
- codeData . Languages [ languageName ] || { }
35
+ codeData . Languages [ languageName ] = codeData . Languages [ languageName ] || { }
37
36
38
37
const categoryNameSlugIndex = languageSupportDirIndex + 2
39
38
const categoryNameSlug = pathArr [ categoryNameSlugIndex ]
40
39
const categoryName = slugMap [ categoryNameSlug ]
41
- codeData . Languages [ languageName ] [ categoryName ] =
42
- codeData . Languages [ languageName ] [ categoryName ] || [ ]
40
+ codeData . Languages [ languageName ] [ categoryName ] = codeData . Languages [ languageName ] [ categoryName ] || [ ]
43
41
codeData . Languages [ languageName ] [ categoryName ] . push ( {
44
42
name,
45
43
description,
@@ -157,150 +155,183 @@ exports.createPages = async ({ graphql, actions }) => {
157
155
throw result . errors
158
156
}
159
157
160
- const { edges } = result . data . allMarkdownRemark
158
+ const markdownPages = result . data . allMarkdownRemark . edges
161
159
162
- let sideBardata = { }
160
+ // foundation: [
161
+ // {
162
+ // fileAbsolutePath: '/graphql/graphql.github.io/src/content/foundation/About.md',
163
+ // parent: {},
164
+ // frontmatter: {},
165
+ // id: '1d502d5e-3453-56cf-ad9a-7f6bfb68d9ba'
166
+ // },
167
+ // ...
168
+ // ]
169
+ // }
163
170
let pagesGroupedByFolder = { }
164
- const allPages = [ ]
165
171
166
- await Promise . all (
167
- edges . map ( async ( { node } ) => {
168
- const {
169
- frontmatter : { permalink, next, sidebarTitle } ,
170
- parent : { relativeDirectory, sourceInstanceName } ,
171
- } = node
172
+ // {
173
+ // foundation: [
174
+ // { name: 'foundation', links: [{"fileAbsolutePath":"/graphql/graphql.github.io/src/content/foundation/About.md","parent":{"relativeDirectory":"foundation","sourceInstanceName":"content"},"frontmatter":{"title":"What is the GraphQL Foundation?","permalink":"/foundation/","next":"/foundation/join/","category":"GraphQL Foundation","sublinks":null,"sidebarTitle":"About the Foundation","date":null},"id":"1d502d5e-3453-56cf-ad9a-7f6bfb68d9ba" }] },
175
+ // { name: 'GraphQL Foundation', links: [Array] }
176
+ // ],
177
+ // Note that this is mutated
178
+ let sideBardata = { }
172
179
173
- if (
174
- sourceInstanceName !== "content" ||
175
- relativeDirectory . includes ( "code/" )
176
- ) {
177
- return
178
- }
179
180
180
- if ( ! pagesGroupedByFolder [ relativeDirectory ] ) {
181
- pagesGroupedByFolder = {
182
- ...pagesGroupedByFolder ,
183
- [ relativeDirectory ] : [ node ] ,
184
- }
185
- } else {
186
- pagesGroupedByFolder = {
187
- ...pagesGroupedByFolder ,
188
- [ relativeDirectory ] : [
189
- ...pagesGroupedByFolder [ relativeDirectory ] ,
190
- node ,
191
- ] ,
192
- }
193
- }
194
- allPages . push ( {
195
- permalink,
196
- relativeDirectory,
197
- sidebarTitle,
198
- nextPermalink : next ,
199
- sourcePath : path . relative ( __dirname , node . fileAbsolutePath ) ,
200
- } )
201
- } )
202
- )
181
+ // Sidebar items to add which don't come from markdown
182
+ const additionalSidebarItems = {
183
+ foundation : [ {
184
+ name : "GraphQL Foundation" ,
185
+ links : [ { frontmatter : { sidebarTitle :"Foundation Members" , title : "Foundation Members" , permalink : "/foundation/members/" , date : null , category : "GraphQL Foundation" } } ]
186
+ } ]
187
+ }
203
188
204
- await Promise . all (
205
- Object . entries ( pagesGroupedByFolder ) . map ( async ( [ folder , pages ] ) => {
206
- let pagesByUrl = { }
207
- let previousPagesMap = { }
208
- let pagesByDate = pages . sort ( ( a , b ) => {
209
- const aDate = new Date ( a . frontmatter . date || Date . now ( ) )
210
- const bDate = new Date ( b . frontmatter . date || Date . now ( ) )
211
- if ( aDate > bDate ) {
212
- return - 1
213
- } else if ( aDate < bDate ) {
214
- return 1
215
- }
216
- return 0
217
- } )
189
+ // E.g.
190
+ // {
191
+ // permalink: '/learn/best-practices/',
192
+ // relativeDirectory: 'learn',
193
+ // sidebarTitle: 'Introduction',
194
+ // nextPermalink: '/learn/thinking-in-graphs/',
195
+ // sourcePath: 'src/content/learn/BestPractice-Introduction.md'
196
+ // }
197
+ const allPages = [ ]
218
198
219
- await Promise . all (
220
- pagesByDate . map ( async page => {
221
- const {
222
- frontmatter : { permalink, next } ,
223
- } = page
224
- if ( next ) {
225
- previousPagesMap [ next ] = permalink
226
- }
227
- pagesByUrl [ permalink ] = page
228
- } )
229
- )
199
+ // Loop through all *.md files in the repo, setting up both pagesGroupedByFolder
200
+ // and allPages.
201
+ markdownPages . map ( ( { node } ) => {
202
+ const {
203
+ frontmatter : { permalink, next, sidebarTitle } ,
204
+ parent : { relativeDirectory, sourceInstanceName } ,
205
+ } = node
230
206
231
- let firstPage = null
207
+ if ( sourceInstanceName !== "content" || relativeDirectory . includes ( "code/" ) ) {
208
+ return
209
+ }
232
210
233
- await Promise . all (
234
- pagesByDate . map ( async page => {
235
- const {
236
- frontmatter : { permalink } ,
237
- } = page
211
+ if ( ! pagesGroupedByFolder [ relativeDirectory ] ) {
212
+ pagesGroupedByFolder = {
213
+ ...pagesGroupedByFolder ,
214
+ [ relativeDirectory ] : [ node ] ,
215
+ }
216
+ } else {
217
+ pagesGroupedByFolder = {
218
+ ...pagesGroupedByFolder ,
219
+ [ relativeDirectory ] : [
220
+ ...pagesGroupedByFolder [ relativeDirectory ] ,
221
+ node ,
222
+ ] ,
223
+ }
224
+ }
225
+
226
+ allPages . push ( {
227
+ permalink,
228
+ relativeDirectory,
229
+ sidebarTitle,
230
+ nextPermalink : next ,
231
+ sourcePath : path . relative ( __dirname , node . fileAbsolutePath ) ,
232
+ } )
233
+ } )
234
+
235
+ // Loop through the sections in the sidebar, mutating the
236
+ // next and previous objects for different
237
+ Object . entries ( pagesGroupedByFolder ) . map ( ( [ folder , pages ] ) => {
238
+ let pagesByUrl = { }
239
+ let previousPagesMap = { }
240
+ let pagesByDate = pages . sort ( ( a , b ) => {
241
+ const aDate = new Date ( a . frontmatter . date || Date . now ( ) )
242
+ const bDate = new Date ( b . frontmatter . date || Date . now ( ) )
243
+ if ( aDate > bDate ) {
244
+ return - 1
245
+ } else if ( aDate < bDate ) {
246
+ return 1
247
+ }
248
+ return 0
249
+ } )
238
250
239
- if ( ! previousPagesMap [ permalink ] && ! firstPage ) {
240
- firstPage = page
241
- return
242
- }
243
- } )
244
- )
251
+ pagesByDate . forEach ( page => {
252
+ const next = page . frontmatter . next
253
+ const permalink = page . frontmatter . permalink
245
254
246
- if ( ! firstPage ) {
247
- throw new Error ( `First page not found in ${ folder } ` )
255
+ if ( next ) {
256
+ previousPagesMap [ next ] = permalink
248
257
}
258
+ pagesByUrl [ permalink ] = page
259
+ } )
249
260
250
- let categoriesMap = { }
251
- let currentCategory = null
261
+ let firstPage = null
262
+ pagesByDate . forEach ( page => {
263
+ const permalink = page . frontmatter . permalink
264
+ if ( ! previousPagesMap [ permalink ] && ! firstPage ) {
265
+ firstPage = page
266
+ return
267
+ }
268
+ } )
269
+
270
+ if ( ! firstPage ) {
271
+ throw new Error ( `First page not found in ${ folder } ` )
272
+ }
252
273
253
- let page = firstPage
254
- let i = 0
255
- while ( page && i ++ < 1000 ) {
256
- const { frontmatter } = page
257
- const {
258
- category : definedCategory ,
259
- next : definedNextPageUrl ,
260
- } = frontmatter
261
- let category = definedCategory || folder
262
- if ( ! currentCategory || category !== currentCategory . name ) {
263
- if ( currentCategory ) {
264
- if ( ! ( currentCategory . name in categoriesMap ) ) {
265
- categoriesMap [ currentCategory . name ] = currentCategory
266
- }
267
- }
268
- currentCategory = {
269
- name : category ,
270
- links : [ ] ,
274
+ let categoriesMap = { }
275
+ let currentCategory = null
276
+
277
+ let page = firstPage
278
+ let i = 0
279
+ while ( page && i ++ < 1000 ) {
280
+ const { frontmatter } = page
281
+ const {
282
+ category : definedCategory ,
283
+ next : definedNextPageUrl ,
284
+ } = frontmatter
285
+ let category = definedCategory || folder
286
+ if ( ! currentCategory || category !== currentCategory . name ) {
287
+ if ( currentCategory ) {
288
+ if ( ! ( currentCategory . name in categoriesMap ) ) {
289
+ categoriesMap [ currentCategory . name ] = currentCategory
271
290
}
272
291
}
273
- currentCategory . links . push ( page )
274
- if ( definedNextPageUrl ) {
275
- page = pagesByUrl [ definedNextPageUrl ]
276
- } else {
277
- page = pagesByDate [ pagesByDate . indexOf ( page ) + 1 ]
278
- }
279
- if ( currentCategory . links . includes ( page ) ) {
280
- page = null
292
+ currentCategory = {
293
+ name : category ,
294
+ links : [ ] ,
281
295
}
282
296
}
283
-
284
- if ( ! ( currentCategory . name in categoriesMap ) ) {
285
- categoriesMap [ currentCategory . name ] = currentCategory
297
+ currentCategory . links . push ( page )
298
+ if ( definedNextPageUrl ) {
299
+ page = pagesByUrl [ definedNextPageUrl ]
300
+ } else {
301
+ page = pagesByDate [ pagesByDate . indexOf ( page ) + 1 ]
302
+ }
303
+ if ( currentCategory . links . includes ( page ) ) {
304
+ page = null
286
305
}
306
+ }
307
+
308
+ if ( ! ( currentCategory . name in categoriesMap ) ) {
309
+ categoriesMap [ currentCategory . name ] = currentCategory
310
+ }
287
311
288
- sideBardata [ folder ] = Object . values ( categoriesMap )
312
+ sideBardata [ folder ] = Object . values ( categoriesMap )
313
+ } )
314
+
315
+ Object . entries ( additionalSidebarItems ) . map ( ( [ folder , sections ] ) => {
316
+ sections . forEach ( s => {
317
+ const originalLinks = sideBardata [ folder ] . find ( l => l . name === s . name )
318
+ originalLinks . links = [ ...originalLinks . links , ...s . links ]
289
319
} )
290
- )
320
+ } )
291
321
292
- await Promise . all (
293
- allPages . map ( async page => {
294
- createPage ( {
295
- path : `${ page . permalink } ` ,
296
- component : docTemplate ,
297
- context : {
298
- permalink : page . permalink ,
299
- nextPermalink : page . nextPermalink ,
300
- sideBarData : sideBardata [ page . relativeDirectory ] ,
301
- sourcePath : page . sourcePath ,
302
- } ,
303
- } )
322
+
323
+ // Use all the set up data to now tell Gatsby to create pages
324
+ // on the site
325
+ allPages . forEach ( page => {
326
+ createPage ( {
327
+ path : `${ page . permalink } ` ,
328
+ component : docTemplate ,
329
+ context : {
330
+ permalink : page . permalink ,
331
+ nextPermalink : page . nextPermalink ,
332
+ sideBarData : sideBardata [ page . relativeDirectory ] ,
333
+ sourcePath : page . sourcePath ,
334
+ } ,
304
335
} )
305
- )
336
+ } )
306
337
}
0 commit comments