From 814670f24c09daf8cb3a6f73885d63022f25e910 Mon Sep 17 00:00:00 2001 From: Madison Date: Wed, 23 Jul 2025 14:44:52 -0500 Subject: [PATCH] update platform-nav script, and docs-layout-router for proper type-safe solution for PLATFORMS --- docs/scripts/generate-platform-navigation.js | 17 +++++++++++--- .../components/layouts/docs-layout-router.tsx | 23 +++++++++++++++---- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/docs/scripts/generate-platform-navigation.js b/docs/scripts/generate-platform-navigation.js index b823ad078..e4aa21758 100644 --- a/docs/scripts/generate-platform-navigation.js +++ b/docs/scripts/generate-platform-navigation.js @@ -54,6 +54,16 @@ ${config.pages.map(page => { }).join('\n')} ]; +/** + * Safely join URL path segments + */ +function joinUrlPath(...segments: string[]): string { + return segments + .filter(segment => segment && segment.length > 0) + .join('/') + .replace(/\\/+/g, '/'); // Remove duplicate slashes +} + /** * Check if a specific page exists for a given platform */ @@ -69,7 +79,7 @@ export function pageExistsForPlatform(path: string, platform: Platform): boolean if (!page && !pathWithExt.includes('/index.mdx')) { const indexPath = normalizedPath.endsWith('.mdx') ? normalizedPath.replace('.mdx', '/index.mdx') - : \`\${normalizedPath}/index.mdx\`; + : joinUrlPath(normalizedPath, 'index.mdx'); page = PLATFORM_PAGES.find(p => p.path === indexPath); } @@ -86,11 +96,12 @@ export function getSmartPlatformRedirect(currentPath: string, targetPlatform: Pl // If the exact same page exists for target platform, use it if (pageExistsForPlatform(pathWithoutPlatform, targetPlatform)) { - return \`/docs/\${targetPlatform}/\${pathWithoutPlatform.replace(/\\.mdx$/, '')}\`; + const cleanPath = pathWithoutPlatform.replace(/\\.mdx$/, ''); + return joinUrlPath('/docs', targetPlatform, cleanPath); } // Otherwise, redirect to overview - return \`/docs/\${targetPlatform}/overview\`; + return joinUrlPath('/docs', targetPlatform, 'overview'); } /** diff --git a/docs/src/components/layouts/docs-layout-router.tsx b/docs/src/components/layouts/docs-layout-router.tsx index 9c6849183..49c1239e1 100644 --- a/docs/src/components/layouts/docs-layout-router.tsx +++ b/docs/src/components/layouts/docs-layout-router.tsx @@ -149,26 +149,39 @@ export function DynamicDocsLayout({ children, ...props }: DynamicDocsLayoutProps // Extract current platform from pathname const currentPlatform = getCurrentPlatform(pathname); + // Helper function to safely cast platform to Platform type + const isPlatform = (platform: string): platform is Platform => { + return ['next', 'react', 'js', 'python'].includes(platform); + }; + return PLATFORMS.map(platform => { + // Safe type guard - if this fails, something is seriously wrong with PLATFORMS constant + if (!isPlatform(platform)) { + console.error(`Invalid platform in PLATFORMS array: ${platform}`); + // Fallback to a safe default to prevent runtime errors + platform = 'next'; + } + + const platformType = platform as Platform; let url: string; if (isInSdkSection(pathname)) { // For SDK section: check if platform supports SDK, otherwise use smart redirect - if (platformSupportsSDK(platform as Platform)) { + if (platformSupportsSDK(platformType)) { url = `/docs/${platform}/sdk`; } else { - url = getSmartRedirectUrl(pathname, platform as Platform); + url = getSmartRedirectUrl(pathname, platformType); } } else if (isInComponentsSection(pathname)) { // For Components section: check if platform supports components, otherwise use smart redirect - if (platformSupportsComponents(platform as Platform)) { + if (platformSupportsComponents(platformType)) { url = `/docs/${platform}/components`; } else { - url = getSmartRedirectUrl(pathname, platform as Platform); + url = getSmartRedirectUrl(pathname, platformType); } } else { // For normal docs: use smart redirect - url = getSmartRedirectUrl(pathname, platform as Platform); + url = getSmartRedirectUrl(pathname, platformType); } return {