1
1
import { fromZodError } from 'zod-validation-error'
2
- import type {
3
- Folder ,
4
- FrontMatter ,
5
- MdxFile ,
6
- MenuItem ,
7
- PageMapItem ,
8
- SeparatorItem
9
- } from '../../types.js'
2
+ import type { Folder , FrontMatter , MdxFile , PageMapItem } from '../../types.js'
10
3
import { metaSchema } from '../schemas.js'
11
4
import { pageTitleFromFilename } from '../utils.js'
12
5
@@ -39,18 +32,15 @@ function titlize(item: Folder | MdxFile, meta: MetaRecord): string {
39
32
type MetaRecord = Record < string , Record < string , any > >
40
33
41
34
function sortFolder ( pageMap : PageMapItem [ ] | Folder ) {
42
- const newChildren : (
43
- | ( { title : string } & ( Folder | MdxFile ) )
44
- | ( ( SeparatorItem | MenuItem ) & { name : string } )
45
- ) [ ] = [ ]
35
+ const newChildren : ( Folder | MdxFile ) [ ] = [ ]
46
36
47
37
const isFolder = ! Array . isArray ( pageMap )
48
38
49
39
const folder = (
50
40
isFolder ? { ...pageMap } : { children : pageMap }
51
41
) as ParsedFolder
52
42
53
- const meta : MetaRecord = { }
43
+ const meta : Record < string , Record < string , any > > = { }
54
44
for ( const item of folder . children ) {
55
45
if (
56
46
isFolder &&
@@ -60,10 +50,7 @@ function sortFolder(pageMap: PageMapItem[] | Folder) {
60
50
) {
61
51
folder . frontMatter = item . frontMatter
62
52
} else if ( 'children' in item ) {
63
- newChildren . push ( {
64
- ...normalizePageMap ( item ) ,
65
- title : titlize ( item , meta )
66
- } )
53
+ newChildren . push ( normalizePageMap ( item ) )
67
54
} else if ( 'data' in item ) {
68
55
for ( const [ key , titleOrObject ] of Object . entries ( item . data ) ) {
69
56
const { data, error } = metaSchema . safeParse ( titleOrObject )
@@ -79,16 +66,8 @@ function sortFolder(pageMap: PageMapItem[] | Folder) {
79
66
// @ts -expect-error -- fixme
80
67
meta [ key ] = data
81
68
}
82
- } else if (
83
- 'type' in item &&
84
- ( item . type === 'separator' || item . type === 'menu' )
85
- ) {
86
- newChildren . push ( item as any )
87
69
} else {
88
- newChildren . push ( {
89
- ...item ,
90
- title : titlize ( item , meta )
91
- } )
70
+ newChildren . push ( item )
92
71
}
93
72
}
94
73
@@ -121,7 +100,7 @@ function sortFolder(pageMap: PageMapItem[] | Folder) {
121
100
122
101
// Validate menu items, local page should exist
123
102
const { children } = items . find (
124
- ( i ) : i is { title : string } & Folder < MdxFile > => i . name === metaKey
103
+ ( i ) : i is Folder < MdxFile > => i . name === metaKey
125
104
) !
126
105
for ( const [ key , value ] of Object . entries (
127
106
// @ts -expect-error fixme
@@ -162,6 +141,22 @@ The field key "${metaKey}" in \`_meta\` file refers to a page that cannot be fou
162
141
items . unshift ( { data : meta } )
163
142
}
164
143
165
- const result = isFolder ? { ...folder , children : items } : items
144
+ const itemsWithTitle = items . map ( item => {
145
+ const isSeparator = 'type' in item && item . type === 'separator'
146
+ if ( 'name' in item && ! isSeparator ) {
147
+ return {
148
+ ...item ,
149
+ title : titlize ( item , meta )
150
+ }
151
+ }
152
+ return item
153
+ } )
154
+
155
+ const result = isFolder
156
+ ? {
157
+ ...folder ,
158
+ children : itemsWithTitle
159
+ }
160
+ : itemsWithTitle
166
161
return result
167
162
}
0 commit comments