update platform-nav script, and docs-layout-router for proper type-safe solution for PLATFORMS

This commit is contained in:
Madison 2025-07-23 14:44:52 -05:00
parent adc9da4948
commit 814670f24c
2 changed files with 32 additions and 8 deletions

View File

@ -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');
}
/**

View File

@ -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 {