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

fix: normalize path with nextjs normalizeAppPath utility function #4149

Merged
merged 4 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/friendly-windows-count.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"nextra": patch
---

fix route group within `app/` dir crash the `convertToPageMap`
1 change: 0 additions & 1 deletion docs/app/_components/i18n-demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export const I18n: FC = () => {
<div
key={lang}
onPointerOver={() => setActive(lang)}
// eslint-disable-next-line tailwindcss/no-custom-classname -- TODO: configure eslint-plugin-tailwindcss to import nextra-theme-docs styles so below classes could be removed
className={cn(
'relative cursor-default px-4 py-1.5 whitespace-nowrap select-none',
active === lang
Expand Down
20 changes: 20 additions & 0 deletions examples/docs/src/app/showcase/(overview)/page.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @see https://github.com/shuding/nextra/issues/4148
*/

export const metadata = {}

export default function Page() {
return (
<h1
style={{
textAlign: 'center',
fontSize: 64,
margin: '25vh 0',
fontWeight: 'bold'
}}
>
Showcase
</h1>
)
}
42 changes: 21 additions & 21 deletions packages/eslint-config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { includeIgnoreFile } from '@eslint/compat'
import js from '@eslint/js'
// @ts-expect-error -- no types
import eslintPluginNext from '@next/eslint-plugin-next'
import type { Linter } from 'eslint'
// import type { Linter } from 'eslint'
// @ts-expect-error -- no types
import eslintConfigPrettier from 'eslint-config-prettier'
import eslintPluginImport from 'eslint-plugin-import-x'
Expand All @@ -13,24 +13,24 @@ import * as eslintPluginReactCompiler from 'eslint-plugin-react-compiler'
// @ts-expect-error -- no types
import eslintPluginReactHooks from 'eslint-plugin-react-hooks'
import eslintPluginSonarJs from 'eslint-plugin-sonarjs'
// @ts-expect-error -- no types
import eslintPluginTailwindCss from 'eslint-plugin-tailwindcss'
// import eslintPluginTailwindCss from 'eslint-plugin-tailwindcss'
// @ts-expect-error -- no types
import eslintPluginTsSortKeys from 'eslint-plugin-typescript-sort-keys'
import eslintPluginUnicorn from 'eslint-plugin-unicorn'
import tseslint from 'typescript-eslint'
import type { Config } from 'typescript-eslint'

const TAILWIND_CONFIG = {
extends: [eslintPluginTailwindCss.configs['flat/recommended']],
rules: {
'tailwindcss/classnames-order': 'off', // conflicts with prettier-plugin-tailwindcss
'tailwindcss/enforces-negative-arbitrary-values': 'error',
'tailwindcss/enforces-shorthand': 'error',
'tailwindcss/migration-from-tailwind-2': 'error',
'tailwindcss/no-custom-classname': 'error'
} satisfies Linter.RulesRecord
}
// TODO: Enable once `eslint-plugin-tailwindcss` will support Tailwind CSS v4
// const TAILWIND_CONFIG = {
// extends: [eslintPluginTailwindCss.configs['flat/recommended']],
// rules: {
// 'tailwindcss/classnames-order': 'off', // conflicts with prettier-plugin-tailwindcss
// 'tailwindcss/enforces-negative-arbitrary-values': 'error',
// 'tailwindcss/enforces-shorthand': 'error',
// 'tailwindcss/migration-from-tailwind-2': 'error',
// 'tailwindcss/no-custom-classname': 'error'
// } satisfies Linter.RulesRecord
// }

const REACT_COMPILER_RESTRICT = {
name: 'react',
Expand Down Expand Up @@ -201,7 +201,7 @@ const config: Config = tseslint.config(
},
// ⚙️ nextra-theme-docs
{
...TAILWIND_CONFIG,
// ...TAILWIND_CONFIG,
files: ['packages/nextra-theme-docs/**'],
settings: {
tailwindcss: {
Expand All @@ -217,7 +217,7 @@ const config: Config = tseslint.config(
}
},
rules: {
...TAILWIND_CONFIG.rules,
// ...TAILWIND_CONFIG.rules,
'no-restricted-imports': [
'error',
{ name: 'next/link', message: 'Use `<Anchor>` instead' },
Expand All @@ -229,10 +229,10 @@ const config: Config = tseslint.config(
},
// ⚙️ nextra-theme-blog
{
...TAILWIND_CONFIG,
// ...TAILWIND_CONFIG,
files: ['packages/nextra-theme-blog/**'],
rules: {
...TAILWIND_CONFIG.rules,
// ...TAILWIND_CONFIG.rules,
'no-restricted-imports': [
'error',
{
Expand All @@ -252,7 +252,7 @@ const config: Config = tseslint.config(
},
// ⚙️ nextra
{
...TAILWIND_CONFIG,
// ...TAILWIND_CONFIG,
files: ['packages/nextra/**'],
settings: {
tailwindcss: {
Expand All @@ -267,15 +267,15 @@ const config: Config = tseslint.config(
}
},
rules: {
...TAILWIND_CONFIG.rules,
// ...TAILWIND_CONFIG.rules,
'import/extensions': ['error', 'ignorePackages'],
// False positive due Tailwind CSS v4
'tailwindcss/no-custom-classname': 'off'
}
},
// ⚙️ Docs
{
...TAILWIND_CONFIG,
// ...TAILWIND_CONFIG,
files: ['docs/**'],
settings: {
tailwindcss: {
Expand Down Expand Up @@ -307,7 +307,7 @@ const config: Config = tseslint.config(
},
// ⚙️ SWR-site example
{
...TAILWIND_CONFIG,
// ...TAILWIND_CONFIG,
files: ['examples/swr-site/**'],
settings: {
tailwindcss: {
Expand Down
11 changes: 11 additions & 0 deletions packages/nextra/src/server/__tests__/to-page-map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,7 @@ describe('generatePageMap()', () => {
"src/app/_meta.js",
"src/app/blog/page.jsx",
"src/app/page.jsx",
"src/app/showcase/(overview)/page.jsx",
"src/content/_meta.js",
"src/content/advanced/code-highlighting.mdx",
"src/content/features/_meta.js",
Expand Down Expand Up @@ -1011,6 +1012,11 @@ describe('generatePageMap()', () => {
"name": "index",
"route": "/",
},
{
"__pagePath": "src/app/showcase/(overview)/page.jsx",
"name": "showcase",
"route": "/showcase",
},
{
"children": [
{
Expand Down Expand Up @@ -1220,6 +1226,11 @@ describe('generatePageMap()', () => {
"name": "index",
"route": "/",
},
{
"__pagePath": "src/app/showcase/(overview)/page.jsx",
"name": "showcase",
"route": "/showcase",
},
]
`)
})
Expand Down
5 changes: 4 additions & 1 deletion packages/nextra/src/server/page-map/to-page-map.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import path from 'node:path'
import { normalizeAppPath } from 'next/dist/shared/lib/router/utils/app-paths.js'
import type { TItem } from '../../types.js'

interface NestedMap {
Expand Down Expand Up @@ -54,7 +55,9 @@ export function convertToPageMap({
//
// will be normalized to:
// app/posts/aaron-swartz-a-programmable-web/page.mdx
dir.replaceAll(/\(.*?\)(\/|$)/g, '')
//
// The `normalizeAppPath` function ensures a leading slash is present, so we slice it off.
normalizeAppPath(dir).slice(1)
: [dir, name !== 'index' && name].filter(Boolean).join('/')
pages[key] = filePath
}
Expand Down