|
1 | 1 | import { SidebarItem } from "vocs";
|
2 |
| -import fs from 'fs'; |
3 |
| -import path from 'path'; |
| 2 | +import fs from "fs"; |
| 3 | +import path from "path"; |
4 | 4 |
|
5 | 5 | function capitalize(text: string): string {
|
6 |
| - return text.charAt(0).toUpperCase() + text.slice(1) |
| 6 | + return text.charAt(0).toUpperCase() + text.slice(1); |
7 | 7 | }
|
8 | 8 |
|
9 |
| - |
10 | 9 | function generateSidebar(dirPath: string, parentDir: string): SidebarItem[] {
|
11 |
| - const entries = fs.readdirSync(dirPath, { withFileTypes: true }); |
12 |
| - |
13 |
| - const items: SidebarItem[] = []; |
14 |
| - |
15 |
| - entries.forEach(entry => { |
16 |
| - // If entry is file end with '.stories.mdx', add sidebar item with link |
17 |
| - if (entry.isFile() && entry.name.endsWith('.stories.mdx')) { |
18 |
| - const fileName = entry.name.replace('.stories.mdx', '') |
19 |
| - if (fileName === 'index') { |
20 |
| - items.unshift({ |
21 |
| - text: capitalize(fileName.replace(/-/g, ' ')), |
22 |
| - link: parentDir |
23 |
| - }); |
24 |
| - } else { |
25 |
| - items.push({ |
26 |
| - text: capitalize(fileName.replace(/-/g, ' ')), |
27 |
| - link: `${parentDir}/${fileName}` |
28 |
| - }); |
29 |
| - } |
30 |
| - return |
31 |
| - } |
32 |
| - |
33 |
| - // If entry is directory, add collapsable sidebar item |
34 |
| - if (entry.isDirectory()) { |
35 |
| - const subDirPath = path.join(dirPath, entry.name); |
36 |
| - const subItems = generateSidebar(subDirPath, `${parentDir}/${entry.name}`); |
37 |
| - if (subItems.length === 0) |
38 |
| - return |
39 |
| - |
40 |
| - const item = { |
41 |
| - text: capitalize(entry.name), |
42 |
| - collapsed: false, |
43 |
| - items: subItems |
44 |
| - } |
45 |
| - items.push(item); |
46 |
| - } |
47 |
| - }); |
48 |
| - |
49 |
| - return items; |
| 10 | + const entries = fs.readdirSync(dirPath, { withFileTypes: true }); |
| 11 | + |
| 12 | + const items: SidebarItem[] = []; |
| 13 | + |
| 14 | + entries.forEach((entry) => { |
| 15 | + // If entry is file end with '.stories.mdx', add sidebar item with link |
| 16 | + if (entry.isFile() && entry.name.endsWith(".stories.mdx")) { |
| 17 | + const fileName = entry.name.replace(".stories.mdx", ""); |
| 18 | + if (fileName === "index") { |
| 19 | + items.unshift({ |
| 20 | + text: capitalize(fileName.replace(/-/g, " ")), |
| 21 | + link: parentDir, |
| 22 | + }); |
| 23 | + } else { |
| 24 | + items.push({ |
| 25 | + text: capitalize(fileName.replace(/-/g, " ")), |
| 26 | + link: `${parentDir}/${fileName}`, |
| 27 | + }); |
| 28 | + } |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + // If entry is directory, add collapsable sidebar item |
| 33 | + if (entry.isDirectory()) { |
| 34 | + const subDirPath = path.join(dirPath, entry.name); |
| 35 | + const subItems = generateSidebar( |
| 36 | + subDirPath, |
| 37 | + `${parentDir}/${entry.name}`, |
| 38 | + ); |
| 39 | + if (subItems.length === 0) return; |
| 40 | + |
| 41 | + const item = { |
| 42 | + text: capitalize(entry.name), |
| 43 | + collapsed: false, |
| 44 | + items: subItems, |
| 45 | + }; |
| 46 | + items.push(item); |
| 47 | + } |
| 48 | + }); |
| 49 | + |
| 50 | + return items; |
50 | 51 | }
|
51 | 52 |
|
52 |
| -const TOP_LEVEL_ORDER = ['Intro', 'Patterns', 'Components', 'Draft'] |
53 |
| -export const sidebar = generateSidebar('./pages', '').sort((a, b) => { |
54 |
| - return TOP_LEVEL_ORDER.indexOf(a.text) - TOP_LEVEL_ORDER.indexOf(b.text) |
| 53 | +const TOP_LEVEL_ORDER = ["Intro", "Patterns", "Components", "Draft"]; |
| 54 | +export const sidebar = generateSidebar("./pages", "").sort((a, b) => { |
| 55 | + return TOP_LEVEL_ORDER.indexOf(a.text) - TOP_LEVEL_ORDER.indexOf(b.text); |
55 | 56 | });
|
0 commit comments