Skip to content

Commit 60fd1c3

Browse files
committed
breaking(core): rename layouts
1 parent b1a00a0 commit 60fd1c3

7 files changed

Lines changed: 106 additions & 84 deletions

File tree

.changeset/every-spies-pick.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"fumapress": minor
3+
---
4+
5+
rename layouts

apps/docs/content/docs/config.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ export default defineConfig({
2121
Specify or customize the UI (renderers) for pages.
2222

2323
```tsx title="press.config.tsx"
24-
import { createDocsLayout } from "fumapress/layouts/docs";
24+
import { createDocsLayoutPage } from "fumapress/layouts/docs";
2525
import { defineConfig } from "fumapress";
2626

2727
export default defineConfig({
2828
// ...
2929
}).useLayouts({
3030
// [!code ++] customize layout for pages
31-
page: createDocsLayout(),
31+
page: createDocsLayoutPage(),
3232
});
3333
```
3434

packages/core/src/layouts/docs.tsx

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { ConfigContext, Layouts } from "@/config";
22
import {
33
AppContext,
4+
baseLayoutProps,
45
createTransformChildren,
56
getGitHubFileUrl,
67
getLastModifiedDate,
@@ -54,58 +55,49 @@ export interface DocsLayoutContextData {
5455
) => Awaitable<DocsLayoutRenderData>)[];
5556
}
5657

57-
export function createDocsLayout<C extends ConfigContext = ConfigContext>({
58+
export function createDocsLayoutPage<C extends ConfigContext = ConfigContext>({
5859
render,
5960
}: DocsLayoutOptions<NoInfer<C>> = {}): Layouts<C>["page"] {
6061
const TDocsLayout = createTransformChildren(DocsLayout);
6162
const TDocsPage = createTransformChildren(DocsPage);
6263

63-
return async function Layout(props) {
64+
return async function Layout({ slugs, lang, ctx }) {
6465
const {
65-
slugs,
66-
lang,
6766
getLoader,
68-
siteConfig,
6967
layouts,
7068
data: { "core:docs-layout": layoutData },
71-
} = props;
69+
} = ctx;
7270
const source = await getLoader();
7371
const page = source.getPage(slugs, lang);
7472
if (!page) unstable_notFound();
7573

7674
async function getLayoutProps(
7775
overrides?: Partial<TransformChildren<DocsLayoutProps>>,
7876
): Promise<TransformChildren<DocsLayoutProps>> {
79-
const { name, git } = siteConfig;
80-
const inherit = await layouts.defaultProps?.call(props, page!);
77+
const inherit = await layouts.defaultProps?.call(ctx, { lang });
8178

8279
return mergeLayoutConfigs(
83-
{
84-
tree: source.getPageTree(lang),
85-
githubUrl: git ? `https://github.com/${git.user}/${git.repo}` : undefined,
86-
nav: {
87-
title: name,
88-
},
89-
},
80+
{ tree: source.getPageTree(lang) },
81+
baseLayoutProps(ctx),
9082
inherit,
9183
overrides,
9284
);
9385
}
9486

95-
const _raw = await render?.call(props, page);
87+
const _raw = await render?.call(ctx, page);
9688
let result: DocsLayoutRenderData = {
9789
..._raw,
98-
lastModified: _raw?.lastModified ?? (await getLastModifiedDate(props, page)),
90+
lastModified: _raw?.lastModified ?? (await getLastModifiedDate(ctx, page)),
9991
pageProps: {
10092
..._raw?.pageProps,
101-
toc: _raw?.pageProps?.toc ?? (await renderToc(props, page)),
93+
toc: _raw?.pageProps?.toc ?? (await renderToc(ctx, page)),
10294
},
10395
body:
10496
_raw?.body ??
10597
(await renderBody(
106-
props,
98+
ctx,
10799
page,
108-
"[Fumapress] Please specify the `render` option in createDocsLayout()",
100+
"[Fumapress] Please specify the `render` option in createDocsLayoutPage()",
109101
)),
110102
layoutProps: await getLayoutProps(_raw?.layoutProps),
111103
};
@@ -119,15 +111,15 @@ export function createDocsLayout<C extends ConfigContext = ConfigContext>({
119111

120112
return (
121113
<TDocsLayout props={result.layoutProps}>
122-
{renderPageMeta(page, props)}
114+
{renderPageMeta(page, ctx)}
123115
<TDocsPage props={result.pageProps}>
124116
<DocsTitle>{page.data.title}</DocsTitle>
125117
<DocsDescription className="mb-0">{page.data.description}</DocsDescription>
126118
<div className="flex flex-row gap-2 items-center border-b pt-2 pb-6">
127119
{result.markdownUrl && <MarkdownCopyButton markdownUrl={result.markdownUrl} />}
128120
<ViewOptionsPopover
129121
markdownUrl={result.markdownUrl}
130-
githubUrl={page.absolutePath ? getGitHubFileUrl(props, page.absolutePath) : undefined}
122+
githubUrl={page.absolutePath ? getGitHubFileUrl(ctx, page.absolutePath) : undefined}
131123
/>
132124
</div>
133125
<DocsBody>{result.body}</DocsBody>

packages/core/src/layouts/home.tsx

Lines changed: 61 additions & 27 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+
baseLayoutProps,
45
createTransformChildren,
56
mergeLayoutConfigs,
67
renderBody,
@@ -10,10 +11,10 @@ import {
1011
import type { Awaitable } from "@/lib/types";
1112
import type { Page } from "fumadocs-core/source";
1213
import { HomeLayout, type HomeLayoutProps } from "fumadocs-ui/layouts/home";
13-
import type { ReactNode } from "react";
14+
import type { ComponentType, ReactNode } from "react";
1415
import { unstable_notFound } from "waku/router/server";
1516

16-
export interface HomeLayoutOptions<C extends ConfigContext = ConfigContext> {
17+
export interface HomeLayoutPageOptions<C extends ConfigContext = ConfigContext> {
1718
render?: (
1819
this: AppContext<C>,
1920
page: C["loaderConfig"]["page"],
@@ -30,55 +31,42 @@ export interface HomeLayoutRenderData {
3031

3132
export interface HomeLayoutContextData {
3233
renderers?: ((
33-
this: { page: Page },
34+
this: { page: Page | undefined },
3435
data: HomeLayoutRenderData,
3536
) => Awaitable<HomeLayoutRenderData>)[];
3637
}
3738

38-
export function createHomeLayout<C extends ConfigContext = ConfigContext>({
39+
export function createHomeLayoutPage<C extends ConfigContext = ConfigContext>({
3940
render,
40-
}: HomeLayoutOptions<NoInfer<C>> = {}): Layouts<C>["page"] {
41+
}: HomeLayoutPageOptions<NoInfer<C>> = {}): Layouts<C>["page"] {
4142
const THomeLayout = createTransformChildren(HomeLayout);
4243

43-
return async function Layout(props) {
44+
return async function Layout({ slugs, lang, ctx }) {
4445
const {
45-
slugs,
46-
lang,
4746
getLoader,
48-
siteConfig,
4947
layouts,
5048
data: { "core:home-layout": layoutData },
51-
} = props;
49+
} = ctx;
5250
const source = await getLoader();
5351
const page = source.getPage(slugs, lang);
5452
if (!page) unstable_notFound();
5553

5654
async function getLayoutProps(
5755
overrides?: TransformChildren<HomeLayoutProps>,
5856
): Promise<TransformChildren<HomeLayoutProps>> {
59-
const { name, git } = siteConfig;
60-
const inherit = await layouts.defaultProps?.call(props, page!);
61-
62-
return mergeLayoutConfigs(
63-
{
64-
githubUrl: git ? `https://github.com/${git.user}/${git.repo}` : undefined,
65-
nav: {
66-
title: name,
67-
},
68-
},
69-
inherit,
70-
overrides,
71-
);
57+
const inherit = await layouts.defaultProps?.call(ctx, { lang });
58+
59+
return mergeLayoutConfigs(baseLayoutProps(ctx), inherit, overrides);
7260
}
7361

74-
const _raw = await render?.call(props, page);
62+
const _raw = await render?.call(ctx, page);
7563
let result: HomeLayoutRenderData = {
7664
body:
7765
_raw?.body ??
7866
(await renderBody(
79-
props,
67+
ctx,
8068
page,
81-
"[Fumapress] Please specify the `render` option in createHomeLayout()",
69+
"[Fumapress] Please specify the `render` option in createHomeLayoutPage()",
8270
)),
8371
layoutProps: await getLayoutProps(_raw?.layoutProps),
8472
};
@@ -92,9 +80,55 @@ export function createHomeLayout<C extends ConfigContext = ConfigContext>({
9280

9381
return (
9482
<THomeLayout props={result.layoutProps}>
95-
{renderPageMeta(page, props)}
83+
{renderPageMeta(page, ctx)}
9684
{result.body}
9785
</THomeLayout>
9886
);
9987
};
10088
}
89+
90+
export interface HomeLayoutOptions<C extends ConfigContext = ConfigContext> {
91+
render?: (this: AppContext<C>) => Awaitable<{
92+
layoutProps?: TransformChildren<HomeLayoutProps>;
93+
}>;
94+
}
95+
96+
export function createHomeLayout<C extends ConfigContext = ConfigContext>({
97+
render,
98+
}: HomeLayoutOptions<C> = {}): ComponentType<{
99+
lang?: string;
100+
children: ReactNode;
101+
ctx: AppContext<C>;
102+
}> {
103+
const THomeLayout = createTransformChildren(HomeLayout);
104+
105+
return async function Layout({ lang, children, ctx }) {
106+
const {
107+
layouts,
108+
data: { "core:home-layout": layoutData },
109+
} = ctx;
110+
111+
async function getLayoutProps(
112+
overrides?: TransformChildren<HomeLayoutProps>,
113+
): Promise<TransformChildren<HomeLayoutProps>> {
114+
const inherit = await layouts.defaultProps?.call(ctx, { lang });
115+
116+
return mergeLayoutConfigs(baseLayoutProps(ctx), inherit, overrides);
117+
}
118+
119+
const _raw = await render?.call(ctx);
120+
let result: HomeLayoutRenderData = {
121+
body: children,
122+
layoutProps: await getLayoutProps(_raw?.layoutProps),
123+
};
124+
125+
if (layoutData?.renderers) {
126+
const renderCtx = { page: undefined };
127+
for (const r of layoutData.renderers) {
128+
result = await r.call(renderCtx, result);
129+
}
130+
}
131+
132+
return <THomeLayout props={result.layoutProps}>{result.body}</THomeLayout>;
133+
};
134+
}

packages/core/src/layouts/notebook.tsx

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { ConfigContext, Layouts } from "@/config";
22
import {
33
AppContext,
4+
baseLayoutProps,
45
createTransformChildren,
56
getGitHubFileUrl,
67
getLastModifiedDate,
@@ -54,58 +55,49 @@ export interface NotebookLayoutContextData {
5455
) => Awaitable<NotebookLayoutRenderData>)[];
5556
}
5657

57-
export function createNotebookLayout<C extends ConfigContext = ConfigContext>({
58+
export function createNotebookLayoutPage<C extends ConfigContext = ConfigContext>({
5859
render,
5960
}: NotebookLayoutOptions<NoInfer<C>> = {}): Layouts<C>["page"] {
6061
const TDocsLayout = createTransformChildren(DocsLayout);
6162
const TDocsPage = createTransformChildren(DocsPage);
6263

63-
return async function Layout(props) {
64+
return async function Layout({ slugs, lang, ctx }) {
6465
const {
65-
slugs,
66-
lang,
6766
getLoader,
68-
siteConfig,
6967
layouts,
7068
data: { "core:notebook-layout": layoutData },
71-
} = props;
69+
} = ctx;
7270
const source = await getLoader();
7371
const page = source.getPage(slugs, lang);
7472
if (!page) unstable_notFound();
7573

7674
async function getLayoutProps(
7775
overrides?: Partial<TransformChildren<DocsLayoutProps>>,
7876
): Promise<TransformChildren<DocsLayoutProps>> {
79-
const { name, git } = siteConfig;
80-
const inherit = await layouts.defaultProps?.call(props, page!);
77+
const inherit = await layouts.defaultProps?.call(ctx, { lang });
8178

8279
return mergeLayoutConfigs(
83-
{
84-
tree: source.getPageTree(lang),
85-
githubUrl: git ? `https://github.com/${git.user}/${git.repo}` : undefined,
86-
nav: {
87-
title: name,
88-
},
89-
},
80+
{ tree: source.getPageTree(lang) },
81+
baseLayoutProps(ctx),
9082
inherit,
9183
overrides,
9284
);
9385
}
9486

95-
const _raw = await render?.call(props, page);
87+
const _raw = await render?.call(ctx, page);
9688
let result: NotebookLayoutRenderData = {
9789
..._raw,
98-
lastModified: _raw?.lastModified ?? (await getLastModifiedDate(props, page)),
90+
lastModified: _raw?.lastModified ?? (await getLastModifiedDate(ctx, page)),
9991
pageProps: {
10092
..._raw?.pageProps,
101-
toc: _raw?.pageProps?.toc ?? (await renderToc(props, page)),
93+
toc: _raw?.pageProps?.toc ?? (await renderToc(ctx, page)),
10294
},
10395
body:
10496
_raw?.body ??
10597
(await renderBody(
106-
props,
98+
ctx,
10799
page,
108-
"[Fumapress] Please specify the `render` option in createNotebookLayout()",
100+
"[Fumapress] Please specify the `render` option in createNotebookLayoutPage()",
109101
)),
110102
layoutProps: await getLayoutProps(_raw?.layoutProps),
111103
};
@@ -119,15 +111,15 @@ export function createNotebookLayout<C extends ConfigContext = ConfigContext>({
119111

120112
return (
121113
<TDocsLayout props={result.layoutProps}>
122-
{renderPageMeta(page, props)}
114+
{renderPageMeta(page, ctx)}
123115
<TDocsPage props={result.pageProps}>
124116
<DocsTitle>{page.data.title}</DocsTitle>
125117
<DocsDescription className="mb-0">{page.data.description}</DocsDescription>
126118
<div className="flex flex-row gap-2 items-center border-b pt-2 pb-6">
127119
{result.markdownUrl && <MarkdownCopyButton markdownUrl={result.markdownUrl} />}
128120
<ViewOptionsPopover
129121
markdownUrl={result.markdownUrl}
130-
githubUrl={page.absolutePath ? getGitHubFileUrl(props, page.absolutePath) : undefined}
122+
githubUrl={page.absolutePath ? getGitHubFileUrl(ctx, page.absolutePath) : undefined}
131123
/>
132124
</div>
133125
<DocsBody>{result.body}</DocsBody>

packages/core/src/layouts/root.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@ export interface RootLayoutOptions {
1010
export function createRootLayout<C extends ConfigContext = ConfigContext>(
1111
options?: RootLayoutOptions,
1212
): Layouts<C>["root"] {
13-
return async function (props) {
14-
const { children, lang, i18nConfig, data } = props;
15-
const hooks = data["core:provider"];
13+
return async function ({ lang, ctx, children }) {
14+
const hooks = ctx.data["core:provider"];
1615
let providerProps: RootProviderProps = {
1716
...options?.providerProps,
1817
};
1918

20-
if (i18nConfig) {
21-
const { languages } = i18nConfig as I18nConfig;
19+
if (ctx.i18nConfig) {
20+
const { languages } = ctx.i18nConfig as I18nConfig;
2221
providerProps.i18n ??= {
2322
locale: lang,
2423
locales: Object.entries(languages).map(([k, v]) => ({
@@ -39,7 +38,7 @@ export function createRootLayout<C extends ConfigContext = ConfigContext>(
3938
<html lang={lang ?? "en"} suppressHydrationWarning>
4039
<head>
4140
<style>{styles}</style>
42-
{renderRootMeta(props)}
41+
{renderRootMeta(ctx)}
4342
</head>
4443
<body data-version="1.0" className="flex flex-col min-h-screen">
4544
<RootProvider {...providerProps}>{children}</RootProvider>

0 commit comments

Comments
 (0)