@@ -9,31 +9,61 @@ import path from 'path'
99
1010const JSXParser = Parser . extend ( acornJsx ( ) )
1111
12- const getAstToInject = function ( locale , outline ) {
12+ const getAstToInject = function ( pagePath , outline ) {
13+ const isDynamic = pagePath . includes ( '[locale]' )
14+ const locale = isDynamic ? null : pagePath . substring ( 0 , 2 )
15+
1316 return {
1417 type : 'mdxjsEsm' ,
1518 value : '' ,
1619 data : {
1720 estree : JSXParser . parse (
1821 `
1922 import { MDXLayout as _MDXLayout } from '@/layout'
23+ import { Locale } from '@/i18n'
2024 import { getNavItems } from '@/navigation'
2125
22- export const getStaticProps = async () => {
23- const navItems = await getNavItems('${ locale } ')
26+ const outline = ${ Serializer . run ( outline ) }
2427
25- return {
26- props: {
27- navItems,
28- },
28+ ${
29+ isDynamic
30+ ? `
31+ export const getStaticPaths = async () => {
32+ return {
33+ paths: Object.values(Locale).map((locale) => ({
34+ params: { locale },
35+ })),
36+ fallback: false,
37+ }
2938 }
39+
40+ export const getStaticProps = async (context) => {
41+ const navItems = await getNavItems(context.params?.locale)
42+
43+ return {
44+ props: {
45+ navItems,
46+ },
47+ }
48+ }
49+ `
50+ : `
51+ export const getStaticProps = async () => {
52+ const navItems = await getNavItems('${ locale } ')
53+
54+ return {
55+ props: {
56+ navItems,
57+ },
58+ }
59+ }
60+ `
3061 }
3162
3263 export default function ({ navItems, children }) {
33- const outline = ${ Serializer . run ( outline ) }
34-
3564 return (
3665 <_MDXLayout
66+ pagePath="${ pagePath } "
3767 navItems={navItems}
3868 frontmatter={typeof frontmatter !== 'undefined' ? frontmatter : undefined}
3969 outline={outline}
@@ -82,9 +112,8 @@ export const remarkMdxLayout = () => {
82112 } )
83113 } )
84114
85- const relativePath = path . relative ( path . join ( process . cwd ( ) , 'pages' ) , file . path )
86- const locale = relativePath . substring ( 0 , 2 )
87- const astToInject = getAstToInject ( locale , outline )
115+ const pagePath = path . relative ( path . join ( process . cwd ( ) , 'pages' ) , file . path )
116+ const astToInject = getAstToInject ( pagePath , outline )
88117
89118 ast . children . push ( astToInject )
90119 }
0 commit comments