Skip to content

Commit ab07609

Browse files
author
Dimitri POSTOLOV
authoredSep 6, 2023
[v3] remove locale and defaultLocale from normalizePages (#2264)

File tree

22 files changed

+210
-1453
lines changed

22 files changed

+210
-1453
lines changed
 

‎.changeset/metal-flowers-lie.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'nextra': major
3+
---
4+
5+
remove `locale` and `defaultLocale` from `normalizePages`

‎examples/swr-site/pages/en/docs/_meta.json

+11-11
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,28 @@
33
"title": "Getting Started",
44
"type": "separator"
55
},
6-
"getting-started": {},
7-
"options": {},
8-
"global-configuration": {},
9-
"data-fetching": {},
6+
"getting-started": "",
7+
"options": "",
8+
"global-configuration": "",
9+
"data-fetching": "",
1010
"error-handling": {
1111
"display": "hidden"
1212
},
1313
"revalidation": "Auto Revalidation",
1414
"conditional-fetching": "Conditional Data Fetching",
15-
"arguments": {},
16-
"mutation": {},
17-
"pagination": {},
15+
"arguments": "",
16+
"mutation": "",
17+
"pagination": "",
1818
"--- my_new_separator": {
1919
"title": "Advanced",
2020
"type": "separator"
2121
},
22-
"prefetching": {},
22+
"prefetching": "",
2323
"with-nextjs": "Next.js SSG and SSR",
2424
"typescript": "TypeScript :)",
25-
"suspense": {},
26-
"middleware": {},
27-
"advanced": {},
25+
"suspense": "",
26+
"middleware": "",
27+
"advanced": "",
2828
"change-log": {
2929
"theme": {
3030
"sidebar": false

‎examples/swr-site/pages/en/docs/data-fetching.mdx

+11-9
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ function App() {
3636
<Callout>
3737
If you are using <strong>Next.js</strong>, you don't need to import this
3838
polyfill:
39-
<br />
40-
[New Built-In Polyfills: fetch(), URL, and
41-
Object.assign](https://nextjs.org/blog/next-9-1-7#new-built-in-polyfills-fetch-url-and-objectassign)
39+
40+
[New Built-In Polyfills: fetch(), URL, and Object.assign](https://nextjs.org/blog/next-9-1-7#new-built-in-polyfills-fetch-url-and-objectassign)
41+
4242
</Callout>
4343

4444
## Axios
@@ -66,14 +66,16 @@ const fetcher = query => request('/api/graphql', query)
6666

6767
function App() {
6868
const { data, error } = useSWR(
69-
`{
70-
Movie(title: "Inception") {
71-
releaseDate
72-
actors {
73-
name
69+
/* GraphQL */ `
70+
{
71+
Movie(title: "Inception") {
72+
releaseDate
73+
actors {
74+
name
75+
}
7476
}
7577
}
76-
}`,
78+
`,
7779
fetcher
7880
)
7981
// ...

‎examples/swr-site/pages/en/examples/_meta.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"basic": "Basic Usage",
33
"auth": "Authentication",
4-
"infinite-loading": {},
5-
"error-handling": {},
4+
"infinite-loading": "",
5+
"error-handling": "",
66
"ssr": "Next.js SSR",
77
"full": {
88
"theme": {

‎examples/swr-site/pages/es/docs/data-fetching.mdx

+11-9
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ function App() {
3535

3636
<Callout>
3737
Si estás usando <strong>Next.js</strong>, no necesita importar este polyfill:
38-
<br />
39-
[Nuevos Polyfills Incorporados: fetch(), URL, y
40-
Object.assign](https://nextjs.org/blog/next-9-1-7#new-built-in-polyfills-fetch-url-and-objectassign)
38+
39+
[Nuevos Polyfills Incorporados: fetch(), URL, y Object.assign](https://nextjs.org/blog/next-9-1-7#new-built-in-polyfills-fetch-url-and-objectassign)
40+
4141
</Callout>
4242

4343
## Axios
@@ -65,14 +65,16 @@ const fetcher = query => request('/api/graphql', query)
6565

6666
function App() {
6767
const { data, error } = useSWR(
68-
`{
69-
Movie(title: "Inception") {
70-
releaseDate
71-
actors {
72-
name
68+
/* GraphQL */ `
69+
{
70+
Movie(title: "Inception") {
71+
releaseDate
72+
actors {
73+
name
74+
}
7375
}
7476
}
75-
}`,
77+
`,
7678
fetcher
7779
)
7880
// ...

‎examples/swr-site/pages/ru/docs/data-fetching.mdx

+11-9
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ function App() {
3636
<Callout>
3737
Если вы используете <strong>Next.js</strong>, вам не нужно импортировать этот
3838
полифил:
39-
<br />
40-
[Новые встроенные полифилы: fetch(), URL, и
41-
Object.assign](https://nextjs.org/blog/next-9-1-7#new-built-in-polyfills-fetch-url-and-objectassign)
39+
40+
[Новые встроенные полифилы: fetch(), URL, и Object.assign](https://nextjs.org/blog/next-9-1-7#new-built-in-polyfills-fetch-url-and-objectassign)
41+
4242
</Callout>
4343

4444
## Axios
@@ -66,14 +66,16 @@ const fetcher = query => request('/api/graphql', query)
6666

6767
function App() {
6868
const { data, error } = useSWR(
69-
`{
70-
Movie(title: "Начало") {
71-
releaseDate
72-
actors {
73-
name
69+
/* GraphQL */ `
70+
{
71+
Movie(title: "Начало") {
72+
releaseDate
73+
actors {
74+
name
75+
}
7476
}
7577
}
76-
}`,
78+
`,
7779
fetcher
7880
)
7981
// ...

‎packages/nextra-theme-blog/__test__/__fixture__/pageMap.ts

-12
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export const indexOpts: BlogPageOpts = {
1212
kind: 'MdxPage',
1313
name: 'index',
1414
route: '/',
15-
locale: '',
1615
frontMatter: {
1716
type: 'page',
1817
title: 'About',
@@ -27,7 +26,6 @@ export const indexOpts: BlogPageOpts = {
2726
kind: 'MdxPage',
2827
name: 'aaron-swartz-a-programmable-web',
2928
route: '/posts/aaron-swartz-a-programmable-web',
30-
locale: '',
3129
frontMatter: {
3230
title: 'Notes on A Programmable Web by Aaron Swartz',
3331
date: '2016/5/21',
@@ -41,7 +39,6 @@ export const indexOpts: BlogPageOpts = {
4139
kind: 'MdxPage',
4240
name: 'index',
4341
route: '/posts',
44-
locale: '',
4542
frontMatter: {
4643
type: 'posts',
4744
title: 'Random Thoughts',
@@ -59,7 +56,6 @@ export const indexOpts: BlogPageOpts = {
5956
kind: 'MdxPage',
6057
name: '[tag]',
6158
route: '/tags/[tag]',
62-
locale: '',
6359
frontMatter: {
6460
type: 'tag'
6561
}
@@ -90,7 +86,6 @@ export const postsOpts: BlogPageOpts = {
9086
kind: 'MdxPage',
9187
name: 'index',
9288
route: '/',
93-
locale: '',
9489
frontMatter: {
9590
type: 'page',
9691
title: 'About',
@@ -105,7 +100,6 @@ export const postsOpts: BlogPageOpts = {
105100
kind: 'MdxPage',
106101
name: 'aaron-swartz-a-programmable-web',
107102
route: '/posts/aaron-swartz-a-programmable-web',
108-
locale: '',
109103
frontMatter: {
110104
title: 'Notes on A Programmable Web by Aaron Swartz',
111105
date: '2016/5/21',
@@ -119,7 +113,6 @@ export const postsOpts: BlogPageOpts = {
119113
kind: 'MdxPage',
120114
name: 'index',
121115
route: '/posts',
122-
locale: '',
123116
frontMatter: {
124117
type: 'posts',
125118
title: 'Random Thoughts',
@@ -137,7 +130,6 @@ export const postsOpts: BlogPageOpts = {
137130
kind: 'MdxPage',
138131
name: '[tag]',
139132
route: '/tags/[tag]',
140-
locale: '',
141133
frontMatter: {
142134
type: 'tag'
143135
}
@@ -171,7 +163,6 @@ export const articleOpts: BlogPageOpts = {
171163
kind: 'MdxPage',
172164
name: 'index',
173165
route: '/',
174-
locale: '',
175166
frontMatter: {
176167
type: 'page',
177168
title: 'About',
@@ -186,7 +177,6 @@ export const articleOpts: BlogPageOpts = {
186177
kind: 'MdxPage',
187178
name: 'aaron-swartz-a-programmable-web',
188179
route: '/posts/aaron-swartz-a-programmable-web',
189-
locale: '',
190180
frontMatter: {
191181
title: 'Notes on A Programmable Web by Aaron Swartz',
192182
date: '2016/5/21',
@@ -200,7 +190,6 @@ export const articleOpts: BlogPageOpts = {
200190
kind: 'MdxPage',
201191
name: 'index',
202192
route: '/posts',
203-
locale: '',
204193
frontMatter: {
205194
type: 'posts',
206195
title: 'Random Thoughts',
@@ -218,7 +207,6 @@ export const articleOpts: BlogPageOpts = {
218207
kind: 'MdxPage',
219208
name: '[tag]',
220209
route: '/tags/[tag]',
221-
locale: '',
222210
frontMatter: {
223211
type: 'tag'
224212
}

‎packages/nextra-theme-blog/__test__/collect.spec.ts

-9
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ describe('collect', () => {
2727
"type": "posts",
2828
},
2929
"kind": "MdxPage",
30-
"locale": "",
3130
"name": "index",
3231
"route": "/posts",
3332
},
@@ -39,7 +38,6 @@ describe('collect', () => {
3938
"type": "page",
4039
},
4140
"kind": "MdxPage",
42-
"locale": "",
4341
"name": "index",
4442
"route": "/",
4543
},
@@ -54,7 +52,6 @@ describe('collect', () => {
5452
"title": "Notes on A Programmable Web by Aaron Swartz",
5553
},
5654
"kind": "MdxPage",
57-
"locale": "",
5855
"name": "aaron-swartz-a-programmable-web",
5956
"route": "/posts/aaron-swartz-a-programmable-web",
6057
},
@@ -76,7 +73,6 @@ describe('collect', () => {
7673
"type": "posts",
7774
},
7875
"kind": "MdxPage",
79-
"locale": "",
8076
"name": "index",
8177
"route": "/posts",
8278
},
@@ -88,7 +84,6 @@ describe('collect', () => {
8884
"type": "page",
8985
},
9086
"kind": "MdxPage",
91-
"locale": "",
9287
"name": "index",
9388
"route": "/",
9489
},
@@ -103,7 +98,6 @@ describe('collect', () => {
10398
"title": "Notes on A Programmable Web by Aaron Swartz",
10499
},
105100
"kind": "MdxPage",
106-
"locale": "",
107101
"name": "aaron-swartz-a-programmable-web",
108102
"route": "/posts/aaron-swartz-a-programmable-web",
109103
},
@@ -127,7 +121,6 @@ describe('collect', () => {
127121
"type": "posts",
128122
},
129123
"kind": "MdxPage",
130-
"locale": "",
131124
"name": "index",
132125
"route": "/posts",
133126
},
@@ -139,7 +132,6 @@ describe('collect', () => {
139132
"type": "page",
140133
},
141134
"kind": "MdxPage",
142-
"locale": "",
143135
"name": "index",
144136
"route": "/",
145137
},
@@ -154,7 +146,6 @@ describe('collect', () => {
154146
"title": "Notes on A Programmable Web by Aaron Swartz",
155147
},
156148
"kind": "MdxPage",
157-
"locale": "",
158149
"name": "aaron-swartz-a-programmable-web",
159150
"route": "/posts/aaron-swartz-a-programmable-web",
160151
},

‎packages/nextra-theme-blog/__test__/parent.spec.ts

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ describe('parent', () => {
2222
"type": "posts",
2323
},
2424
"kind": "MdxPage",
25-
"locale": "",
2625
"name": "index",
2726
"route": "/posts",
2827
},

‎packages/nextra-theme-docs/src/index.tsx

+7-13
Original file line numberDiff line numberDiff line change
@@ -106,36 +106,30 @@ function Body({
106106
)
107107
}
108108

109-
const InnerLayout = ({
109+
function InnerLayout({
110110
filePath,
111111
pageMap,
112112
frontMatter,
113113
headings,
114114
timestamp,
115115
children
116-
}: PageOpts & { children: ReactNode }): ReactElement => {
116+
}: PageOpts & { children: ReactNode }): ReactElement {
117117
const config = useConfig()
118-
const { locale = DEFAULT_LOCALE, defaultLocale } = useRouter()
118+
const { locale = DEFAULT_LOCALE } = useRouter()
119119
const fsPath = useFSRoute()
120120

121121
const {
122122
activeType,
123123
activeIndex,
124124
activeThemeContext,
125125
activePath,
126-
topLevelNavbarItems,
126+
directories,
127127
docsDirectories,
128128
flatDocsDirectories,
129-
directories
129+
topLevelNavbarItems
130130
} = useMemo(
131-
() =>
132-
normalizePages({
133-
list: pageMap,
134-
locale,
135-
defaultLocale,
136-
route: fsPath
137-
}),
138-
[pageMap, locale, defaultLocale, fsPath]
131+
() => normalizePages({ list: pageMap, route: fsPath }),
132+
[pageMap, fsPath]
139133
)
140134

141135
const themeContext = { ...activeThemeContext, ...frontMatter }

‎packages/nextra-theme-docs/src/schemas.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { NextSeoProps } from 'next-seo'
22
import type { FC, ReactNode } from 'react'
33
import { isValidElement } from 'react'
4-
import * as z from 'zod' // use `import * as z` for better tree-shaking https://github.com/colinhacks/zod/issues/2596#issuecomment-1643053289
4+
import { z } from 'zod'
55
import type { NavBarProps } from './components/navbar'
66
import type { TOCProps } from './components/toc'
77

‎packages/nextra/__test__/__snapshots__/normalize-page.spec.ts.snap

+13-400
Large diffs are not rendered by default.

‎packages/nextra/__test__/__snapshots__/page-map.test.ts.snap

+48-13
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ exports[`Page Process > should match i18n site page maps 1`] = `
4949
"title": "Examples",
5050
"type": "page",
5151
},
52+
"foo": "Foo",
5253
"index": {
5354
"display": "hidden",
5455
"title": "Introduction",
@@ -65,6 +66,7 @@ exports[`Page Process > should match i18n site page maps 1`] = `
6566
"title": "Remote",
6667
"type": "page",
6768
},
69+
"test": "Test",
6870
},
6971
"kind": "Meta",
7072
},
@@ -77,9 +79,11 @@ exports[`Page Process > should match i18n site page maps 1`] = `
7779
"theme": {
7880
"layout": "raw",
7981
},
82+
"title": "A Page",
8083
"type": "page",
8184
},
8285
"acknowledgement": "🧩 Acknowledgement",
86+
"changelog": "Changelog",
8387
"team": "👥 Team",
8488
},
8589
"kind": "Meta",
@@ -162,39 +166,45 @@ exports[`Page Process > should match i18n site page maps 1`] = `
162166
"type": "separator",
163167
},
164168
"404-500": "404/500 Custom Error Pages",
165-
"advanced": {},
166-
"arguments": {},
169+
"advanced": "Advanced",
170+
"arguments": "Arguments",
171+
"callout": "Callout",
167172
"change-log": {
168173
"theme": {
169174
"sidebar": false,
170175
},
176+
"title": "Change Log",
171177
},
178+
"code-block-without-language": "Code Block without Language",
172179
"conditional-fetching": "Conditional Data Fetching",
173180
"custom-header-ids": "Custom Header IDs",
174-
"data-fetching": {},
181+
"data-fetching": "Data Fetching",
175182
"error-handling": {
176183
"display": "hidden",
184+
"title": "Error Handling",
177185
},
178-
"getting-started": {},
186+
"getting-started": "Getting Started",
179187
"github_link": {
180188
"href": "https://github.com/shuding/nextra",
181189
"newWindow": true,
182190
"title": "GitHub 🐙 →",
183191
},
184-
"global-configuration": {},
185-
"middleware": {},
186-
"mutation": {},
187-
"options": {},
188-
"pagination": {},
189-
"prefetching": {},
192+
"global-configuration": "Global Configuration",
193+
"middleware": "Middleware",
194+
"mutation": "Mutation",
195+
"options": "Options",
196+
"pagination": "Pagination",
197+
"prefetching": "Prefetching",
190198
"raw-layout": {
191199
"theme": {
192200
"layout": "raw",
193201
},
202+
"title": "Raw Layout",
194203
},
195204
"revalidation": "Auto Revalidation",
196-
"suspense": {},
205+
"suspense": "Suspense",
197206
"typescript": "TypeScript :)",
207+
"understanding": "Understanding",
198208
"with-nextjs": "Next.js SSG and SSR",
199209
"wrap-toc-items": "Wrap Table of Content Items",
200210
},
@@ -213,8 +223,16 @@ exports[`Page Process > should match i18n site page maps 1`] = `
213223
"title": "Do Not Use",
214224
"type": "separator",
215225
},
226+
"cache": "Cache",
227+
"code-highlighting": "Code Highlighting",
228+
"dynamic-markdown-import": "Dynamic Markdown Import",
216229
"file-name.with.DOTS": "Filenames with Dots",
230+
"images": "Images",
231+
"markdown-import": "Markdown Import",
217232
"more": "More: A Super Super Super Super Long Directory",
233+
"performance": "Performance",
234+
"react-native": "React Native",
235+
"scrollbar-x": "Scrollbar X",
218236
},
219237
"kind": "Meta",
220238
},
@@ -272,11 +290,25 @@ exports[`Page Process > should match i18n site page maps 1`] = `
272290
"name": "two",
273291
"route": "/en/docs/advanced/more/tree/two",
274292
},
293+
{
294+
"data": {
295+
"one": "One",
296+
"three": "Three",
297+
"two": "Two",
298+
},
299+
"kind": "Meta",
300+
},
275301
],
276302
"kind": "Folder",
277303
"name": "tree",
278304
"route": "/en/docs/advanced/more/tree",
279305
},
306+
{
307+
"data": {
308+
"loooooooooooooooooooong-title": "Loooooooooooooooooooong Title",
309+
},
310+
"kind": "Meta",
311+
},
280312
],
281313
"kind": "Folder",
282314
"name": "more",
@@ -428,13 +460,14 @@ exports[`Page Process > should match i18n site page maps 1`] = `
428460
"data": {
429461
"auth": "Authentication",
430462
"basic": "Basic Usage",
431-
"error-handling": {},
463+
"error-handling": "Error Handling",
432464
"full": {
433465
"theme": {
434466
"layout": "raw",
435467
},
468+
"title": "Full",
436469
},
437-
"infinite-loading": {},
470+
"infinite-loading": "Infinite Loading",
438471
"ssr": "Next.js SSR",
439472
},
440473
"kind": "Meta",
@@ -591,6 +624,7 @@ exports[`Page Process > should match i18n site page maps 1`] = `
591624
"revalidation": "Revalidación Automática",
592625
"suspense": "Suspense",
593626
"typescript": "Typescript",
627+
"understanding": "Understanding",
594628
"with-nextjs": "Next.js SSG and SSR",
595629
"wrap-toc-items": "Ajustar elementos de la tabla de contenido",
596630
},
@@ -851,6 +885,7 @@ exports[`Page Process > should match i18n site page maps 1`] = `
851885
"revalidation": "Авто-ревалидация",
852886
"suspense": "Задержка (Suspense)",
853887
"typescript": "Typescript",
888+
"understanding": "Understanding",
854889
"with-nextjs": "Next.js SSG и SSR",
855890
"wrap-toc-items": "Обернуть таблицу содержимого элементов",
856891
},

‎packages/nextra/__test__/fixture/page-maps/pageMap.ts

+60-866
Large diffs are not rendered by default.

‎packages/nextra/__test__/normalize-page.spec.ts

-19
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,34 @@
11
import { normalizePages } from '../src/normalize-pages'
22
import { cnPageMap, usPageMap } from './fixture/page-maps/pageMap'
33

4-
const defaultLocale = 'en-US'
5-
64
describe('normalize-page', () => {
75
it('zh-CN home', () => {
8-
const locale = 'zh-CN'
96
const result = normalizePages({
107
list: cnPageMap,
11-
locale,
12-
defaultLocale,
138
route: '/'
149
})
1510
expect(result).toMatchSnapshot()
1611
})
1712

1813
it('zh-CN getting-started', () => {
19-
const locale = 'zh-CN'
2014
const result = normalizePages({
2115
list: cnPageMap,
22-
locale,
23-
defaultLocale,
2416
route: '/docs/getting-started'
2517
})
2618
expect(result).toMatchSnapshot()
2719
})
2820

2921
it('en-US home', () => {
30-
const locale = 'en-US'
3122
const result = normalizePages({
3223
list: usPageMap,
33-
locale,
34-
defaultLocale,
3524
route: '/'
3625
})
3726
expect(result).toMatchSnapshot()
3827
})
3928

4029
it('en-US getting-started', () => {
41-
const locale = 'en-US'
4230
const result = normalizePages({
4331
list: usPageMap,
44-
locale,
45-
defaultLocale,
4632
route: '/docs/getting-started'
4733
})
4834
expect(result).toMatchSnapshot()
@@ -72,8 +58,6 @@ describe('normalize-page', () => {
7258
}
7359
}
7460
],
75-
locale: 'en-US',
76-
defaultLocale: 'en-US',
7761
route: '/500ddd'
7862
})
7963
expect(result).toMatchSnapshot()
@@ -103,8 +87,6 @@ describe('normalize-page', () => {
10387
}
10488
}
10589
],
106-
locale: 'en-US',
107-
defaultLocale: 'en-US',
10890
route: '/500'
10991
})
11092
expect(result).toMatchSnapshot()
@@ -159,7 +141,6 @@ describe('normalize-page', () => {
159141
route: '/showcase'
160142
}
161143
],
162-
locale: 'en-US',
163144
route: '/docs'
164145
})
165146
expect(result.topLevelNavbarItems).toMatchInlineSnapshot(`

‎packages/nextra/__test__/page-map.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ describe('Page Process', () => {
1818
expect(items).toEqual([])
1919
})
2020

21-
it('should add `_meta.json` file if it missing', async () => {
21+
it("should add `_meta.json` file if it's missing", async () => {
2222
const { items } = await collectFiles({
2323
dir: path.join(
2424
CWD,

‎packages/nextra/src/normalize-pages.ts

+3-18
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,12 @@ function findFirstRoute(items: DocsItem[]): string | undefined {
8282

8383
export function normalizePages({
8484
list,
85-
locale,
86-
defaultLocale,
8785
route,
8886
docsRoot = '',
8987
underCurrentDocsRoot = false,
9088
pageThemeContext = DEFAULT_PAGE_THEME
9189
}: {
9290
list: PageMapItem[]
93-
locale: string
94-
defaultLocale?: string
9591
route: string
9692
docsRoot?: string
9793
underCurrentDocsRoot?: boolean
@@ -100,12 +96,8 @@ export function normalizePages({
10096
let _meta: Record<string, any> | undefined
10197
for (const item of list) {
10298
if (item.kind === 'Meta') {
103-
if (item.locale === locale) {
104-
_meta = item.data
105-
break
106-
}
107-
// fallback
108-
_meta ||= item.data
99+
_meta = item.data
100+
break
109101
}
110102
}
111103
const meta = _meta || {}
@@ -147,14 +139,9 @@ export function normalizePages({
147139
const items = list
148140
.filter(
149141
(a): a is MdxFile | Folder =>
150-
// not meta
151142
a.kind !== 'Meta' &&
152143
// not hidden routes
153-
!a.name.startsWith('_') &&
154-
// locale matches, or fallback to default locale
155-
(!('locale' in a) ||
156-
!a.locale ||
157-
[locale, defaultLocale].includes(a.locale))
144+
!a.name.startsWith('_')
158145
)
159146
.sort((a, b) => {
160147
const indexA = metaKeys.indexOf(a.name)
@@ -227,8 +214,6 @@ export function normalizePages({
227214
a.children &&
228215
normalizePages({
229216
list: a.children,
230-
locale,
231-
defaultLocale,
232217
route,
233218
docsRoot: type === 'page' || type === 'menu' ? a.route : docsRoot,
234219
underCurrentDocsRoot: underCurrentDocsRoot || isCurrentDocsTree,

‎packages/nextra/src/plugin.ts

+19-44
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import grayMatter from 'gray-matter'
55
import pLimit from 'p-limit'
66
import {
77
CWD,
8-
DEFAULT_LOCALES,
98
DYNAMIC_META_FILENAME,
109
MARKDOWN_EXTENSION_REGEX,
1110
META_FILENAME
@@ -32,10 +31,10 @@ const readFile = promisify(fs.readFile)
3231
const realpath = promisify(fs.realpath)
3332
const stat = promisify(fs.stat)
3433

35-
export const collectMdx = async (
34+
export async function collectMdx(
3635
filePath: string,
3736
route = ''
38-
): Promise<MdxFile> => {
37+
): Promise<MdxFile> {
3938
const { name } = path.parse(filePath)
4039

4140
const content = await readFile(filePath, 'utf8')
@@ -52,13 +51,11 @@ const limit = pLimit(20)
5251

5352
export async function collectFiles({
5453
dir,
55-
locales = DEFAULT_LOCALES,
5654
route = '/',
5755
fileMap = Object.create(null),
5856
isFollowingSymlink = false
5957
}: {
6058
dir: string
61-
locales?: string[]
6259
route?: string
6360
fileMap?: FileMap
6461
isFollowingSymlink?: boolean
@@ -90,7 +87,6 @@ export async function collectFiles({
9087
if (fileRoute === '/api') return
9188
const { items } = await collectFiles({
9289
dir: filePath,
93-
locales,
9490
route: fileRoute,
9591
fileMap,
9692
isFollowingSymlink: isSymlinked
@@ -188,54 +184,33 @@ ${(error as Error).name}: ${(error as Error).message}`
188184
const items = (await Promise.all(promises)).filter(truthy)
189185

190186
const mdxPages: MdxFile[] = []
191-
const metaLocaleIndexes = new Map<string, number>()
187+
let metaFile: MetaJsonFile | undefined
192188

193-
for (let i = 0; i < items.length; i++) {
194-
const item = items[i]
189+
for (const item of items) {
195190
if (item.kind === 'MdxPage') {
196191
mdxPages.push(item)
197192
} else if (item.kind === 'Meta') {
198-
// It is possible that it doesn't have a locale suffixed, we use '' here.
199-
metaLocaleIndexes.set(item.locale || '', i)
193+
metaFile = item
200194
}
201195
}
196+
const defaultMeta = sortPages(mdxPages)
202197

203-
// In the current level, find the corresponding meta file for each locale and
204-
// extend the fallback meta data we get from the file system.
205-
for (const locale of locales) {
206-
let metaIndex = metaLocaleIndexes.get(locale)
207-
208-
const defaultMeta = sortPages(mdxPages, locale)
209-
210-
const metaFilename = locale
211-
? META_FILENAME.replace('.', `.${locale}.`)
212-
: META_FILENAME
213-
214-
const metaPath = path.join(dir, metaFilename) as MetaJsonPath
215-
216-
if (metaIndex === undefined && defaultMeta.length > 0) {
217-
// Create a new meta file if it doesn't exist.
218-
const meta: MetaJsonFile = {
219-
kind: 'Meta',
220-
data: Object.fromEntries(defaultMeta)
221-
}
222-
fileMap[metaPath] = meta
223-
items.push(meta)
224-
metaIndex = items.length - 1
198+
// add `_meta.json` file if it's missing
199+
if (!metaFile && mdxPages.length > 0) {
200+
metaFile = {
201+
kind: 'Meta',
202+
data: Object.fromEntries(defaultMeta)
225203
}
204+
items.push(metaFile)
205+
}
226206

227-
if (metaIndex !== undefined) {
228-
// Fill with the fallback. Note that we need to keep the original order.
229-
const meta = { ...items[metaIndex] } as MetaJsonFile
230-
for (const [key, capitalizedTitle] of defaultMeta) {
231-
meta.data[key] ||= capitalizedTitle
232-
const metaItem = meta.data[key]
233-
if (typeof metaItem === 'object') {
234-
metaItem.title ||= capitalizedTitle
235-
}
207+
if (metaFile) {
208+
for (const [key, capitalizedTitle] of defaultMeta) {
209+
metaFile.data[key] ||= capitalizedTitle
210+
const metaItem = metaFile.data[key]
211+
if (typeof metaItem === 'object') {
212+
metaItem.title ||= capitalizedTitle
236213
}
237-
fileMap[metaPath] = meta
238-
items[metaIndex] = meta
239214
}
240215
}
241216

‎packages/nextra/src/schemas.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { ProcessorOptions } from '@mdx-js/mdx'
22
import type { Options as RehypePrettyCodeOptions } from 'rehype-pretty-code'
3-
import * as z from 'zod' // use `import * as z` for better tree-shaking https://github.com/colinhacks/zod/issues/2596#issuecomment-1643053289
3+
import { z } from 'zod'
44
import type { PageOpts } from './types'
55

66
export const searchSchema = z.boolean().or(

‎packages/nextra/src/types.ts

+1-5
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export interface Folder<FileType = PageMapItem> {
2727

2828
export type MetaJsonFile = {
2929
kind: 'Meta'
30-
locale?: string
3130
data: {
3231
[fileName: string]: Meta
3332
}
@@ -47,7 +46,6 @@ export type DynamicMeta = Record<string, DynamicMetaItem>
4746

4847
export type DynamicMetaJsonFile = {
4948
kind: 'Meta'
50-
locale?: string
5149
data: DynamicMeta
5250
}
5351

@@ -58,7 +56,6 @@ export type MdxFile<FrontMatterType = FrontMatter> = {
5856
kind: 'MdxPage'
5957
name: string
6058
route: string
61-
locale?: string
6259
frontMatter?: FrontMatterType
6360
}
6461

@@ -113,7 +110,6 @@ export type ThemeConfig = any | null
113110

114111
export type NextraThemeLayoutProps = {
115112
pageOpts: PageOpts
116-
pageProps: any
117113
themeConfig: ThemeConfig
118114
children: ReactNode
119115
}
@@ -124,7 +120,7 @@ export type NextraInternalGlobal = typeof globalThis & {
124120
route: string
125121
context: Record<string, { Content: FC; pageOpts: PageOpts }>
126122
refreshListeners: Record<string, (() => void)[]>
127-
Layout: FC<any>
123+
Layout: FC<NextraThemeLayoutProps>
128124
themeConfig?: ThemeConfig
129125
}
130126
}

‎packages/nextra/src/utils.ts

+4-9
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,10 @@ export function pageTitleFromFilename(fileName: string) {
2222
}
2323

2424
export function sortPages(
25-
pages: (
26-
| Pick<MdxFile, 'kind' | 'name' | 'frontMatter' | 'locale'>
27-
| Pick<Folder, 'kind' | 'name'>
28-
)[],
25+
pages: (Omit<MdxFile, 'route'> | Omit<Folder, 'route' | 'children'>)[],
2926
locale?: string
3027
): [string, string][] {
31-
if (locale === '') {
32-
locale = undefined
33-
}
3428
return pages
35-
.filter(item => item.kind === 'Folder' || item.locale === locale)
3629
.map(item => ({
3730
name: item.name,
3831
date: 'frontMatter' in item && item.frontMatter?.date,
@@ -50,7 +43,9 @@ export function sortPages(
5043
if (b.date) {
5144
return 1 // sort a after b
5245
}
53-
return a.title.localeCompare(b.title, locale, { numeric: true })
46+
return a.title.localeCompare(b.title, locale || undefined, {
47+
numeric: true
48+
})
5449
})
5550
.map(item => [item.name, item.title])
5651
}

‎packages/nextra/src/webpack-plugins/nextra.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class NextraPlugin {
1818

1919
compiler.hooks.beforeCompile.tapAsync(pluginName, async (_, callback) => {
2020
try {
21-
const result = await collectFiles({ dir: PAGES_DIR, locales })
21+
const result = await collectFiles({ dir: PAGES_DIR })
2222

2323
for (const locale of locales) {
2424
const folderItem =

0 commit comments

Comments
 (0)
Please sign in to comment.