diff --git a/docs/src/app/js/[...path]/route.ts b/docs/src/app/js/[...path]/route.ts new file mode 100644 index 000000000..caeb4078e --- /dev/null +++ b/docs/src/app/js/[...path]/route.ts @@ -0,0 +1,31 @@ +import { source } from 'lib/source'; +import { notFound, redirect } from 'next/navigation'; +import { NextRequest } from 'next/server'; + +export function GET(request: NextRequest) { + const pathname = new URL(request.url).pathname; + + // Ensure we have the correct target path without double prefixes using proper URL construction + let targetPath: string; + if (pathname.startsWith('/docs')) { + targetPath = pathname; + } else { + // Remove leading slash and use as relative path to properly construct /docs prefix + targetPath = new URL(pathname.substring(1), 'file:///docs/').pathname; + } + + // Extract slug by removing any '/docs' prefix and splitting by '/' + const cleanPath = pathname.startsWith('/docs') ? pathname.substring(5) : pathname; + const slug = cleanPath.substring(1).split('/').filter(Boolean); + + // Check if the target page exists + const page = source.getPage(slug); + + if (page) { + // Page exists, redirect to the full path + return redirect(targetPath); + } else { + // Page doesn't exist, return 404 + return notFound(); + } +} diff --git a/docs/src/app/next/[...path]/route.ts b/docs/src/app/next/[...path]/route.ts new file mode 100644 index 000000000..0e328050f --- /dev/null +++ b/docs/src/app/next/[...path]/route.ts @@ -0,0 +1,31 @@ +import { source } from 'lib/source'; +import { notFound, redirect } from 'next/navigation'; +import { NextRequest } from 'next/server'; + +export function GET(request: NextRequest) { + const pathname = new URL(request.url).pathname; + + // Ensure we have the correct target path without double prefixes using proper URL construction + let targetPath: string; + if (pathname.startsWith('/docs')) { + targetPath = pathname; + } else { + // Remove leading slash and use as relative path to properly construct /docs prefix + targetPath = new URL(pathname.substring(1), 'file:///docs/').pathname; + } + + // Extract slug by removing any '/docs' prefix and splitting by '/' + const cleanPath = pathname.startsWith('/docs') ? pathname.substring(5) : pathname; + const slug = cleanPath.substring(1).split('/').filter(Boolean); + + // Check if the target page exists + const page = source.getPage(slug); + + if (page) { + // Page exists, redirect to the full path + return redirect(targetPath); + } else { + // Page doesn't exist, redirect to overview + return notFound(); + } +} diff --git a/docs/src/app/python/[...path]/route.ts b/docs/src/app/python/[...path]/route.ts new file mode 100644 index 000000000..0e328050f --- /dev/null +++ b/docs/src/app/python/[...path]/route.ts @@ -0,0 +1,31 @@ +import { source } from 'lib/source'; +import { notFound, redirect } from 'next/navigation'; +import { NextRequest } from 'next/server'; + +export function GET(request: NextRequest) { + const pathname = new URL(request.url).pathname; + + // Ensure we have the correct target path without double prefixes using proper URL construction + let targetPath: string; + if (pathname.startsWith('/docs')) { + targetPath = pathname; + } else { + // Remove leading slash and use as relative path to properly construct /docs prefix + targetPath = new URL(pathname.substring(1), 'file:///docs/').pathname; + } + + // Extract slug by removing any '/docs' prefix and splitting by '/' + const cleanPath = pathname.startsWith('/docs') ? pathname.substring(5) : pathname; + const slug = cleanPath.substring(1).split('/').filter(Boolean); + + // Check if the target page exists + const page = source.getPage(slug); + + if (page) { + // Page exists, redirect to the full path + return redirect(targetPath); + } else { + // Page doesn't exist, redirect to overview + return notFound(); + } +} diff --git a/docs/src/app/react/[...path]/route.ts b/docs/src/app/react/[...path]/route.ts new file mode 100644 index 000000000..0e328050f --- /dev/null +++ b/docs/src/app/react/[...path]/route.ts @@ -0,0 +1,31 @@ +import { source } from 'lib/source'; +import { notFound, redirect } from 'next/navigation'; +import { NextRequest } from 'next/server'; + +export function GET(request: NextRequest) { + const pathname = new URL(request.url).pathname; + + // Ensure we have the correct target path without double prefixes using proper URL construction + let targetPath: string; + if (pathname.startsWith('/docs')) { + targetPath = pathname; + } else { + // Remove leading slash and use as relative path to properly construct /docs prefix + targetPath = new URL(pathname.substring(1), 'file:///docs/').pathname; + } + + // Extract slug by removing any '/docs' prefix and splitting by '/' + const cleanPath = pathname.startsWith('/docs') ? pathname.substring(5) : pathname; + const slug = cleanPath.substring(1).split('/').filter(Boolean); + + // Check if the target page exists + const page = source.getPage(slug); + + if (page) { + // Page exists, redirect to the full path + return redirect(targetPath); + } else { + // Page doesn't exist, redirect to overview + return notFound(); + } +} diff --git a/docs/src/app/rest-api/[...path]/route.ts b/docs/src/app/rest-api/[...path]/route.ts new file mode 100644 index 000000000..9653e7bfd --- /dev/null +++ b/docs/src/app/rest-api/[...path]/route.ts @@ -0,0 +1,31 @@ +import { apiSource } from 'lib/source'; +import { notFound, redirect } from 'next/navigation'; +import { NextRequest } from 'next/server'; + +export function GET(request: NextRequest) { + const pathname = new URL(request.url).pathname; + + // For rest-api, we redirect to /api not /docs using proper URL construction + let targetPath: string; + if (pathname.startsWith('/api')) { + targetPath = pathname; + } else { + // Remove leading slash and use as relative path to properly construct /api prefix + targetPath = new URL(pathname.substring(1), 'file:///api/').pathname; + } + + // Extract slug by removing any '/api' prefix and splitting by '/' + const cleanPath = pathname.startsWith('/api') ? pathname.substring(4) : pathname; + const slug = cleanPath.substring(1).split('/').filter(Boolean); + + // Check if the target page exists using apiSource for API docs + const page = apiSource.getPage(slug); + + if (page) { + // Page exists, redirect to the full path + return redirect(targetPath); + } else { + // Page doesn't exist, redirect to overview + return notFound(); + } +}