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

fixes heading issues #2199

Merged
merged 11 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 8 additions & 0 deletions .changeset/modern-snakes-taste.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'nextra': minor
'nextra-theme-docs': minor
---

- show headings for partial md/mdx in toc

- hide headings in toc when parent `<Tab />` or `<Tabs.Tab />`
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ packages/nextra-theme-*/style.css
.idea/
.eslintcache
.env

tsup.config.bundled*
2 changes: 1 addition & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"@vercel/og": "^0.5.0",
"clsx": "^2.0.0",
"framer-motion": "^10.0.0",
"next": "^13.4.8",
"next": "^13.4.19",
"nextra": "workspace:*",
"nextra-theme-docs": "workspace:*",
"react": "^18.2.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/blog/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"start": "next start"
},
"dependencies": {
"next": "^13.4.8",
"next": "^13.4.19",
"nextra": "workspace:*",
"nextra-theme-blog": "workspace:*",
"react": "^18.2.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"start": "next start"
},
"dependencies": {
"next": "^13.4.8",
"next": "^13.4.19",
"nextra": "workspace:*",
"nextra-theme-docs": "workspace:*",
"react": "*",
Expand Down
2 changes: 1 addition & 1 deletion examples/swr-site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"intersection-observer": "^0.10.0",
"isomorphic-git": "1.21.0",
"markdown-to-jsx": "^6.11.4",
"next": "^13.4.8",
"next": "^13.4.19",
"nextra": "workspace:*",
"nextra-theme-docs": "workspace:*",
"react": "^18.2.0",
Expand Down
12 changes: 0 additions & 12 deletions examples/swr-site/pages/foo.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,6 @@

## after

<div onmouseover="alert('alpha')">
<a href="jAva script:alert('bravo')">delta</a>
<img src="x" onerror="alert('charlie')">
<iframe src="javascript:alert('delta')"></iframe>
<math>
<mi xlink:href="data:x,<script>alert('echo')</script>"></mi>
</math>
</div>
<script>
require('child_process').spawn('echo', ['hack!']);
</script>

```sh npm2yarn
npm i @graphql-eslint/eslint-plugin
```
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,10 @@
},
"pnpm": {
"overrides": {
"vitest": "^0.27.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"@mdx-js/react": "^2.3.0",
"next-themes": "^0.2.1",
"next": "^13.4.8",
"@types/react": "^18.0.15",
"@types/react-dom": "^18.0.6"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/nextra-theme-blog/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"@types/react": "^18.0.15",
"@types/react-dom": "^18.0.6",
"concurrently": "^8.0.0",
"next": "^13.4.8",
"next": "^13.4.19",
"nextra": "workspace:*",
"postcss": "^8.4.28",
"postcss-cli": "^10.1.0",
Expand All @@ -79,7 +79,7 @@
"react-cusdis": "^2.1.3",
"react-dom": "^18.2.0",
"tailwindcss": "^3.3.3",
"vitest": "^0.34.0"
"vitest": "^0.34.2"
},
"sideEffects": false
}
6 changes: 3 additions & 3 deletions packages/nextra-theme-docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@
"@types/git-url-parse": "^9.0.1",
"@types/react": "^18.0.15",
"@types/react-dom": "^18.0.6",
"@vitejs/plugin-react": "^3.0.1",
"@vitejs/plugin-react": "^4.0.4",
"concurrently": "^8.0.0",
"jsdom": "^22.0.0",
"next": "^13.4.8",
"next": "^13.4.19",
"nextra": "workspace:*",
"postcss": "^8.4.28",
"postcss-cli": "^10.1.0",
Expand All @@ -70,7 +70,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"tailwindcss": "^3.3.3",
"vitest": "^0.34.0"
"vitest": "^0.34.2"
},
"sideEffects": [
"./src/polyfill.ts"
Expand Down
62 changes: 26 additions & 36 deletions packages/nextra/__test__/__snapshots__/compile.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
// Vitest Snapshot v1
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Process heading > code-h1 1`] = `
{
"frontMatter": {},
"headings": [
{
"depth": 1,
"id": "codegenyml",
"value": "codegen.yml",
},
],
"result": "/*@jsxRuntime automatic @jsxImportSource react*/
import {useMDXComponents as _provideComponents} from \\"nextra/mdx\\";
export const __headings = [{
depth: 1,
value: \\"codegen.yml\\",
id: \\"codegenyml\\"
}];
function _createMdxContent(props) {
const _components = Object.assign({
h1: \\"h1\\",
Expand All @@ -33,15 +31,13 @@ export default MDXContent;
exports[`Process heading > code-with-text-h1 1`] = `
{
"frontMatter": {},
"headings": [
{
"depth": 1,
"id": "codegenyml-file",
"value": "codegen.yml file",
},
],
"result": "/*@jsxRuntime automatic @jsxImportSource react*/
import {useMDXComponents as _provideComponents} from \\"nextra/mdx\\";
export const __headings = [{
depth: 1,
value: \\"codegen.yml file\\",
id: \\"codegenyml-file\\"
}];
function _createMdxContent(props) {
const _components = Object.assign({
h1: \\"h1\\",
Expand All @@ -64,20 +60,18 @@ exports[`Process heading > dynamic-h1 1`] = `
{
"frontMatter": {},
"hasJsxInH1": true,
"headings": [
{
"depth": 1,
"id": "posts-tagged-with-",
"value": "Posts Tagged with “”",
},
],
"result": "/*@jsxRuntime automatic @jsxImportSource react*/
import {useMDXComponents as _provideComponents} from \\"nextra/mdx\\";
import {useRouter} from 'next/router';
export const TagName = () => {
const {tag} = useRouter().query;
return tag || null;
};
export const __headings = [{
depth: 1,
value: \\"Posts Tagged with “”\\",
id: \\"posts-tagged-with-\\"
}];
function _createMdxContent(props) {
const _components = Object.assign({
h1: \\"h1\\"
Expand All @@ -98,15 +92,13 @@ export default MDXContent;
exports[`Process heading > no-h1 1`] = `
{
"frontMatter": {},
"headings": [
{
"depth": 2,
"id": "h2",
"value": "H2",
},
],
"result": "/*@jsxRuntime automatic @jsxImportSource react*/
import {useMDXComponents as _provideComponents} from \\"nextra/mdx\\";
export const __headings = [{
depth: 2,
value: \\"H2\\",
id: \\"h2\\"
}];
function _createMdxContent(props) {
const _components = Object.assign({
h2: \\"h2\\"
Expand All @@ -126,15 +118,13 @@ export default MDXContent;
exports[`Process heading > static-h1 1`] = `
{
"frontMatter": {},
"headings": [
{
"depth": 1,
"id": "hello-world",
"value": "Hello World",
},
],
"result": "/*@jsxRuntime automatic @jsxImportSource react*/
import {useMDXComponents as _provideComponents} from \\"nextra/mdx\\";
export const __headings = [{
depth: 1,
value: \\"Hello World\\",
id: \\"hello-world\\"
}];
function _createMdxContent(props) {
const _components = Object.assign({
h1: \\"h1\\"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Vitest Snapshot v1
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`context > getAllPages() > should work 1`] = `
[
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Vitest Snapshot v1
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`normalize-page > /404 page 1`] = `
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Vitest Snapshot v1
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`Page Process > pageMap en-US 1`] = `
[
Expand Down
1 change: 0 additions & 1 deletion packages/nextra/__test__/collect-catch-all.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { describe, expect, it } from 'vitest'
import { createCatchAllMeta } from '../src/catch-all'
import { collectCatchAllRoutes } from '../src/setup-page'

Expand Down
75 changes: 74 additions & 1 deletion packages/nextra/__test__/compile.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fs from 'node:fs/promises'
import path from 'node:path'
import { describe, expect, it } from 'vitest'
import { compileMdx } from '../src/compile'
import { CWD } from '../src/constants'

Expand Down Expand Up @@ -77,6 +76,80 @@ describe('Process heading', () => {
const { result } = await compileMdx(`### My Header`, { mdxOptions })
expect(result).toMatch(`<_components.h3 id="my-header">{"My Header"}`)
})

it('should merge headings from partial components', async () => {
const { result } = await compileMdx(
`
import FromMdx from './one.mdx'
import FromMarkdown from './two.md'
import IgnoreMe from './foo'

## ❤️

<FromMdx />

## ✅

<FromMarkdown />

import Last from './three.mdx'

<Last />

<IgnoreMe />

## 👋`,
{ mdxOptions }
)
expect(result).toMatchInlineSnapshot(`
"/*@jsxRuntime automatic @jsxImportSource react*/
import {useMDXComponents as _provideComponents} from \\"nextra/mdx\\";
import FromMdx, {__headings as __headings0} from './one.mdx';
import FromMarkdown, {__headings as __headings1} from './two.md';
import IgnoreMe from './foo';
import Last, {__headings as __headings2} from './three.mdx';
export const __headings = [{
depth: 2,
value: \\"❤️\\",
id: \\"️\\"
}, ...__headings0, {
depth: 2,
value: \\"✅\\",
id: \\"\\"
}, ...__headings1, ...__headings2, {
depth: 2,
value: \\"👋\\",
id: \\"-1\\"
}];
function _createMdxContent(props) {
const _components = Object.assign({
h2: \\"h2\\"
}, _provideComponents(), props.components);
return <><_components.h2 id=\\"️\\">{\\"❤️\\"}</_components.h2>{\\"\\\\n\\"}<FromMdx />{\\"\\\\n\\"}<_components.h2 id=\\"\\">{\\"✅\\"}</_components.h2>{\\"\\\\n\\"}<FromMarkdown />{\\"\\\\n\\"}{\\"\\\\n\\"}<Last />{\\"\\\\n\\"}<IgnoreMe />{\\"\\\\n\\"}<_components.h2 id=\\"-1\\">{\\"👋\\"}</_components.h2></>;
}
function MDXContent(props = {}) {
const {wrapper: MDXLayout} = Object.assign({}, _provideComponents(), props.components);
return MDXLayout ? <MDXLayout {...props}><_createMdxContent {...props} /></MDXLayout> : _createMdxContent(props);
}
export default MDXContent;
"
`)
})
it('should not attach headings with parent Tab or Tabs.Tab', async () => {
const { result } = await compileMdx(
`
<Tab>
## hello
</Tab>

<Tabs.Tab>
## World
</Tabs.Tab>
`,
{ mdxOptions }
)
expect(result).toMatch('export const __headings = [];')
})
})

describe('Link', () => {
Expand Down
1 change: 0 additions & 1 deletion packages/nextra/__test__/context.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import path from 'node:path'
import { beforeAll, describe, expect, it } from 'vitest'
import { CWD, NEXTRA_INTERNAL } from '../src/constants'
import {
getAllPages,
Expand Down
1 change: 0 additions & 1 deletion packages/nextra/__test__/loader.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import fs from 'node:fs/promises'
import path from 'node:path'
import { describe, expect, it } from 'vitest'
import { CWD } from '../src/constants'

describe('tree shaking', async () => {
Expand Down
1 change: 0 additions & 1 deletion packages/nextra/__test__/locale-with-base-path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ process.env.__NEXT_ROUTER_BASEPATH = '/testBasePath'

import { NextRequest } from 'next/server'
import type NextServer from 'next/server'
import { describe, expect, it, vi } from 'vitest'
import { locales as originalLocales } from '../src/locales'

vi.mock('next/server', async () => {
Expand Down
1 change: 0 additions & 1 deletion packages/nextra/__test__/locale.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { NextRequest } from 'next/server'
import type NextServer from 'next/server'
import { describe, expect, it, vi } from 'vitest'
import { locales as originalLocales } from '../src/locales'

vi.mock('next/server', async () => {
Expand Down
1 change: 0 additions & 1 deletion packages/nextra/__test__/normalize-page.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { describe, expect, it } from 'vitest'
import { normalizePages } from '../src/normalize-pages'
import { cnPageMap, usPageMap } from './fixture/page-maps/pageMap'

Expand Down
1 change: 0 additions & 1 deletion packages/nextra/__test__/page-map.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import path from 'node:path'
import { beforeAll, describe, expect, it } from 'vitest'
import { CWD } from '../src/constants'
import { getDynamicMeta, resolvePageMap } from '../src/page-map'
import { collectFiles } from '../src/plugin'
Expand Down
1 change: 0 additions & 1 deletion packages/nextra/__test__/sort-pages.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { describe, expect, it } from 'vitest'
import { sortPages } from '../src/utils'

const kind = 'MdxPage'
Expand Down
1 change: 0 additions & 1 deletion packages/nextra/__test__/use-fs-route.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { renderHook } from '@testing-library/react'
import { useRouter } from 'next/router'
import type { Mock } from 'vitest'
import { describe, expect, it, vi } from 'vitest'
import { useFSRoute } from '../src/hooks'

vi.mock('next/router', () => ({
Expand Down
Loading