Skip to content

Commit 754b2d0

Browse files
committed
feat(core): add merge for
1 parent 95ccccb commit 754b2d0

8 files changed

Lines changed: 57 additions & 43 deletions

File tree

packages/core/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"zod": "^4.4.3"
5454
},
5555
"devDependencies": {
56+
"@fastify/deepmerge": "^3.2.1",
5657
"@tailwindcss/oxide": "^4.3.0",
5758
"@types/mdx": "^2.0.13",
5859
"@types/node": "^25.7.0",
@@ -82,5 +83,8 @@
8283
"@types/react": {
8384
"optional": true
8485
}
86+
},
87+
"inlinedDependencies": {
88+
"@fastify/deepmerge": "3.2.1"
8589
}
8690
}

packages/core/src/config.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,7 @@ export interface Layouts<C extends ConfigContext = ConfigContext> {
4545
notFound: ComponentType<AppContext<C> & { lang?: string }>;
4646

4747
/**
48-
* Define default props for page layouts.
49-
* This will also be ignored if you specify a custom page layout wtih the `render` option.
48+
* Define default props for page layouts, will be merged with current props.
5049
*/
5150
defaultProps?: (
5251
this: AppContext<C>,

packages/core/src/layouts/docs.tsx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { ConfigContext, Layouts } from "@/config";
22
import {
33
AppContext,
44
getGitHubFileUrl,
5+
mergeLayoutConfigs,
56
renderPageMeta,
67
TransformChildren,
78
TransformChildrenSlot,
@@ -30,7 +31,7 @@ export interface DocsLayoutOptions<C extends ConfigContext = ConfigContext> {
3031
markdownUrl?: string;
3132
lastModified?: Date | null;
3233
body?: ReactNode;
33-
layoutProps?: TransformChildren<Partial<DocsLayoutProps>>;
34+
layoutProps?: Partial<TransformChildren<DocsLayoutProps>>;
3435
pageProps?: TransformChildren<DocsPageProps>;
3536
}>;
3637
}
@@ -83,23 +84,22 @@ export function createDocsLayout<C extends ConfigContext = ConfigContext>({
8384
if (!page) unstable_notFound();
8485

8586
async function getLayoutProps(
86-
overrides?: TransformChildren<Partial<DocsLayoutProps>>,
87+
overrides?: Partial<TransformChildren<DocsLayoutProps>>,
8788
): Promise<TransformChildren<DocsLayoutProps>> {
8889
const { name, git } = siteConfig;
90+
const inherit = await layouts.defaultProps?.call(props, page!);
8991

90-
if (layouts.defaultProps) {
91-
overrides ??= await layouts.defaultProps.call(props, page!);
92-
}
93-
94-
return {
95-
tree: source.getPageTree(lang),
96-
githubUrl: git ? `https://github.com/${git.user}/${git.repo}` : undefined,
97-
...overrides,
98-
nav: {
99-
title: name,
100-
...overrides?.nav,
92+
return mergeLayoutConfigs(
93+
{
94+
tree: source.getPageTree(lang),
95+
githubUrl: git ? `https://github.com/${git.user}/${git.repo}` : undefined,
96+
nav: {
97+
title: name,
98+
},
10199
},
102-
};
100+
inherit,
101+
overrides,
102+
);
103103
}
104104

105105
const _raw = await render?.call(props, page);

packages/core/src/layouts/home.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { ConfigContext, Layouts } from "@/config";
22
import {
33
type AppContext,
4+
mergeLayoutConfigs,
45
renderPageMeta,
56
TransformChildren,
67
TransformChildrenSlot,
@@ -62,19 +63,18 @@ export function createHomeLayout<C extends ConfigContext = ConfigContext>({
6263
overrides?: TransformChildren<HomeLayoutProps>,
6364
): Promise<TransformChildren<HomeLayoutProps>> {
6465
const { name, git } = siteConfig;
66+
const inherit = await layouts.defaultProps?.call(props, page!);
6567

66-
if (layouts.defaultProps) {
67-
overrides ??= await layouts.defaultProps.call(props, page!);
68-
}
69-
70-
return {
71-
githubUrl: git ? `https://github.com/${git.user}/${git.repo}` : undefined,
72-
...overrides,
73-
nav: {
74-
title: name,
75-
...overrides?.nav,
68+
return mergeLayoutConfigs(
69+
{
70+
githubUrl: git ? `https://github.com/${git.user}/${git.repo}` : undefined,
71+
nav: {
72+
title: name,
73+
},
7674
},
77-
};
75+
inherit,
76+
overrides,
77+
);
7878
}
7979

8080
const _raw = await render?.call(props, page);

packages/core/src/layouts/notebook.tsx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { ConfigContext, Layouts } from "@/config";
22
import {
33
AppContext,
44
getGitHubFileUrl,
5+
mergeLayoutConfigs,
56
renderPageMeta,
67
TransformChildren,
78
TransformChildrenSlot,
@@ -30,7 +31,7 @@ export interface NotebookLayoutOptions<C extends ConfigContext = ConfigContext>
3031
markdownUrl?: string;
3132
lastModified?: Date | null;
3233
body?: ReactNode;
33-
layoutProps?: TransformChildren<Partial<DocsLayoutProps>>;
34+
layoutProps?: Partial<TransformChildren<DocsLayoutProps>>;
3435
pageProps?: TransformChildren<DocsPageProps>;
3536
}>;
3637
}
@@ -83,23 +84,22 @@ export function createNotebookLayout<C extends ConfigContext = ConfigContext>({
8384
if (!page) unstable_notFound();
8485

8586
async function getLayoutProps(
86-
overrides?: TransformChildren<Partial<DocsLayoutProps>>,
87+
overrides?: Partial<TransformChildren<DocsLayoutProps>>,
8788
): Promise<TransformChildren<DocsLayoutProps>> {
8889
const { name, git } = siteConfig;
90+
const inherit = await layouts.defaultProps?.call(props, page!);
8991

90-
if (layouts.defaultProps) {
91-
overrides ??= await layouts.defaultProps.call(props, page!);
92-
}
93-
94-
return {
95-
tree: source.getPageTree(lang),
96-
githubUrl: git ? `https://github.com/${git.user}/${git.repo}` : undefined,
97-
...overrides,
98-
nav: {
99-
title: name,
100-
...overrides?.nav,
92+
return mergeLayoutConfigs(
93+
{
94+
tree: source.getPageTree(lang),
95+
githubUrl: git ? `https://github.com/${git.user}/${git.repo}` : undefined,
96+
nav: {
97+
title: name,
98+
},
10199
},
102-
};
100+
inherit,
101+
overrides,
102+
);
103103
}
104104

105105
const _raw = await render?.call(props, page);

packages/core/src/lib/shared.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import path from "node:path";
44
import type { LoaderOutput, Page } from "fumadocs-core/source";
55
import type { Awaitable, Adapter, ServerPlugin } from "./types";
66
import type { DocsLayoutContextData } from "@/layouts/docs";
7-
import { ComponentType, Fragment, type ReactNode } from "react";
7+
import { type ComponentType, Fragment, type ReactNode } from "react";
88
import type { HomeLayoutContextData } from "@/layouts/home";
99
import { fumadocsMdx } from "@/adapters/mdx";
1010
import type { RootProviderProps } from "fumadocs-ui/provider/waku";
1111
import type { NotebookLayoutContextData } from "@/layouts/notebook";
12+
import createDeepmerge from "@fastify/deepmerge";
1213

1314
export interface AppContext<C extends ConfigContext = ConfigContext> {
1415
mode: BuildMode;
@@ -130,3 +131,5 @@ export function TransformChildrenSlot<T>({
130131

131132
return <Comp {...props}>{children}</Comp>;
132133
}
134+
135+
export const mergeLayoutConfigs = createDeepmerge({ all: true, onlyDefinedProperties: true });

packages/core/tsdown.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default defineConfig({
2121
},
2222
},
2323
deps: {
24-
onlyBundle: [],
24+
onlyBundle: ["@fastify/deepmerge"],
2525
neverBundle: [/^virtual:/, /^node:/],
2626
},
2727
async onSuccess() {

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)