refactor: review docs routes

This commit is contained in:
Francesca Giannino
2026-02-06 18:31:00 +01:00
parent de20f2e91f
commit ef5e6c3671
4 changed files with 21 additions and 11 deletions

View File

@@ -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}`;
}

View File

@@ -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,
},

View File

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

View File

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