-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
normalize-pages.ts
364 lines (334 loc) · 9.46 KB
/
normalize-pages.ts
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import { PageMapItem } from 'nextra'
import { DEFAULT_PAGE_THEME } from '../constants'
import { PageTheme } from '../types'
import { Folder, MdxFile } from 'nextra'
function extendMeta(
meta: string | Record<string, any> = {},
fallback: Record<string, any>
) {
if (typeof meta === 'string') {
meta = { title: meta }
}
const theme = Object.assign({}, fallback.theme, meta.theme)
return Object.assign({}, fallback, meta, { theme })
}
export interface Item extends MdxFile {
title: string
type: string
children?: Item[]
hidden?: boolean
withIndexPage?: boolean
}
export interface PageItem extends MdxFile {
title: string
type: string
href?: string
newWindow?: boolean
children?: PageItem[]
firstChildRoute?: string
hidden?: boolean
withIndexPage?: boolean
}
export interface MenuItem extends MdxFile {
title: string
type: 'menu'
hidden?: boolean
children?: PageItem[]
items?: Record<
string,
{
title: string
href?: string
newWindow?: boolean
}
>
}
interface DocsItem extends MdxFile {
title: string
type: string
children?: DocsItem[]
firstChildRoute?: string
withIndexPage?: boolean
}
function findFirstRoute(items: DocsItem[]): string | undefined {
for (const item of items) {
if (item.route) return item.route
if (item.children) {
const route = findFirstRoute(item.children)
if (route) return route
}
}
}
const CUSTOM_ERROR_PAGES = ['/404', '/500']
export function normalizePages({
list,
locale,
defaultLocale,
route,
docsRoot = '',
underCurrentDocsRoot = false,
pageThemeContext = DEFAULT_PAGE_THEME
}: {
list: PageMapItem[]
locale: string
defaultLocale?: string
route: string
docsRoot?: string
underCurrentDocsRoot?: boolean
pageThemeContext?: PageTheme
}) {
let _meta: Record<string, any> | undefined
for (let item of list) {
if (item.kind === 'Meta') {
if (item.locale === locale) {
_meta = item.data
break
}
// fallback
if (!_meta) {
_meta = item.data
}
}
}
const meta = _meta || {}
const metaKeys = Object.keys(meta)
for (let key of metaKeys) {
if (typeof meta[key] === 'string') {
meta[key] = {
title: meta[key]
}
}
}
// All directories
// - directories: all directories in the tree structure
// - flatDirectories: all directories in the flat structure, used by search
const directories: Item[] = []
const flatDirectories: Item[] = []
// Docs directories
const docsDirectories: DocsItem[] = []
const flatDocsDirectories: DocsItem[] = []
// Page directories
const topLevelNavbarItems: PageItem[] = []
let activeType: string | undefined
let activeIndex = 0
let activeThemeContext = pageThemeContext
let activePath: Item[] = []
let metaKeyIndex = -1
const fallbackMeta = meta['*'] || {}
delete fallbackMeta.title
delete fallbackMeta.href
// Normalize items based on files and _meta.json.
const items = list
.filter(
(a): a is MdxFile | Folder =>
// not meta
a.kind !== 'Meta' &&
// not hidden routes
!a.name.startsWith('_') &&
// locale matches, or fallback to default locale
// @ts-expect-error
(a.locale === locale || a.locale === defaultLocale || !a.locale)
)
.sort((a, b) => {
const indexA = metaKeys.indexOf(a.name)
const indexB = metaKeys.indexOf(b.name)
if (indexA === -1 && indexB === -1) return a.name < b.name ? -1 : 1
if (indexA === -1) return 1
if (indexB === -1) return -1
return indexA - indexB
})
.flatMap(item => {
const items = []
const index = metaKeys.indexOf(item.name)
let extendedItem
if (index !== -1) {
// Fill all skipped items in meta.
for (let i = metaKeyIndex + 1; i < index; i++) {
const key = metaKeys[i]
if (key !== '*') {
items.push({
name: key,
route: '',
...meta[key]
})
}
}
metaKeyIndex = index
extendedItem = { ...meta[item.name], ...item }
}
items.push(extendedItem || item)
return items
})
// Fill all skipped items in meta.
for (let i = metaKeyIndex + 1; i < metaKeys.length; i++) {
const key = metaKeys[i]
if (key !== '*') {
items.push({
name: key,
route: '#',
...meta[key]
})
}
}
for (let i = 0; i < items.length; i++) {
const a = items[i]
// If there are two items with the same name, they must be a directory and a
// page. In that case we merge them, and use the page's link.
if (i + 1 < items.length && a.name === items[i + 1].name) {
items[i + 1] = { ...items[i + 1], withIndexPage: true }
if (a.children && !items[i + 1].children) {
items[i + 1].children = a.children
}
continue
}
// Get the item's meta information.
const extendedMeta = extendMeta(meta[a.name], fallbackMeta)
const { hidden, type = 'doc' } = extendedMeta
const extendedPageThemeContext = {
...pageThemeContext,
...extendedMeta.theme
}
// If the doc is under the active page root.
const isCurrentDocsTree = route.startsWith(docsRoot)
const normalizedChildren: any =
a.children &&
normalizePages({
list: a.children,
locale,
defaultLocale,
route,
docsRoot: type === 'page' || type === 'menu' ? a.route : docsRoot,
underCurrentDocsRoot: underCurrentDocsRoot || isCurrentDocsTree,
pageThemeContext: extendedPageThemeContext
})
const title = extendedMeta.title || (type !== 'separator' && a.name)
const getItem = (): Item => ({
...a,
type,
...(title && { title }),
...(hidden && { hidden }),
...(normalizedChildren && { children: [] })
})
const item: Item = getItem()
const docsItem: DocsItem = getItem()
const pageItem: PageItem = getItem()
// This item is currently active, we collect the active path etc.
if (a.route === route) {
activePath = [item]
activeType = type
// There can be multiple matches.
activeThemeContext = {
...activeThemeContext,
...extendedPageThemeContext
}
switch (type) {
case 'page':
case 'menu':
// Active on the navbar
activeIndex = topLevelNavbarItems.length
break
case 'doc':
// Active in the docs tree
if (isCurrentDocsTree) {
activeIndex = flatDocsDirectories.length
}
}
}
if (hidden || CUSTOM_ERROR_PAGES.includes(a.route)) continue
if (normalizedChildren) {
if (
normalizedChildren.activeIndex !== undefined &&
normalizedChildren.activeType !== undefined
) {
activeThemeContext = normalizedChildren.activeThemeContext
activeType = normalizedChildren.activeType
activePath = [item, ...normalizedChildren.activePath]
switch (activeType) {
case 'page':
case 'menu':
activeIndex =
topLevelNavbarItems.length + normalizedChildren.activeIndex
break
case 'doc':
activeIndex =
flatDocsDirectories.length + normalizedChildren.activeIndex
break
}
if (a.withIndexPage) {
if (type === 'doc') {
activeIndex++
}
}
}
switch (type) {
case 'page':
case 'menu':
// @ts-expect-error normalizedChildren === true
pageItem.children.push(...normalizedChildren.directories)
docsDirectories.push(...normalizedChildren.docsDirectories)
// If it's a page with children inside, we inject itself as a page too.
if (normalizedChildren.flatDirectories.length) {
pageItem.firstChildRoute = findFirstRoute(
normalizedChildren.flatDirectories
)
topLevelNavbarItems.push(pageItem)
} else if (pageItem.withIndexPage) {
topLevelNavbarItems.push(pageItem)
}
break
case 'doc':
if (isCurrentDocsTree) {
if (Array.isArray(docsItem.children)) {
docsItem.children.push(...normalizedChildren.docsDirectories)
}
// Itself is a doc page.
if (item.withIndexPage) {
flatDocsDirectories.push(docsItem)
}
}
}
flatDirectories.push(...normalizedChildren.flatDirectories)
flatDocsDirectories.push(...normalizedChildren.flatDocsDirectories)
if (Array.isArray(item.children)) {
item.children.push(...normalizedChildren.directories)
}
} else {
flatDirectories.push(item)
switch (type) {
case 'page':
case 'menu':
topLevelNavbarItems.push(pageItem)
break
case 'doc':
if (isCurrentDocsTree) {
flatDocsDirectories.push(docsItem)
}
}
}
directories.push(item)
switch (type) {
case 'page':
case 'menu':
if (isCurrentDocsTree && underCurrentDocsRoot) {
docsDirectories.push(pageItem)
}
break
case 'doc':
case 'separator':
if (isCurrentDocsTree) {
docsDirectories.push(docsItem)
}
}
}
return {
activeType,
activeIndex,
activeThemeContext,
activePath,
directories,
flatDirectories,
docsDirectories,
flatDocsDirectories,
topLevelNavbarItems
}
}