@@ -48,8 +48,38 @@ const breadcrumbSchema = {
4848 item: item .url ,
4949 })),
5050};
51+
52+ // API version redirect - check if we need to redirect to stored preference
53+ const currentPath = Astro .url .pathname ;
54+ const apiMatch = currentPath .match (/ ^ \/ api\/ ([^ /] + )/ );
55+ const isApiPage = apiMatch !== null ;
56+
57+ // Build version map for the inline script
58+ import { API_VERSIONS , versionToSlug , getVersionFromPath } from ' @/lib/api-versions' ;
59+ const versionMap = Object .fromEntries (
60+ API_VERSIONS .map (v => [v .id , versionToSlug (v .id )])
61+ );
62+ const currentVersionId = isApiPage ? getVersionFromPath (currentPath ) : null ;
5163---
5264
65+ { /* Version redirect script - must run immediately before any content */ }
66+ { isApiPage && (
67+ <script is :inline define :vars = { { versionMap , currentVersionId }} >
68+ (function() {
69+ var STORAGE_KEY = ' sprites-api-version' ;
70+ var storedVersion = localStorage .getItem (STORAGE_KEY );
71+ if (! storedVersion || ! currentVersionId || storedVersion === currentVersionId ) return ;
72+ var storedSlug = versionMap [storedVersion ];
73+ if (! storedSlug ) return ;
74+ var path = window .location .pathname ;
75+ var match = path .match (/ ^ \/ api\/ [^ /] + \/ ? (. * )$ / );
76+ var page = match ? match [1 ] : ' ' ;
77+ var newPath = ' /api/' + storedSlug + (page ? ' /' + page : ' /' );
78+ window .location .replace (newPath );
79+ } )();
80+ </script >
81+ )}
82+
5383<Default {... Astro .props }><slot /></Default >
5484<Background />
5585
@@ -219,9 +249,13 @@ const breadcrumbSchema = {
219249 function initSidebarNavigation() {
220250 // Normalize path by removing trailing slash for comparison
221251 const normalizePath = (path) => path.replace(/\/$/, '') || '/';
252+
253+ // Strip API version from path for comparison (e.g., /api/v001-rc30/sprites -> /api/sprites)
254+ const stripApiVersion = (path) => path.replace(/^\/api\/[^/]+/, '/api');
255+
222256 const currentPath = normalizePath(window.location.pathname);
257+ const currentPathNoVersion = stripApiVersion(currentPath);
223258 const currentHash = window.location.hash;
224- const currentUrl = currentPath + currentHash;
225259
226260 // Find all nested sidebar groups within API Reference
227261 const sidebarGroups = document.querySelectorAll('nav[aria-label="Main"] details details');
@@ -237,10 +271,11 @@ const breadcrumbSchema = {
237271 // Extract the page URL without the anchor and normalize
238272 const href = firstLink.getAttribute('href') || '';
239273 const pageUrl = normalizePath(href.split('#')[0]);
274+ const pageUrlNoVersion = stripApiVersion(pageUrl);
240275 if (!pageUrl) return;
241276
242- // Auto-expand if current page matches this group's page
243- if (currentPath === pageUrl ) {
277+ // Auto-expand if current page matches this group's page (comparing without version)
278+ if (currentPathNoVersion === pageUrlNoVersion ) {
244279 details.setAttribute('open', '');
245280 }
246281
@@ -249,15 +284,15 @@ const breadcrumbSchema = {
249284 allLinks.forEach((link) => {
250285 const linkHref = link.getAttribute('href') || '';
251286 const linkPath = normalizePath(linkHref.split('#')[0]);
287+ const linkPathNoVersion = stripApiVersion(linkPath);
252288 const linkHash = linkHref.includes('#') ? '#' + linkHref.split('#')[1] : '';
253- const fullLinkUrl = linkPath + linkHash;
254289
255290 // Only mark as current if:
256- // 1. Link has a hash and it matches the current URL exactly (path + hash)
257- // 2. Link has no hash and we're on that exact page with no hash
291+ // 1. Link has a hash and path matches (ignoring version) + hash matches exactly
292+ // 2. Link has no hash and we're on that exact page (ignoring version) with no hash
258293 const isCurrentLink = linkHash
259- ? fullLinkUrl === currentUrl
260- : (linkPath === currentPath && !currentHash);
294+ ? (linkPathNoVersion === currentPathNoVersion && linkHash === currentHash)
295+ : (linkPathNoVersion === currentPathNoVersion && !currentHash);
261296
262297 if (isCurrentLink) {
263298 link.setAttribute('data-current', 'true');
@@ -275,14 +310,15 @@ const breadcrumbSchema = {
275310 // Allow normal toggle behavior with modifier keys
276311 if (e.shiftKey || e.ctrlKey || e.metaKey) return;
277312
278- // If currently closed, expand and navigate to page
279- if (!details.open) {
313+ // If currently closed and we're NOT already on this page, expand and navigate
314+ // (Compare without version to handle being on different version of same page)
315+ if (!details.open && currentPathNoVersion !== pageUrlNoVersion) {
280316 e.preventDefault();
281317 e.stopPropagation();
282318 // Navigate to the page (without anchor hash)
283319 window.location.href = pageUrl;
284320 }
285- // If already open, just toggle closed ( default behavior)
321+ // If already on this page or already open, let default toggle behavior happen
286322 });
287323
288324 // Make summary look clickable
0 commit comments