Core: Make loader().getPages() to return pages from all languages when locale is not specified

This commit is contained in:
Fuma Nama
2025-09-25 21:11:31 +08:00
parent 055858f0a4
commit 71bce864a8
4 changed files with 20 additions and 6 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'fumadocs-core': patch
---
Make `loader().getPages()` to return pages from all languages when locale is not specified
@@ -105,7 +105,7 @@ Get a list of page available for locale.
```ts
import { source } from '@/lib/source';
// from default locale
// from any locale
source.getPages();
// for a specific locale
+13 -4
View File
@@ -40,6 +40,7 @@ export interface LoaderOptions<
url?: UrlFn;
source: Source<T> | Source<T>[];
// TODO: solve the overlaps between page tree transformers & storage transformers, like a plugin API?
transformers?: Transformer[];
/**
@@ -144,15 +145,21 @@ export interface LoaderOutput<Config extends LoaderConfig> {
}
| undefined;
/**
* @internal
*/
_i18n?: I18nConfig;
/**
* Get list of pages from language
* Get a list of pages from specified language
*
* @param language - If empty, the default language will be used
* @param language - If empty, list pages from all languages.
*/
getPages: (language?: string) => Page<Config['source']['pageData']>[];
/**
* get each language and its pages, empty if i18n is not enabled.
*/
getLanguages: () => LanguageEntry<Config['source']['pageData']>[];
/**
@@ -396,11 +403,13 @@ function createOutput(options: LoaderOptions): LoaderOutput<LoaderConfig> {
hash,
};
},
getPages(language = defaultLanguage) {
getPages(language) {
const pages: Page[] = [];
for (const [key, value] of walker.pages.entries()) {
if (key.startsWith(`${language}.`)) pages.push(value);
if (language === undefined || key.startsWith(`${language}.`)) {
pages.push(value);
}
}
return pages;
+1 -1
View File
@@ -121,7 +121,7 @@ test('Internationalized Routing: Hide Prefix', async () => {
await expect(removeUndefined(result.pageTree, true)).toMatchFileSnapshot(
'./fixtures/page-trees/i18n-no-prefix.tree.json',
);
expect(result.getPages().length).toBe(2);
expect(result.getPages().length).toBe(4);
expect(result.getPage(['test'])?.url).toBe('/test');
expect(result.getPage(['test'], 'cn')?.url).toBe('/cn/test');
});