Core: [Page Tree] Extract index page from folder

This commit is contained in:
Fuma Nama
2026-01-28 20:43:32 +08:00
parent 2abaff94cb
commit 099fde7f27
5 changed files with 37 additions and 23 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"fumadocs-core": patch
---
[Page Tree] Extract index page from folder
+18 -12
View File
@@ -212,7 +212,7 @@ function createPageTreeBuilderUtils(ctx: PageTreeBuilderContext) {
continue;
}
const dirNode = this.folder(path, false);
const dirNode = this.folder(path);
if (dirNode) folders.push(dirNode);
}
@@ -277,37 +277,43 @@ function createPageTreeBuilderUtils(ctx: PageTreeBuilderContext) {
if (item.startsWith(extractPrefix)) {
const path = joinPath(folderPath, item.slice(extractPrefix.length));
const node = this.folder(path, false);
const node = this.folder(path);
if (!node) return;
excludedPaths.add(path);
const children = node.index ? [node.index, ...node.children] : node.children;
if (registerOwner(folderPath, node, 2)) {
for (const child of node.children) {
for (const child of children) {
transferOwner(folderPath, child);
outputArray.push(child);
}
excludedPaths.add(path);
} else {
for (const child of node.children) {
for (const child of children) {
if (registerOwner(folderPath, child, 2)) outputArray.push(child);
}
}
return;
}
const path = resolveFlattenPath(joinPath(folderPath, item), 'page');
const node = this.folder(path, false) ?? this.file(path);
if (node) {
if (registerOwner(folderPath, node, 2)) outputArray.push(node);
excludedPaths.add(path);
let path = joinPath(folderPath, item);
let node: PageTree.Node | undefined = this.folder(path);
if (!node) {
path = resolveFlattenPath(path, 'page');
node = this.file(path);
}
if (!node) return;
if (registerOwner(folderPath, node, 2)) outputArray.push(node);
excludedPaths.add(path);
},
folder(folderPath: string, isGlobalRoot: boolean): PageTree.Folder | undefined {
folder(folderPath: string): PageTree.Folder | undefined {
const cached = pathToNode.get(folderPath);
if (cached) return cached as PageTree.Folder;
const files = ctx.storage.readDir(folderPath);
if (!files) return;
const isGlobalRoot = folderPath === '';
const metaPath = resolveFlattenPath(joinPath(folderPath, 'meta'), 'meta');
const indexPath = resolveFlattenPath(joinPath(folderPath, 'index'), 'page');
let meta = ctx.storage.read(metaPath);
@@ -417,7 +423,7 @@ function createPageTreeBuilderUtils(ctx: PageTreeBuilderContext) {
return item;
},
root(): PageTree.Root {
const folder = this.folder('', true)!;
const folder = this.folder('')!;
let root: PageTree.Root = {
$id: ctx.rootId,
name: folder.name || 'Docs',
@@ -23,6 +23,12 @@
"type": "page",
"url": "/c/test",
},
{
"$id": "root:b/index.mdx",
"name": "Page B (Index)",
"type": "page",
"url": "/b",
},
{
"$id": "root:b/test.mdx",
"name": "Page B (Test)",
+7
View File
@@ -46,6 +46,13 @@ export const withPriority: Source = {
pages: ['test', '...'],
},
},
{
type: 'page',
path: '/b/index.mdx',
data: {
title: 'Page B (Index)',
},
},
{
type: 'page',
path: '/b/hello.mdx',
+1 -11
View File
@@ -381,6 +381,7 @@ test('Loader: Serialize data', async () => {
},
});
removeUndefined(result.pageTree, true);
const prev = JSON.stringify(result.pageTree);
expect(await result.serializePageTree(result.pageTree)).toMatchInlineSnapshot(`
@@ -391,8 +392,6 @@ test('Loader: Serialize data', async () => {
"children": [
{
"$id": "root:test.mdx",
"$ref": undefined,
"description": undefined,
"icon": "<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-rocket" aria-hidden="true"><path d="M4.5 16.5c-1.5 1.26-2 5-2 5s3.74-.5 5-2c.71-.84.7-2.13-.09-2.91a2.18 2.18 0 0 0-2.91-.09z"></path><path d="m12 15-3-3a22 22 0 0 1 2-3.95A12.88 12.88 0 0 1 22 2c0 2.72-.78 7.5-6 11a22.35 22.35 0 0 1-4 2z"></path><path d="M9 12H4s.55-3.03 2-4c1.62-1.08 5 0 5 0"></path><path d="M12 15v5s3.03-.55 4-2c1.08-1.62 0-5 0-5"></path></svg>",
"name": "Hello &lt;Foo&gt;",
"type": "page",
@@ -400,7 +399,6 @@ test('Loader: Serialize data', async () => {
},
{
"$id": "root:hello",
"$ref": undefined,
"children": [
{
"$id": "root:_0",
@@ -410,20 +408,12 @@ test('Loader: Serialize data', async () => {
},
{
"$id": "root:hello/index.mdx",
"$ref": undefined,
"description": undefined,
"icon": undefined,
"name": "Hello",
"type": "page",
"url": "/hello",
},
],
"collapsible": undefined,
"defaultOpen": undefined,
"description": undefined,
"icon": undefined,
"name": "Hello Folder",
"root": undefined,
"type": "folder",
},
],