forked from graphql/graphql.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gatsby-node.js
197 lines (176 loc) · 4.74 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
const path = require("path")
exports.onCreatePage = async ({ page, actions }) => {
const { createPage, deletePage } = actions
deletePage(page)
createPage({
...page,
context: {
...page.context,
sourcePath: path.relative(__dirname, page.componentPath),
},
})
}
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions
const result = await graphql(/* GraphQL */ `
query {
allMarkdownRemark {
edges {
node {
fileAbsolutePath
parent {
... on File {
relativeDirectory
sourceInstanceName
}
}
frontmatter {
title
permalink
next
category
sublinks
sidebarTitle
date
}
id
}
}
}
}
`)
const docTemplate = path.resolve("./src/templates/doc.tsx")
if (result.errors) {
// eslint-disable-next-line no-console
console.error(result.errors)
throw result.errors
}
const { edges } = result.data.allMarkdownRemark
let sideBardata = {}
let pagesGroupedByFolder = {}
const allPages = []
await Promise.all(
edges.map(async ({ node }) => {
const {
frontmatter: { permalink, next, sidebarTitle },
parent: { relativeDirectory, sourceInstanceName },
} = node
if (sourceInstanceName !== "content") {
return
}
if (!pagesGroupedByFolder[relativeDirectory]) {
pagesGroupedByFolder = {
...pagesGroupedByFolder,
[relativeDirectory]: [node],
}
} else {
pagesGroupedByFolder = {
...pagesGroupedByFolder,
[relativeDirectory]: [
...pagesGroupedByFolder[relativeDirectory],
node,
],
}
}
allPages.push({
permalink,
relativeDirectory,
sidebarTitle,
nextPermalink: next,
sourcePath: path.relative(__dirname, node.fileAbsolutePath),
})
})
)
await Promise.all(
Object.entries(pagesGroupedByFolder).map(async ([folder, pages]) => {
let pagesByUrl = {}
let previousPagesMap = {}
let pagesByDate = pages.sort((a, b) => {
const aDate = new Date(a.frontmatter.date || Date.now())
const bDate = new Date(b.frontmatter.date || Date.now())
if (aDate > bDate) {
return -1
} else if (aDate < bDate) {
return 1
}
return 0
})
await Promise.all(
pagesByDate.map(async page => {
const {
frontmatter: { permalink, next },
} = page
if (next) {
previousPagesMap[next] = permalink
}
pagesByUrl[permalink] = page
})
)
let firstPage = null
await Promise.all(
pagesByDate.map(async page => {
const {
frontmatter: { permalink },
} = page
if (!previousPagesMap[permalink] && !firstPage) {
firstPage = page
return
}
})
)
if (!firstPage) {
throw new Error(`First page not found in ${folder}`)
}
let categoriesMap = {}
let currentCategory = null
let page = firstPage
let i = 0
while (page && i++ < 1000) {
const { frontmatter } = page
const {
category: definedCategory,
next: definedNextPageUrl,
} = frontmatter
let category = definedCategory || folder
if (!currentCategory || category !== currentCategory.name) {
if (currentCategory) {
if (!(currentCategory.name in categoriesMap)) {
categoriesMap[currentCategory.name] = currentCategory
}
}
currentCategory = {
name: category,
links: [],
}
}
currentCategory.links.push(page)
if (definedNextPageUrl) {
page = pagesByUrl[definedNextPageUrl]
} else {
page = pagesByDate[pagesByDate.indexOf(page) + 1]
}
if (currentCategory.links.includes(page)) {
page = null
}
}
if (!(currentCategory.name in categoriesMap)) {
categoriesMap[currentCategory.name] = currentCategory
}
sideBardata[folder] = Object.values(categoriesMap);
})
)
await Promise.all(
allPages.map(async page => {
createPage({
path: `${page.permalink}`,
component: docTemplate,
context: {
permalink: page.permalink,
nextPermalink: page.nextPermalink,
sideBarData: sideBardata[page.relativeDirectory],
sourcePath: page.sourcePath,
},
})
})
)
}