Core: Ignore numeric locale file name

This commit is contained in:
fuma-nama
2024-08-28 12:20:52 +08:00
parent b0aaf78756
commit f8cc1672c8
3 changed files with 23 additions and 2 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'fumadocs-core': patch
---
Ignore numeric locale file name
+11 -2
View File
@@ -34,7 +34,7 @@ export function parseFilePath(path: string): FileInfo {
.filter((p) => p.length > 0) .filter((p) => p.length > 0)
.join('/'); .join('/');
const [name, locale] = nameWithLocale.split('.'); const [name, locale] = getLocale(nameWithLocale);
return { return {
dirname, dirname,
name, name,
@@ -47,7 +47,7 @@ export function parseFilePath(path: string): FileInfo {
export function parseFolderPath(path: string): FileInfo { export function parseFolderPath(path: string): FileInfo {
const segments = splitPath(slash(path)); const segments = splitPath(slash(path));
const base = segments.at(-1) ?? ''; const base = segments.at(-1) ?? '';
const [name, locale] = base.split('.'); const [name, locale] = getLocale(base);
const flattenedPath = segments.join('/'); const flattenedPath = segments.join('/');
return { return {
@@ -59,6 +59,15 @@ export function parseFolderPath(path: string): FileInfo {
}; };
} }
function getLocale(name: string): [string, string?] {
const sep = name.lastIndexOf('.');
if (sep === -1) return [name];
const locale = name.slice(sep + 1);
if (/\d+/.exec(locale)) return [name];
return [name.slice(0, sep), locale];
}
/** /**
* @param path - Relative path * @param path - Relative path
* @returns Normalized path, with no trailing/leading slashes * @returns Normalized path, with no trailing/leading slashes
+7
View File
@@ -64,6 +64,13 @@ describe('Path utilities', () => {
path: 'nested/test.cn.mdx', path: 'nested/test.cn.mdx',
}); });
expect(parseFilePath('nested/test.01.mdx')).toEqual({
dirname: 'nested',
name: 'test.01',
flattenedPath: 'nested/test.01',
path: 'nested/test.01.mdx',
});
expect(parseFilePath('nested\\test.cn.mdx')).toEqual( expect(parseFilePath('nested\\test.cn.mdx')).toEqual(
parseFilePath('nested/test.cn.mdx'), parseFilePath('nested/test.cn.mdx'),
); );