Skip to content

Commit d106bcb

Browse files
committed
Adds a members sidebar, and cleans up gatsby-node
1 parent 67a38e0 commit d106bcb

File tree

2 files changed

+159
-128
lines changed

2 files changed

+159
-128
lines changed

gatsby-node.js

Lines changed: 158 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,12 @@ exports.onCreatePage = async ({ page, actions }) => {
3232
const languageNameSlug = pathArr[languageNameSlugIndex]
3333
const languageName = slugMap[languageNameSlug]
3434
codeData.Languages = codeData.Languages || {}
35-
codeData.Languages[languageName] =
36-
codeData.Languages[languageName] || {}
35+
codeData.Languages[languageName] = codeData.Languages[languageName] || {}
3736

3837
const categoryNameSlugIndex = languageSupportDirIndex + 2
3938
const categoryNameSlug = pathArr[categoryNameSlugIndex]
4039
const categoryName = slugMap[categoryNameSlug]
41-
codeData.Languages[languageName][categoryName] =
42-
codeData.Languages[languageName][categoryName] || []
40+
codeData.Languages[languageName][categoryName] = codeData.Languages[languageName][categoryName] || []
4341
codeData.Languages[languageName][categoryName].push({
4442
name,
4543
description,
@@ -157,150 +155,183 @@ exports.createPages = async ({ graphql, actions }) => {
157155
throw result.errors
158156
}
159157

160-
const { edges } = result.data.allMarkdownRemark
158+
const markdownPages = result.data.allMarkdownRemark.edges
161159

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+
// }
163170
let pagesGroupedByFolder = {}
164-
const allPages = []
165171

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 = {}
172179

173-
if (
174-
sourceInstanceName !== "content" ||
175-
relativeDirectory.includes("code/")
176-
) {
177-
return
178-
}
179180

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+
}
203188

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 = []
218198

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
230206

231-
let firstPage = null
207+
if (sourceInstanceName !== "content" || relativeDirectory.includes("code/")) {
208+
return
209+
}
232210

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+
})
238250

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
245254

246-
if (!firstPage) {
247-
throw new Error(`First page not found in ${folder}`)
255+
if (next) {
256+
previousPagesMap[next] = permalink
248257
}
258+
pagesByUrl[permalink] = page
259+
})
249260

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+
}
252273

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
271290
}
272291
}
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: [],
281295
}
282296
}
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
286305
}
306+
}
307+
308+
if (!(currentCategory.name in categoriesMap)) {
309+
categoriesMap[currentCategory.name] = currentCategory
310+
}
287311

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]
289319
})
290-
)
320+
})
291321

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+
},
304335
})
305-
)
336+
})
306337
}

src/pages/foundation/members.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default ({ pageContext }) => {
1515
</p>
1616
<p>
1717
To learn more about the <a href="/foundation">GraphQL Foundation</a> and becoming a
18-
member, please see our <a href="/http://localhost:8000/faq/graphql-foundation/">FAQ</a>,
18+
member, please see our <a href="/faq/graphql-foundation/">FAQ</a>,
1919
reach out to <a href="mailto:[email protected]">[email protected]</a>, or <a
2020
href="https://join.graphql.org">complete a membership application</a>.
2121
</p>

0 commit comments

Comments
 (0)