forked from withastro/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsidebar.ts
52 lines (49 loc) · 1.7 KB
/
sidebar.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
import type starlight from '@astrojs/starlight';
import { normalizeLangTag } from '../src/i18n/bcp-normalize';
import type { NavDict } from '../src/i18n/translation-checkers';
import { navTranslations } from '../src/i18n/util';
/** For an item in our sidebar, get translations of its label. */
function getTranslations(item: NavDict[number]): Record<string, string> | undefined {
return Object.fromEntries(
Object.entries(navTranslations)
.map(([lang, translations]) => {
const translation = translations.find((t) => t.key === item.key);
return [
normalizeLangTag(lang),
translation && translation.text !== item.text ? translation?.text : '',
] as const;
})
.filter(([, text]) => Boolean(text))
);
}
type StarlightSidebarConfig = NonNullable<Parameters<typeof starlight>[0]['sidebar']>;
/** Generate a Starlight sidebar config object from our existing `nav.ts` files. */
export function makeSidebar(): StarlightSidebarConfig {
let currentSubGroup: Extract<StarlightSidebarConfig[number], { items: StarlightSidebarConfig }>;
return navTranslations.en.reduce((sidebar, item) => {
if ('header' in item) {
const newGroup = {
label: item.text,
translations: getTranslations(item),
items: [],
collapsed: item.collapsed,
};
if (item.nested) {
const parentGroup = sidebar.at(-1);
if (parentGroup && typeof parentGroup !== 'string' && 'items' in parentGroup) {
parentGroup.items.push(newGroup);
}
} else {
sidebar.push(newGroup);
}
currentSubGroup = newGroup;
} else {
currentSubGroup.items.push({
label: item.text,
link: item.slug,
translations: getTranslations(item),
});
}
return sidebar;
}, [] as StarlightSidebarConfig);
}