Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v4] sync with nextra 3.2.4 / capitalize folders in sidebar even without index pages #3722

Merged
merged 16 commits into from
Nov 20, 2024
9 changes: 9 additions & 0 deletions .changeset/slimy-ghosts-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'nextra-theme-blog': patch
'nextra-theme-docs': patch
'nextra': patch
---

- fix missing tailwind class for `json` icon in code blocks
- capitalize folders in sidebar even without index pages
- sync with nextra 3.2.4
9 changes: 8 additions & 1 deletion docs/mdx-components.ts
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
export { useMDXComponents } from 'nextra-theme-docs'
import { useMDXComponents as getDocsMDXComponents } from 'nextra-theme-docs'

const docsComponents = getDocsMDXComponents()

export const useMDXComponents: typeof getDocsMDXComponents = components => ({
...docsComponents,
...components
})
9 changes: 8 additions & 1 deletion examples/docs/mdx-components.js
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
export { useMDXComponents } from 'nextra-theme-docs'
import { useMDXComponents as getDocsMDXComponents } from 'nextra-theme-docs'

const docsComponents = getDocsMDXComponents()

export const useMDXComponents = components => ({
...docsComponents,
...components
})
2 changes: 1 addition & 1 deletion examples/docs/src/app/docs/[[...mdxPath]]/page.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable react-hooks/rules-of-hooks -- false positive, useMDXComponents are not react hooks */

import { useMDXComponents } from 'nextra-theme-docs'
import { generateStaticParamsFor, importPage } from 'nextra/pages'
import { useMDXComponents } from '../../../../mdx-components'

export const generateStaticParams = generateStaticParamsFor('mdxPath')

Expand Down
6 changes: 3 additions & 3 deletions examples/docs/src/content/_meta.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export default {
index: '',
'get-started': '',
features: 'Features',
themes: 'Themes',
advanced: 'Advanced'
features: '',
themes: '',
advanced: ''
}
5 changes: 1 addition & 4 deletions packages/nextra-theme-docs/src/components/sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,7 @@ const Menu = forwardRef<HTMLUListElement, MenuProps>(
<ul className={cn(classes.list, className)} ref={forwardedRef}>
{directories.map(item => {
const ComponentToUse =
item.type === 'menu' ||
(item.children && (item.children.length || !('frontMatter' in item)))
? Folder
: File
item.type === 'menu' || item.children?.length ? Folder : File

return (
<ComponentToUse
Expand Down
2 changes: 1 addition & 1 deletion packages/nextra-theme-docs/tailwind.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default {
content: [
'./src/**/*.{ts,tsx}',
'../nextra/src/client/icons/*.svg',
'../nextra/src/client/{components,mdx-components}/**/*.tsx'
'../nextra/src/client/{components,hocs,mdx-components}/**/*.tsx'
],
theme: {
screens: {
Expand Down
2 changes: 1 addition & 1 deletion packages/nextra/src/client/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ export { Playground } from './playground.js'
export { Search } from './search.js'
export { Select } from './select.js'
export { Steps } from './steps.js'
export * from '../with/index.js'
export * from '../hocs/index.js'
export { Mermaid } from '@theguild/remark-mermaid/mermaid'
export * from '../mdx-components/index.js'
4 changes: 2 additions & 2 deletions packages/nextra/src/client/components/playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ export const Playground: FC<
if (compiledSource) {
// `<MDXRemote>` cannot be used here because `useMDXComponents` may include components that
// are only available on the server.
const MDXContent = evaluate(compiledSource, scope).default
return <MDXContent components={components} />
const MDXContent = evaluate(compiledSource, scope, components).default
return <MDXContent />
}

return fallback
Expand Down
17 changes: 14 additions & 3 deletions packages/nextra/src/client/evaluate.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { useMDXComponents } from 'next-mdx-import-source-file'
import jsxDevRuntime from 'react/jsx-dev-runtime'
import jsxRuntime from 'react/jsx-runtime'
import type { MDXComponents } from './mdx-components.js'

const runtime =
process.env.NODE_ENV === 'production' ? jsxRuntime : jsxDevRuntime

export type Scope = Record<string, unknown>

export function evaluate(rawJs: string, scope: Scope = {}) {
export function evaluate(
rawJs: string,
scope: Scope = {},
components?: MDXComponents
) {
// if we're ready to render, we can assemble the component tree and let React do its thing
// first we set up the scope which has to include the mdx custom
// create element function as well as any components we're using
Expand All @@ -20,5 +24,12 @@ export function evaluate(rawJs: string, scope: Scope = {}) {
// function with the actual values.
const hydrateFn = Reflect.construct(Function, ['$', ...keys, rawJs])

return hydrateFn({ ...runtime, useMDXComponents }, ...values)
return hydrateFn(
{
...runtime,
// Inject components in `<MDXContent>` and TOC
useMDXComponents: () => components
},
...values
)
}
2 changes: 1 addition & 1 deletion packages/nextra/src/client/mdx-components/anchor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ export const Anchor: FC<ComponentPropsWithoutRef<'a'>> = ({
href = '',
...props
}) => {
props.className = cn('focus-visible:nextra-focus', props.className)
if (EXTERNAL_URL_RE.test(href)) {
const { children } = props
props.className = cn('focus-visible:nextra-focus', props.className)
return (
<a href={href} target="_blank" rel="noreferrer" {...props}>
{children}
Expand Down
3 changes: 2 additions & 1 deletion packages/nextra/src/client/normalize-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ export function normalizePages({
extendedMeta.title ||
currentItem.frontMatter?.sidebarTitle ||
currentItem.frontMatter?.title ||
(type === 'separator' ? '' : currentItem.name)
// @ts-expect-error -- we use title for capitalize folders without index page
(type === 'separator' ? '' : currentItem.title || currentItem.name)

const getItem = (): Item => ({
...currentItem,
Expand Down
11 changes: 7 additions & 4 deletions packages/nextra/src/server/__tests__/normalize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ describe('normalize-page', () => {
{
name: '1-level',
route: '/1-level',
title: '1 Level',
children: [
{
name: '2-level',
route: '/1-level/2-level',
title: '2 Level',
children: [
{
data: {
Expand Down Expand Up @@ -102,6 +104,7 @@ describe('normalize-page', () => {
{
name: '1-level',
route: '/1-level',
title: '1 Level',
children: [
{
data: {
Expand Down Expand Up @@ -217,14 +220,14 @@ describe('normalize-page', () => {
"isUnderCurrentDocsTree": true,
"name": "two",
"route": "/one/two",
"title": "two",
"title": "Two",
"type": "doc",
},
],
"isUnderCurrentDocsTree": true,
"name": "one",
"route": "/one",
"title": "one",
"title": "One",
"type": "doc",
},
]
Expand Down Expand Up @@ -364,7 +367,7 @@ describe('normalize-page', () => {
asIndexPage: true
},
type: 'doc',
title: 'themes',
title: 'Themes',
isUnderCurrentDocsTree: true
},
{
Expand All @@ -384,7 +387,7 @@ describe('normalize-page', () => {
asIndexPage: true
},
type: 'doc',
title: 'themes-test',
title: 'Themes Test',
isUnderCurrentDocsTree: true
}
])
Expand Down
1 change: 1 addition & 0 deletions packages/nextra/src/server/__tests__/page-map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe('Page Process', () => {
],
"name": "docs",
"route": "/docs",
"title": "Docs",
},
{
"frontMatter": undefined,
Expand Down
Loading