Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions components/SideBar.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import Select from '@node-core/ui-components/Common/Select';
import SideBar from '@node-core/ui-components/Containers/Sidebar';
import { major } from 'semver';
import { sidebar } from '#theme/local/site';
import { version } from '#theme/config';
import versions from '../versions.json' with { type: 'json' };

/** @param {string} url */
const redirect = url => (window.location.href = url);
Expand All @@ -12,22 +16,45 @@ const pathnameFor = path => {
return clean.startsWith('/') ? clean : `/${clean}`;
};

// The versioned API docs pass `sidebar` as an array of groups; the main site keys it by section.
const isVersioned = Array.isArray(sidebar);

const groupsFor = path => {
if (Array.isArray(sidebar)) return sidebar;
if (isVersioned) return sidebar;

const segment = path.split('/').filter(Boolean)[0];
return sidebar[segment] ?? [];
};

/**
* Sidebar component for MDX documentation with page navigation.
*/
const versionItems = versions.map(version => ({
value: `/docs/api/v${major(version)}.x`,
label: `v${major(version)}.x`,
}));
Comment on lines +29 to +32

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can go outside the function, right?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's already sits at the top of the component , so it is only build once when the file loads.


// Preselect the picker with the version this API build was generated for.
const currentVersion = isVersioned
? `/docs/api/v${version.major}.x`
: undefined;

/** Docs sidebar, plus a webpack version picker on the versioned API docs. */
export default ({ metadata }) => (
<SideBar
pathname={pathnameFor(metadata.path)}
groups={groupsFor(metadata.path)}
onSelect={redirect}
as={PrefetchLink}
title="Navigation"
/>
>
{isVersioned && (
<div>
<Select
label="webpack version"
values={versionItems}
value={currentVersion}
inline
onChange={redirect}
/>
</div>
)}
</SideBar>
);
7 changes: 5 additions & 2 deletions scripts/markdown/governance.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ const FILE_MAP = {
'AI_POLICY.md': { output: 'ai-policy', label: 'AI Policy' },
};

const pageLink = output =>
output === 'index' ? '/about/governance' : `/about/governance/${output}`;

// Derived from FILE_MAP - stays in sync automatically if entries are added/removed.
const LINK_REWRITE_MAP = Object.fromEntries(
Object.entries(FILE_MAP).map(([source, { output }]) => [
source,
`/about/governance/${output}`,
pageLink(output),
])
);

Expand Down Expand Up @@ -77,7 +80,7 @@ const siteJson = {
{
groupName: 'Governance',
items: fetched.map(({ output, label }) => ({
link: `/about/governance/${output}`,
link: pageLink(output),
label,
})),
},
Expand Down
Loading