-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnavigation.js
More file actions
186 lines (156 loc) · 4.42 KB
/
navigation.js
File metadata and controls
186 lines (156 loc) · 4.42 KB
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
const { GraphQLScalarType, GraphQLInt } = require("gatsby/graphql")
const config = require("./config")
const CHAPTER_HEADING = "chapter-heading"
const trailingSlash = !!config.gatsby?.trailingSlash // boolean
const ignoreIndex = !!config.sidebar.ignoreIndex // boolean
const { forcedNavOrder = [] } = config.sidebar.forcedNavOrder
function calculateTreeData(pagesArg) {
const pages = []
for (const page of pagesArg) {
const mappedPage = {
slug: page.fields.slug,
title: page.fields.title,
isChapterHeading: page.frontmatter.type === CHAPTER_HEADING,
}
if (ignoreIndex) {
if (mappedPage.slug !== "/") {
pages.push(mappedPage)
}
} else {
pages.push(mappedPage)
}
}
// build tree
const tree = { items: [], label: "" }
for (const page of pages) {
const slugParts = page.slug.split("/")
const directory = trailingSlash
? slugParts.slice(1, -2)
: slugParts.slice(1, -1)
const fileIndex = trailingSlash
? slugParts.length - 2
: slugParts.length - 1
const fileName = slugParts[fileIndex]
let workDir = tree.items
// find or create folders subtree
for (const folderName of directory) {
const dir = workDir.find(f => f.label === folderName)
if (!dir) {
const newFolder = { items: [], label: folderName }
workDir.push(newFolder)
workDir = newFolder.items
} else {
workDir = dir.items
}
}
const file = workDir.find(f => f.label === fileName)
if (!file) {
// add page
workDir.push({
label: fileName,
url: page.slug,
title: page.title,
isChapterHeading: page.isChapterHeading,
items: [],
})
} else {
// if folder/file is already exists, then add url, title, isChapterHeading
file.url = page.slug
file.title = page.title
file.isChapterHeading = page.isChapterHeading
}
}
// merge tree with forcedNavOrder
const tmp = [...forcedNavOrder].reverse()
const treeData = tmp.reduce((acc, slug) => {
const slugParts = slug.split("/")
const directory = trailingSlash
? slugParts.slice(1, -2)
: slugParts.slice(1, -1)
const slicedLength = trailingSlash
? slugParts.length - 2
: slugParts.length - 1
const name = slugParts[slicedLength]
let workDir = acc.items
for (const folder of directory) {
let node = workDir.find(item => item.label === folder)
if (!node) {
const newFolder = { label: folder, items: [] }
workDir.push(newFolder)
} else {
workDir = node.items
}
}
const index = workDir.findIndex(({ label }) => label === name)
if (workDir.length) {
const node = workDir.splice(index, 1)[0] // remove node
accu.items.unshift(node) // add node on the first place
}
return acc
}, tree)
return sortTree(treeData)
}
function sortTree(tree) {
const sortedItems = tree.items.sort((a, b) => {
return a.label.localeCompare(b.label, "kn", { numeric: true })
})
const finallySortedItems = sortedItems.map(item => {
if (item.items.length > 0) {
return sortTree(item)
}
return item
})
return { ...tree, items: finallySortedItems }
}
function recursivelyFlattenNav(tree) {
const items = []
if (!tree.items.length) {
return [{ title: tree.title, url: tree.url }]
}
if (!tree.isChapterHeading) {
items.push({ title: tree.title, url: tree.url })
}
for (const item of tree.items) {
const flatChildren = recursivelyFlattenNav(item)
items.push(...flatChildren)
}
return items
}
async function buildTreeForPath(getNodes) {
const pages = []
for (const node of getNodes()) {
if (node.internal.type === "Mdx") {
pages.push({
fields: node.fields,
frontmatter: node.frontmatter,
})
}
}
const tree = calculateTreeData(pages)
const array = recursivelyFlattenNav(tree)
return { tree, array }
}
module.exports.setFieldsOnGraphQLNodeType = async ({ type, getNodes }) => {
if (type.name !== "Site") {
return {}
}
return {
navigation: {
type: new GraphQLScalarType({
name: "Navigation",
serialize(value) {
return value
},
}),
resolve: () => {
return buildTreeForPath(getNodes)
},
},
order: {
type: GraphQLInt,
result: node => {
return node.fields?.order ? node.fields.order : 0
},
},
}
}