Skip to content

Commit 590d36a

Browse files
committed
Core: Support findSiblings() page tree utility
1 parent 25f1f0c commit 590d36a

File tree

5 files changed

+28
-9
lines changed

5 files changed

+28
-9
lines changed

.changeset/kind-banks-tie.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"fumadocs-core": patch
3+
---
4+
5+
Support `findSiblings()` page tree utility

packages/core/src/page-tree/utils.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,30 @@ export function getPageTreeRoots(
6565
}
6666

6767
/**
68-
* Get other page tree nodes that lives under the same parent
68+
* Get other item nodes that lives under the same parent.
6969
*/
7070
export function getPageTreePeers(
7171
treeOrTrees: PageTree.Root | Record<string, PageTree.Root>,
7272
url: string,
7373
): PageTree.Item[] {
74+
return findSiblings(treeOrTrees, url).filter((item) => item.type === 'page');
75+
}
76+
77+
/**
78+
* Get other tree nodes that lives under the same parent.
79+
*/
80+
export function findSiblings(
81+
treeOrTrees: PageTree.Root | Record<string, PageTree.Root>,
82+
url: string,
83+
): PageTree.Node[] {
7484
// Check if it's a single tree or multiple trees (i18n)
7585
if ('children' in treeOrTrees) {
7686
// Single tree case
7787
const tree = treeOrTrees as PageTree.Root;
78-
const parent = findParentFromTree(tree, url);
88+
const parent = findParent(tree, url);
7989
if (!parent) return [];
8090

81-
return parent.children.filter(
82-
(item) => item.type === 'page' && item.url !== url,
83-
) as PageTree.Item[];
91+
return parent.children;
8492
}
8593

8694
// Multiple trees case
@@ -92,7 +100,7 @@ export function getPageTreePeers(
92100
return [];
93101
}
94102

95-
function findParentFromTree(
103+
export function findParent(
96104
from: PageTree.Root | PageTree.Folder,
97105
url: string,
98106
): PageTree.Root | PageTree.Folder | undefined {
@@ -157,6 +165,12 @@ export function findPath(
157165

158166
const VisitBreak = Symbol('VisitBreak');
159167

168+
/**
169+
* Perform a depth-first search on page tree visiting every node.
170+
*
171+
* @param root - the root of page tree to visit.
172+
* @param visitor - function to receive nodes, return `skip` to skip the children of current node, `break` to stop the search entirely.
173+
*/
160174
export function visit<Root extends PageTree.Node | PageTree.Root>(
161175
root: Root,
162176
visitor: <T extends PageTree.Node | PageTree.Root>(

packages/core/src/source/page-tree/builder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ function createPageTreeBuilderUtils(ctx: PageTreeBuilderContext) {
292292
const folderName = basename(folderPath);
293293
return pathToName(group.exec(folderName)?.[1] ?? folderName);
294294
})(),
295-
icon: metadata.icon,
295+
icon: metadata.icon ?? index?.icon,
296296
root: metadata.root,
297297
defaultOpen: metadata.defaultOpen,
298298
description: metadata.description,

packages/core/src/source/plugins/lucide-icons.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function lucideIconsPlugin(
1515
return iconPlugin((icon = defaultIcon) => {
1616
if (icon === undefined) return;
1717
const Icon = icons[icon as keyof typeof icons];
18-
if (!icon) {
18+
if (!Icon) {
1919
console.warn(`[lucide-icons-plugin] Unknown icon detected: ${icon}.`);
2020
return;
2121
}

packages/mdx/src/utils/fuma-matter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ interface Output {
1212
data: unknown;
1313
}
1414

15-
const regex = /^---\r?\n(.+?)\r?\n---\r?\n/s;
15+
const regex = /^---\r?\n(.+?)\r?\n---\r?\n?/s;
1616

1717
/**
1818
* parse frontmatter, it supports only yaml format

0 commit comments

Comments
 (0)