-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathcollections.ts
More file actions
119 lines (111 loc) · 3.29 KB
/
Copy pathcollections.ts
File metadata and controls
119 lines (111 loc) · 3.29 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
import { minimatch } from 'minimatch';
type Collection<T = {}> = {
name: string;
title: string;
Component: React.ReactElement;
path: string;
} & T;
interface Collections {
pages: Collection<{ index?: number }>[];
components: Collection<{ deprecated?: boolean }>[];
componentsV1: Collection[];
foundations: Collection[];
}
/*
* Maps specific components/mdx to collections:
* - Pages collection is for generic documentation (like getting started, or principles)
* - Components collection is for all component MDX documentation
*
* These collections can be used to automatically import/sort components and display
* them in the relevant section within the site navigation.
*/
const collections: Collections = (require as any)
.context('./', true, /\.(mdx|jsx?)$/)
.keys()
.reduce(
(collections: Collections, key: string) => {
const pagesMatch = minimatch(key, './pages/*.mdx');
const componentMatch = minimatch(key, './pages/components/*.mdx');
const foundationMatch = minimatch(key, './pages/foundations/*.mdx');
const filepath = `docs${key.substring(1)}`;
if (pagesMatch) {
const name = key.match(/(\w+)\.mdx$/)?.[1] || '';
const {
default: Component,
title,
path,
frontmatter,
...props
} = require(`./pages/${name}.mdx`);
const component = {
name,
title,
Component,
path: path || `/${name}`,
filepath,
...frontmatter,
...props
};
collections.pages.push(component);
} else if (componentMatch) {
const name = key.match(/(\w+)\.mdx$/)?.[1] || '';
const {
default: Component,
title,
path,
frontmatter,
...props
} = require(`./pages/components/${name}.mdx`);
const component = {
name,
title,
Component,
path: path || `/components/${name}`,
filepath,
...frontmatter,
...props
};
collections.components.push(component);
} else if (foundationMatch) {
const name = key.match(/(\w+)\.mdx$/)?.[1] || '';
const {
default: Component,
title,
path,
frontmatter,
...props
} = require(`./pages/foundations/${name}.mdx`);
const component = {
name,
title,
Component,
path: path || `/foundations/${name}`,
filepath,
...frontmatter,
...props
};
collections.foundations.push(component);
}
return collections;
},
{ pages: [], components: [], componentsV1: [], foundations: [] }
);
// Pages should first be sorted by their index, then alphabetical
export const pages = [
...collections.pages
.filter(({ index }) => !!index)
.sort(
(a, b) =>
(a as Collection<{ index: number }>).index -
(b as Collection<{ index: number }>).index
),
...collections.pages
.filter(({ index }) => !index)
.sort((a, b) => a.name.localeCompare(b.name))
];
export const components = [...collections.components].sort((a, b) =>
a.name.localeCompare(b.name)
);
export const foundations = [...collections.foundations].sort((a, b) =>
a.name.localeCompare(b.name)
);