11import type { ConfigContext , Layouts } from "@/config" ;
22import {
33 type AppContext ,
4+ baseLayoutProps ,
45 createTransformChildren ,
56 mergeLayoutConfigs ,
67 renderBody ,
@@ -10,10 +11,10 @@ import {
1011import type { Awaitable } from "@/lib/types" ;
1112import type { Page } from "fumadocs-core/source" ;
1213import { HomeLayout , type HomeLayoutProps } from "fumadocs-ui/layouts/home" ;
13- import type { ReactNode } from "react" ;
14+ import type { ComponentType , ReactNode } from "react" ;
1415import { 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
3132export 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+ }
0 commit comments