Skip to content

Commit

Permalink
add display: 'normal' | 'hidden' for _meta item type: 'menu' (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaMachina authored Feb 9, 2025
1 parent 9107bff commit e6c3050
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/rotten-candles-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"nextra": patch
---

add `display: 'normal' | 'hidden'` for `_meta` item `type: 'menu'`
48 changes: 39 additions & 9 deletions docs/app/docs/file-conventions/page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,49 @@ description:

import { Cards } from 'nextra/components'
import { FileIcon, FolderIcon, MdxIcon } from 'nextra/icons'
import { MDXRemote } from 'nextra/mdx-remote'
import { createIndexPage, getPageMap } from 'nextra/page-map'
import { getIndexPageMap, getPageMap } from 'nextra/page-map'
import { useMDXComponents } from '../../../mdx-components'

# File Conventions

<MDXRemote
compiledSource={await createIndexPage(
await getPageMap('/docs/file-conventions')
)}
components={{
export async function Page() {
const { h2: H2 } = useMDXComponents()
const currentRoute = metadata.filePath
.replace('app', '')
.replace('/page.mdx', '')
const pageMap = await getPageMap(currentRoute)
const icons = {
Cards,
FileIcon,
FolderIcon,
MdxIcon
}}
/>
}
return getIndexPageMap(pageMap).map(pageItem => {
if (!Array.isArray(pageItem)) {
return <H2>{pageItem.title}</H2>
}
return (
<Cards>
{pageItem.map(item => {
const icon = item.frontMatter?.icon
const Icon = icons[icon]
if (icon && !Icon) {
throw new Error(
"Icon is defined in front matter but isn't provided"
)
}
return (
<Cards.Card
key={item.name}
title={item.title}
href={item.route || item.href}
icon={Icon && <Icon />}
/>
)
})}
</Cards>
)
})
}

<Page />
24 changes: 23 additions & 1 deletion packages/nextra/src/server/page-map/index-page.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { MdxFile, PageMapItem } from '../../types.js'
import type { MdxFile, PageMapItem, SeparatorItem } from '../../types.js'
import { compileMdx } from '../compile.js'

function renderCard(item: MdxFile): string {
Expand Down Expand Up @@ -42,3 +42,25 @@ export async function createIndexPage(pageMap: PageMapItem[]): Promise<string> {

return rawJs
}

export function getIndexPageMap(pageMap: PageMapItem[]) {
const result: (SeparatorItem | MdxFile[])[] = []
for (const item of pageMap) {
if ('data' in item) {
continue
}
// @ts-expect-error fixme
if (item.type === 'separator') {
// @ts-expect-error fixme
result.push(item)
} else {
const lastResult = result.at(-1)
if (Array.isArray(lastResult)) {
lastResult.push(item)
} else {
result.push([item])
}
}
}
return result
}
2 changes: 1 addition & 1 deletion packages/nextra/src/server/page-map/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export { normalizePageMap } from './normalize.js'
export { convertToPageMap } from './to-page-map.js'
export { mergeMetaWithPageMap } from './merge-meta-with-page-map.js'
export { getPageMap } from './get.js'
export { createIndexPage } from './index-page.js'
export { createIndexPage, getIndexPageMap } from './index-page.js'

export function getMetadata(
page:
Expand Down
3 changes: 2 additions & 1 deletion packages/nextra/src/server/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ export const menuSchema = z.strictObject({
obj[key].title ||= pageTitleFromFilename(key)
}
return obj
})
}),
display: z.enum(['normal', 'hidden']).optional()
})

export const itemSchema = z.strictObject({
Expand Down
4 changes: 2 additions & 2 deletions packages/nextra/styles/cards.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
@apply x:select-none;
}

svg {
& > span > svg {
@apply x:w-6 x:h-auto x:transition x:text-black/30 x:dark:text-white/40 x:shrink-0;
}

&:hover svg {
&:hover > span > svg {
@apply x:text-current!;
}

Expand Down

0 comments on commit e6c3050

Please sign in to comment.