-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathReferenceLayout.astro
163 lines (149 loc) · 4.31 KB
/
ReferenceLayout.astro
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
---
import { type CollectionEntry } from "astro:content";
import Head from "@components/Head/index.astro";
import BaseLayout from "./BaseLayout.astro";
import { ReferenceDirectoryWithFilter } from "@components/ReferenceDirectoryWithFilter/";
import { categories } from "../content/reference/config";
import {
getRefEntryTitleConcatWithParen,
normalizeReferenceRoute,
} from "../pages/_utils";
import {
setJumpToState,
type JumpToLink,
type JumpToState,
} from "../globals/state";
import {
getCurrentLocale,
getUiTranslationWithFallback,
getUiTranslator,
} from "../i18n/utils";
interface Props {
title: string;
entries: CollectionEntry<"reference">[];
categories?: string[];
}
function strCompare(a: string, b: string) {
if (a < b) {
return -1;
}
if (a > b) {
return 1;
}
return 0;
}
const currentLocale = getCurrentLocale(Astro.url.pathname);
const t = await getUiTranslator(currentLocale);
const uiTranslations = await getUiTranslationWithFallback(currentLocale);
const { entries } = Astro.props;
const categoryData = (Astro.props.categories ?? categories).map((category) => {
// Get all reference entries in the module
const directEntries = entries.filter(
(e) => e.data.module === t("referenceCategories", "modules", category)
);
// Get the names of all submodules
const subcats = [
...new Set(
directEntries
// Ignore classes here, they will be added later
.filter((e) => e.data.submodule && !e.data.isConstructor && (!e.data.class || e.data.class === 'p5'))
.map((e) => e.data.submodule)
).values(),
].sort();
// Find the classes in this module. These will be turned into subcategories.
const classes = directEntries
.filter(
(e) =>
e.data.isConstructor &&
e.data.module === category &&
e.data.title !== "p5"
)
.sort((a, b) => strCompare(a.data.title, b.data.title))
.map((e) => ({
...e,
data: {
...e.data,
path: normalizeReferenceRoute(e.id),
},
}));
// Return the data for each subcategory
const subcatData = [
{
name: undefined,
entry: undefined,
entries: directEntries
.filter(
(e) =>
!e.data.submodule &&
!e.data.isConstructor &&
!classes.some((c) => c.data.title === e.data.class)
)
.map((e) => ({
...e,
data: {
...e.data,
title: getRefEntryTitleConcatWithParen(e),
path: normalizeReferenceRoute(e.id),
},
})),
},
...subcats.map((subcat, i) => ({
name: i === 0 && subcat === category ? undefined : subcat,
entry: undefined,
entries: directEntries
.filter(
(e) =>
e.data.submodule === subcat &&
(!e.data.class || e.data.class === "p5") &&
!e.data.isConstructor &&
e.data.description !== ""
)
.map((e) => ({
...e,
data: {
...e.data,
title: getRefEntryTitleConcatWithParen(e),
path: normalizeReferenceRoute(e.id),
},
})),
})),
...classes.map((entry) => ({
name: getRefEntryTitleConcatWithParen(entry),
entry, // Include the class entry itself so we can make the header a link
entries: directEntries
.filter((e) => e.data.class === entry.data.title && e.data.class)
.map((e) => ({
...e,
data: {
...e.data,
title: getRefEntryTitleConcatWithParen(e),
path: normalizeReferenceRoute(e.id),
},
})),
})),
];
return {
name: t("referenceCategories", "modules", category),
subcats: subcatData,
};
});
const jumpCategoryData = categoryData.length === 1
? [...categoryData, ...categoryData[0].subcats]
: categoryData;
const pageJumpToLinks: JumpToLink[] = jumpCategoryData.map((category) => ({
label: category.name as string,
url: `#${category.name}`,
}));
const pageJumpToState: JumpToState = {
links: pageJumpToLinks,
};
setJumpToState(pageJumpToState);
---
<Head title={Astro.props.title} locale={currentLocale} />
<BaseLayout title={Astro.props.title} mainContentParentClass="mx-0">
<ReferenceDirectoryWithFilter
client:load
categoryData={categoryData as any}
uiTranslations={uiTranslations}
/>
</BaseLayout>