Core: Fix wrong index files output in i18n page tree generation

This commit is contained in:
Fuma Nama
2025-04-09 18:23:02 +08:00
parent d49f9ae526
commit 3a4bd88a97
6 changed files with 33 additions and 26 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'fumadocs-core': patch
---
Fix wrong index files output in i18n page tree generation
+1 -1
View File
@@ -4,7 +4,7 @@
"private": true,
"scripts": {
"build": "next build",
"dev": "next dev",
"dev": "next dev --turbo",
"start": "next start"
},
"dependencies": {
+12 -17
View File
@@ -94,20 +94,13 @@ function buildAll(
if (node.file.name === 'index') {
if (!skipIndex) output.unshift(treeNode);
continue;
} else {
output.push(treeNode);
}
output.push(treeNode);
}
if ('children' in node) {
const folder = buildFolderNode(node, false, ctx);
if (folder.children.length === 0 && folder.index) {
output.push(folder.index);
} else {
folders.push(folder);
}
folders.push(buildFolderNode(node, false, ctx));
}
}
@@ -193,22 +186,21 @@ function buildFolderNode(
ctx.localeStorage?.read(metaPath, 'meta') ??
ctx.storage.read(metaPath, 'meta');
const indexFile = ctx.storage.read(
joinPath(folder.file.path, 'index'),
'page',
);
const indexPath = joinPath(folder.file.path, 'index');
const indexFile =
ctx.localeStorage?.read(indexPath, 'page') ??
ctx.storage.read(indexPath, 'page');
const metadata = meta?.data;
const isRoot = metadata?.root ?? isGlobalRoot;
const index = indexFile ? buildFileNode(indexFile, ctx) : undefined;
const addedNodePaths = new Set<string>();
let children: PageTree.Node[];
if (!metadata?.pages) {
children = buildAll(folder.children, ctx, !isRoot);
} else {
const addedNodePaths = new Set<string>();
const resolved = metadata?.pages?.flatMap<
PageTree.Node | typeof rest | typeof restReversed
>((item, i) => {
@@ -245,7 +237,10 @@ function buildFolderNode(
root: metadata?.root,
defaultOpen: metadata?.defaultOpen,
description: metadata?.description,
index,
index:
isRoot || (indexFile && !addedNodePaths.has(indexFile.file.path))
? index
: undefined,
children,
$id: folder.file.path,
$ref: !ctx.options.noRef
+12 -6
View File
@@ -566,18 +566,24 @@ test('Loader: Without meta.json', () => {
{
"$id": "root",
"children": [
{
"$id": "hello/index.mdx",
"name": "Hello",
"type": "page",
"url": "/hello",
},
{
"$id": "test.mdx",
"name": "Hello",
"type": "page",
"url": "/test",
},
{
"$id": "hello",
"children": [],
"index": {
"$id": "hello/index.mdx",
"name": "Hello",
"type": "page",
"url": "/hello",
},
"name": "Hello",
"type": "folder",
},
],
"name": "",
}
+1 -1
View File
@@ -88,7 +88,7 @@ export default async function loader(
source,
path: filePath,
},
`invalid frontmatter in ${filePath}:`,
`invalid frontmatter in ${filePath}`,
)) as Record<string, unknown>;
}
+2 -1
View File
@@ -1,8 +1,9 @@
'use client';
import { ThemeProvider } from 'next-themes';
import {
import React, {
type ComponentPropsWithoutRef,
Fragment,
lazy,
type ReactNode,
useMemo,