Repository navigation
A page in a certain language is not displayed in the TOC #3960
|
For example, if there is an A page in zh-cn language and the title of the A page is displayed in the TOC, it will not be displayed when the language is en. |
Replies: 3 comments 3 replies
|
@yangxuan74 I'm not sure I fully understand. Could you share an example project? You can create one at https://astro.new/starlight-basics |
|
I think I know what's going on here even without a repro, since it matches Starlight's documented i18n fallback mechanism exactly, just in the direction that catches people off guard. Starlight's fallback content only flows one way: from your The fix is to create the |
|
Here's a working version of the route middleware idea from above. It drops every sidebar link that has no file of its own in the current language. The default language keeps everything, since all of its pages exist.
import { defineRouteMiddleware, type StarlightRouteData } from '@astrojs/starlight/route-data';
import { getCollection } from 'astro:content';
type Entry = StarlightRouteData['sidebar'][number];
type Link = Extract<Entry, { type: 'link' }>;
// ids of pages that actually have a file, like "zh-cn/guide/a" or "en/guide/a"
let pageIds: Set<string> | undefined;
export const onRequest = defineRouteMiddleware(async ({ locals }) => {
const route = locals.starlightRoute;
if (!route.locale) return; // root locale never falls back
pageIds ??= new Set((await getCollection('docs')).map((page) => page.id));
const prefix = `${import.meta.env.BASE_URL.replace(/\/$/, '')}/${route.locale}/`;
const isFallback = (link: Link) => {
if (!link.href.startsWith(prefix)) return false;
const path = link.href.slice(prefix.length).replace(/[?#].*$/, '').replace(/\/$/, '');
return !pageIds!.has(path ? `${route.locale}/${path}` : route.locale!);
};
const keep = (entries: Entry[]): Entry[] =>
entries.flatMap<Entry>((entry) => {
if (entry.type === 'link') return isFallback(entry) ? [] : [entry];
const children = keep(entry.entries);
return children.length ? [{ ...entry, entries: children }] : [];
});
route.sidebar = keep(route.sidebar);
// otherwise prev/next can still point at a hidden page
const links: Link[] = [];
const collect = (entries: Entry[]) =>
entries.forEach((entry) => (entry.type === 'link' ? links.push(entry) : collect(entry.entries)));
collect(route.sidebar);
const current = links.findIndex((link) => link.isCurrent);
const { prev, next } = route.pagination;
if (prev && isFallback(prev)) route.pagination.prev = current > 0 ? links[current - 1] : undefined;
if (next && isFallback(next)) route.pagination.next = current !== -1 ? links[current + 1] : undefined;
});and in starlight({
// ...
routeMiddleware: './src/routeData.ts',
}),I tried it on Astro 7.4.7 + Starlight 0.42.7 with Two things to keep in mind:
|

Here's a working version of the route middleware idea from above. It drops every sidebar link that has no file of its own in the current language. The default language keeps everything, since all of its pages exist.
src/routeData.ts: