diff --git a/components/SideBar.jsx b/components/SideBar.jsx index 725e5e57..ffa79547 100644 --- a/components/SideBar.jsx +++ b/components/SideBar.jsx @@ -1,27 +1,42 @@ +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); const PrefetchLink = props => ; +// The versioned API docs pass `sidebar` as an array of groups; the main site keys it by section. +const isVersioned = Array.isArray(sidebar); + +// Versioned pages expose a version relative path, we prefix it so it matches the absolute sidebar links. +const versionBase = isVersioned ? `/docs/api/v${version.major}.x` : ''; + const pathnameFor = path => { - const clean = path.replace(/\/index$/, ''); - if (!clean) return '/'; - return clean.startsWith('/') ? clean : `/${clean}`; + const clean = path.replace(/\/index$/, '').replace(/^\//, ''); + return (clean ? `${versionBase}/${clean}` : versionBase) || '/'; }; 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`, +})); + +// Preselect the picker with the version this API build was generated for. +const currentVersion = isVersioned ? versionBase : undefined; + +/** Docs sidebar, plus a webpack version picker on the versioned API docs. */ export default ({ metadata }) => ( ( onSelect={redirect} as={PrefetchLink} title="Navigation" - /> + > + {isVersioned && ( +
+