|
1 | 1 | ---
|
2 | 2 | import StarlightPage from "@astrojs/starlight/components/StarlightPage.astro";
|
3 |
| -const basePath = `/${Astro.url.pathname.split("/")[1]}`; |
| 3 | +
|
| 4 | +interface GlossaryEntry { |
| 5 | + file: string; |
| 6 | + frontmatter: { |
| 7 | + title: string; |
| 8 | + }; |
| 9 | +} |
4 | 10 |
|
5 | 11 | interface File {
|
6 | 12 | fileBase: string;
|
7 | 13 | title: string;
|
8 | 14 | }
|
9 | 15 |
|
10 |
| -const importedFiles = await Astro.glob("/src/content/docs/glossary/*.md"); |
| 16 | +const basePath = `/${Astro.url.pathname.split("/")[1]}`; |
11 | 17 |
|
12 |
| -const files: File[] = importedFiles.map(({ file, frontmatter: { title } }) => { |
13 |
| - const fileBase = file.toString().split("/").pop()?.split(".").shift(); |
14 |
| - return { fileBase, title }; |
| 18 | +// Get all markdown files |
| 19 | +const posts = import.meta.glob<GlossaryEntry>( |
| 20 | + "/src/content/docs/glossary/*.md", |
| 21 | + { |
| 22 | + eager: true, |
| 23 | + } |
| 24 | +); |
| 25 | +
|
| 26 | +// Convert posts object to array and map to File[] |
| 27 | +const files: File[] = Object.entries(posts).map(([path, post]) => { |
| 28 | + const fileBase = path.split("/").pop()?.split(".").shift() ?? ""; |
| 29 | + return { |
| 30 | + fileBase, |
| 31 | + title: post.frontmatter.title, |
| 32 | + }; |
15 | 33 | });
|
16 | 34 |
|
17 |
| -const sections = files.reduce( |
18 |
| - (acc: { [key: string]: File[] }, { title, fileBase }) => { |
19 |
| - const firstLetter = title.charAt(0).toUpperCase(); |
20 |
| - if (!acc[firstLetter]) { |
21 |
| - acc[firstLetter] = []; |
22 |
| - } |
23 |
| - acc[firstLetter].push({ fileBase, title }); |
24 |
| - return acc; |
25 |
| - }, |
26 |
| - {} |
27 |
| -); |
| 35 | +// Group files by first letter |
| 36 | +const sections = files.reduce((acc: { [key: string]: File[] }, file) => { |
| 37 | + const firstLetter = file.title.charAt(0).toUpperCase(); |
| 38 | + if (!acc[firstLetter]) { |
| 39 | + acc[firstLetter] = []; |
| 40 | + } |
| 41 | + acc[firstLetter].push(file); |
| 42 | + return acc; |
| 43 | +}, {}); |
28 | 44 |
|
29 |
| -const headings = Object.entries(sections).map( |
30 |
| - ([slug, _]: [string, File[]]) => ({ |
| 45 | +// Generate headings for each section |
| 46 | +const headings = Object.entries(sections) |
| 47 | + .sort(([a], [b]) => a.localeCompare(b)) |
| 48 | + .map(([slug]) => ({ |
31 | 49 | depth: 2,
|
32 | 50 | slug,
|
33 | 51 | text: slug,
|
34 |
| - }) |
35 |
| -); |
| 52 | + })); |
36 | 53 | ---
|
37 | 54 |
|
38 | 55 | <StarlightPage frontmatter={{ title: "Glossary" }} headings={headings}>
|
|
0 commit comments