|
1 | 1 | import SideBar from '@node-core/ui-components/Containers/Sidebar'; |
| 2 | +import { relative } from '@node-core/doc-kit/src/utils/url.mjs'; |
| 3 | +import { pages } from '#theme/config'; |
2 | 4 |
|
3 | | -import { sideNav } from '../config.json' with { type: 'json' }; |
| 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 | +]; |
4 | 18 |
|
5 | | -/** |
6 | | - * Redirect to a URL |
7 | | - * @param {string} url URL |
8 | | - */ |
| 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 */ |
9 | 28 | const redirect = url => (window.location.href = url); |
10 | 29 |
|
| 30 | +const PrefetchLink = props => <a {...props} rel="prefetch" />; |
| 31 | + |
11 | 32 | /** |
12 | 33 | * Sidebar component for MDX documentation with page navigation |
13 | | - * @param {{ pathname: string }} props |
14 | 34 | */ |
15 | | -export default ({ pathname }) => ( |
16 | | - <SideBar |
17 | | - pathname={pathname} |
18 | | - groups={sideNav} |
19 | | - onSelect={redirect} |
20 | | - as={props => <a {...props} rel="prefetch" />} |
21 | | - title="Navigation" |
22 | | - /> |
23 | | -); |
| 35 | +export default ({ metadata }) => { |
| 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 ? pathname : `${relative(path, currentPath)}.html`, |
| 45 | + })), |
| 46 | + })); |
| 47 | + |
| 48 | + return ( |
| 49 | + <SideBar |
| 50 | + pathname={pathname} |
| 51 | + groups={groups} |
| 52 | + onSelect={redirect} |
| 53 | + as={PrefetchLink} |
| 54 | + title="Navigation" |
| 55 | + /> |
| 56 | + ); |
| 57 | +}; |
0 commit comments