mirror of
https://github.com/expressjs/expressjs.com.git
synced 2026-02-26 03:35:16 +00:00
refactor: review docs routes
This commit is contained in:
@@ -23,6 +23,8 @@ export function resolveHref(
|
||||
): string {
|
||||
if (versioned && basePath) {
|
||||
return `/${lang}${basePath}/${version}${href}`;
|
||||
} else if (versioned) {
|
||||
return `/${lang}/${version}${href}`;
|
||||
} else if (basePath) {
|
||||
return `/${lang}${basePath}${href}`;
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ export const mainMenu: Menu = {
|
||||
ariaLabel: 'Documentation',
|
||||
icon: 'document-bullet-list-multiple-20-regular',
|
||||
submenu: {
|
||||
basePath: '/docs',
|
||||
versioned: true,
|
||||
sections: docsMenu.sections,
|
||||
},
|
||||
|
||||
@@ -27,18 +27,15 @@ export async function getStaticPaths() {
|
||||
});
|
||||
});
|
||||
|
||||
// Also create non-versioned paths that serve default version content
|
||||
// Find pages in the default version folder and create alias paths
|
||||
const defaultVersionPages = pages.filter((page) => {
|
||||
const [, ...slugParts] = page.id.split('/');
|
||||
return slugParts[0] === DEFAULT_VERSION;
|
||||
});
|
||||
|
||||
defaultVersionPages.forEach((page) => {
|
||||
const [lang, , ...restSlugParts] = page.id.split('/'); // Skip lang and version
|
||||
const [lang, , ...restSlugParts] = page.id.split('/');
|
||||
const nonVersionedSlug = restSlugParts.join('/');
|
||||
|
||||
// Only add if this path doesn't already exist
|
||||
const exists = paths.some((p) => p.params.lang === lang && p.params.slug === nonVersionedSlug);
|
||||
|
||||
if (!exists && nonVersionedSlug) {
|
||||
@@ -55,7 +52,7 @@ export async function getStaticPaths() {
|
||||
const { page, version, isVersionedPath } = Astro.props;
|
||||
const { lang, slug } = Astro.params;
|
||||
const { Content } = await render(page);
|
||||
const breadcrumbs = await buildBreadcrumbs(lang, slug, 'docs');
|
||||
const breadcrumbs = await buildBreadcrumbs(lang, slug, 'docs', version);
|
||||
const { title, description } = page.data;
|
||||
---
|
||||
|
||||
@@ -50,28 +50,40 @@ function formatLabel(segment: string): string {
|
||||
* @param lang - The language code (e.g., 'en')
|
||||
* @param slug - The page slug (e.g., 'resources/middleware/compression')
|
||||
* @param collection - The collection name to check for existing content
|
||||
* @param version - The version to display in breadcrumbs (e.g., 'v5')
|
||||
* @returns Array of breadcrumb items with labels and optional hrefs
|
||||
*/
|
||||
export async function buildBreadcrumbs(
|
||||
lang: string,
|
||||
slug: string,
|
||||
collection: keyof typeof collections
|
||||
collection: keyof typeof collections,
|
||||
version?: string
|
||||
): Promise<BreadcrumbItem[]> {
|
||||
const pages = await getCollection(collection);
|
||||
|
||||
const breadcrumbs: BreadcrumbItem[] = [];
|
||||
|
||||
// Add collection as first breadcrumb
|
||||
breadcrumbs.push({
|
||||
label: getCollectionLabel(collection),
|
||||
href: undefined,
|
||||
});
|
||||
|
||||
// Add version to breadcrumbs if provided
|
||||
if (version) {
|
||||
breadcrumbs.push({
|
||||
label: version,
|
||||
href: undefined,
|
||||
});
|
||||
}
|
||||
|
||||
const slugParts = slug.split('/');
|
||||
|
||||
slugParts.forEach((part, index) => {
|
||||
const isLast = index === slugParts.length - 1;
|
||||
const pathToSegment = `${lang}/${slugParts.slice(0, index + 1).join('/')}`;
|
||||
// Skip version prefix in slug if it matches the version parameter
|
||||
const startIndex = slugParts[0] === version ? 1 : 0;
|
||||
|
||||
slugParts.slice(startIndex).forEach((part, index) => {
|
||||
const isLast = index === slugParts.slice(startIndex).length - 1;
|
||||
const pathToSegment = `${lang}/${slugParts.slice(0, startIndex + index + 1).join('/')}`;
|
||||
|
||||
if (!isLast) {
|
||||
breadcrumbs.push({
|
||||
|
||||
Reference in New Issue
Block a user