Skip to content

Commit

Permalink
fix when sidebar show non-md folders (#1331)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaMachina committed Jan 21, 2023
1 parent 939ac61 commit 624d6b4
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 68 deletions.
6 changes: 6 additions & 0 deletions .changeset/rare-penguins-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'nextra': patch
'nextra-theme-docs': patch
---

fix when sidebar show non-md folders
Empty file.
61 changes: 13 additions & 48 deletions packages/nextra/__test__/__snapshots__/page-map.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,6 @@ exports[`Page Process > pageMap en-US 1`] = `
"name": "team",
"route": "/about/team",
},
{
"data": {},
"kind": "Meta",
},
],
"kind": "Folder",
"name": "about",
Expand Down Expand Up @@ -163,10 +159,6 @@ exports[`Page Process > pageMap en-US 1`] = `
"name": "swr-v1",
"route": "/blog/swr-v1",
},
{
"data": {},
"kind": "Meta",
},
],
"kind": "Folder",
"name": "blog",
Expand Down Expand Up @@ -322,19 +314,11 @@ exports[`Page Process > pageMap en-US 1`] = `
"name": "two",
"route": "/docs/advanced/more/tree/two",
},
{
"data": {},
"kind": "Meta",
},
],
"kind": "Folder",
"name": "tree",
"route": "/docs/advanced/more/tree",
},
{
"data": {},
"kind": "Meta",
},
],
"kind": "Folder",
"name": "more",
Expand All @@ -352,10 +336,6 @@ exports[`Page Process > pageMap en-US 1`] = `
"name": "react-native",
"route": "/docs/advanced/react-native",
},
{
"data": {},
"kind": "Meta",
},
],
"kind": "Folder",
"name": "advanced",
Expand Down Expand Up @@ -493,10 +473,6 @@ exports[`Page Process > pageMap en-US 1`] = `
"name": "wrap-toc-items",
"route": "/docs/wrap-toc-items",
},
{
"data": {},
"kind": "Meta",
},
],
"kind": "Folder",
"name": "docs",
Expand Down Expand Up @@ -576,10 +552,6 @@ exports[`Page Process > pageMap en-US 1`] = `
"name": "ssr",
"route": "/examples/ssr",
},
{
"data": {},
"kind": "Meta",
},
],
"kind": "Folder",
"name": "examples",
Expand All @@ -602,10 +574,6 @@ exports[`Page Process > pageMap en-US 1`] = `
"kind": "Meta",
"locale": "en-US",
},
{
"data": {},
"kind": "Meta",
},
],
"kind": "Folder",
"name": "remote",
Expand All @@ -622,13 +590,6 @@ exports[`Page Process > pageMap en-US 1`] = `
"name": "test",
"route": "/test",
},
{
"data": {
"404": "404",
"500": "500",
},
"kind": "Meta",
},
],
"/docs/data-fetching",
]
Expand Down Expand Up @@ -663,8 +624,19 @@ exports[`Page Process > pageMap ru 1`] = `
{
"children": [
{
"data": {},
"data": {
"a-page": {
"display": "hidden",
"theme": {
"layout": "raw",
},
"type": "page",
},
"acknowledgement": "🧩 Acknowledgement",
"team": "👥 Team",
},
"kind": "Meta",
"locale": "en-US",
},
{
"kind": "MdxPage",
Expand Down Expand Up @@ -785,10 +757,6 @@ exports[`Page Process > pageMap ru 1`] = `
"name": "two",
"route": "/docs/advanced/more/tree/two",
},
{
"data": {},
"kind": "Meta",
},
],
"kind": "Folder",
"name": "tree",
Expand All @@ -800,10 +768,6 @@ exports[`Page Process > pageMap ru 1`] = `
"name": "loooooooooooooooooooong-title",
"route": "/docs/advanced/more/loooooooooooooooooooong-title",
},
{
"data": {},
"kind": "Meta",
},
],
"kind": "Folder",
"name": "more",
Expand Down Expand Up @@ -1081,6 +1045,7 @@ exports[`Page Process > pageMap ru 1`] = `
{
"data": {},
"kind": "Meta",
"locale": "en-US",
},
],
"kind": "Folder",
Expand Down
8 changes: 8 additions & 0 deletions packages/nextra/__test__/page-map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,12 @@ describe('Page Process', () => {
})
expect(gettingStartData.pageMap).toEqual(indexData.pageMap)
})

it("should not add to pageMap folders that doesn't contain markdown files", async () => {
const { items } = await collectFiles(
path.join(PAGES_DIR, 'docs/test-non-md-folders'),
['']
)
expect(items.length).toBe(0)
})
})
2 changes: 1 addition & 1 deletion packages/nextra/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const DEFAULT_CONFIG: Omit<NextraConfig, 'theme'> = {
export const OFFICIAL_THEMES = [
'nextra-theme-docs',
'nextra-theme-blog'
] as const
]

export const META_FILENAME = '_meta.json'
export const DYNAMIC_META_FILENAME = '_meta.js'
Expand Down
7 changes: 4 additions & 3 deletions packages/nextra/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ function filter(
const items: Page[] = []
const meta = pageMap.find(
(item): item is MetaJsonFile => item.kind === 'Meta'
)!.data
)
const metaData = meta?.data || {}

for (const item of pageMap) {
if (item.kind === 'Meta') continue
const page = {
...item,
meta: normalizeMeta(meta[item.name])
meta: normalizeMeta(metaData[item.name])
} as Page

if (page.kind === 'Folder') {
Expand All @@ -54,7 +55,7 @@ function filter(
items.push(page)
}

const metaKeys = Object.keys(meta)
const metaKeys = Object.keys(metaData)
items.sort((a, b) => {
const indexA = metaKeys.indexOf(a.name)
const indexB = metaKeys.indexOf(b.name)
Expand Down
4 changes: 1 addition & 3 deletions packages/nextra/src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,7 @@ async function loader(
)

const katexCssImport = latex ? "import 'katex/dist/katex.min.css'" : ''
const cssImport = OFFICIAL_THEMES.includes(
theme as (typeof OFFICIAL_THEMES)[number]
)
const cssImport = OFFICIAL_THEMES.includes(theme)
? `import '${theme}/style.css'`
: ''

Expand Down
14 changes: 1 addition & 13 deletions packages/nextra/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,11 @@ export async function collectFiles(

const items = (await Promise.all(promises)).filter(truthy)

const mdxPagesAndFolders: (MdxFile | Folder)[] = []
const mdxPages: MdxFile[] = []
const metaLocaleIndexes = new Map<string, number>()

for (let i = 0; i < items.length; i++) {
const item = items[i]
if (item.kind === 'MdxPage' || item.kind === 'Folder') {
mdxPagesAndFolders.push(item)
}
if (item.kind === 'MdxPage') {
mdxPages.push(item)
}
Expand All @@ -183,15 +179,7 @@ export async function collectFiles(

const metaPath = path.join(dir, metaFilename) as MetaJsonPath

if (metaIndex === undefined) {
// Create a new meta file if it doesn't exist.
fileMap[metaPath] = {
kind: 'Meta',
...(locale && { locale }),
data: Object.fromEntries(defaultMeta)
}
items.push(fileMap[metaPath])
} else {
if (metaIndex !== undefined) {
// Fill with the fallback. Note that we need to keep the original order.
const meta = { ...(items[metaIndex] as MetaJsonFile) }
for (const [key, value] of defaultMeta) {
Expand Down

1 comment on commit 624d6b4

@vercel
Copy link

@vercel vercel bot commented on 624d6b4 Jan 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.