|
1 | 1 | import SideBar from '@node-core/ui-components/Containers/Sidebar'; |
2 | 2 | import { relative } from '@node-core/doc-kit/src/utils/url.mjs'; |
3 | | - |
4 | 3 | import { pages } from '#theme/config'; |
5 | 4 |
|
6 | | -/** |
7 | | - * Redirect to a URL |
8 | | - * @param {string} url URL |
9 | | - */ |
| 5 | +/** @type {Array<[string, string]>} */ |
| 6 | +const categories = [ |
| 7 | + ['getting-started', 'Getting Started'], |
| 8 | + ['command-line', 'Command Line'], |
| 9 | + ['http', 'HTTP'], |
| 10 | + ['file-system', 'File System'], |
| 11 | + ['asynchronous-work', 'Asynchronous Work'], |
| 12 | + ['typescript', 'TypeScript'], |
| 13 | + ['package-management', 'Package Management'], |
| 14 | + ['diagnostics', 'Diagnostics'], |
| 15 | + ['testing', 'Testing'], |
| 16 | + ['security', 'Security'], |
| 17 | +]; |
| 18 | + |
| 19 | +/** @type {Map<string, Array<{ heading: string, path: string }>>} */ |
| 20 | +const byDir = new Map(); |
| 21 | +for (const [heading, path] of pages) { |
| 22 | + const dir = path.split('/')[1]; |
| 23 | + if (!byDir.has(dir)) byDir.set(dir, []); |
| 24 | + byDir.get(dir).push({ heading, path }); |
| 25 | +} |
| 26 | + |
| 27 | +/** @param {string} url */ |
10 | 28 | const redirect = url => (window.location.href = url); |
11 | 29 |
|
| 30 | +const PrefetchLink = props => <a {...props} rel="prefetch" />; |
| 31 | + |
12 | 32 | /** |
13 | 33 | * Sidebar component for MDX documentation with page navigation |
14 | 34 | */ |
15 | 35 | export default ({ metadata }) => { |
16 | | - const items = pages.map(([heading, path]) => ({ |
17 | | - label: heading, |
18 | | - link: |
19 | | - metadata.path === path |
20 | | - ? `${metadata.basename}.html` |
21 | | - : `${relative(path, metadata.path)}.html`, |
| 36 | + const { path: currentPath, basename } = metadata; |
| 37 | + const pathname = `${basename}.html`; |
| 38 | + |
| 39 | + const groups = categories.map(([dir, title]) => ({ |
| 40 | + groupName: title, |
| 41 | + items: byDir.get(dir).map(({ heading, path }) => ({ |
| 42 | + label: heading, |
| 43 | + link: |
| 44 | + currentPath === path |
| 45 | + ? pathname |
| 46 | + : `${relative(path, currentPath)}.html`, |
| 47 | + })), |
22 | 48 | })); |
23 | 49 |
|
24 | 50 | return ( |
25 | 51 | <SideBar |
26 | | - pathname={`${metadata.basename}.html`} |
27 | | - groups={[{ groupName: 'Learn Articles', items }]} |
| 52 | + pathname={pathname} |
| 53 | + groups={groups} |
28 | 54 | onSelect={redirect} |
29 | | - as={props => <a {...props} rel="prefetch" />} |
| 55 | + as={PrefetchLink} |
30 | 56 | title="Navigation" |
31 | 57 | /> |
32 | 58 | ); |
|
0 commit comments