Skip to content

Commit 8efbb45

Browse files
author
Dimitri POSTOLOV
authoredOct 23, 2023
[v3] remove nextra/data export, move useData to nextra/hooks, RemoteContent to nextra/components (#2467)
1 parent 80e11e0 commit 8efbb45

File tree

25 files changed

+111
-112
lines changed

25 files changed

+111
-112
lines changed
 

‎.changeset/pretty-colts-float.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'nextra': patch
3+
---
4+
5+
remove `nextra/data` export, move `useData` to `nextra/hooks`, `RemoteContent` to `nextra/components`

‎docs/pages/docs/guide/built-ins/cards.mdx

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { CardsIcon, OneIcon, WarningIcon } from '@components/icons'
2-
import { Cards } from 'nextra/components'
3-
import { RemoteContent } from 'nextra/data'
2+
import { Cards, RemoteContent } from 'nextra/components'
43
import { buildDynamicMDX } from 'nextra/remote'
54

65
export async function getStaticProps() {

‎docs/pages/docs/guide/ssg.mdx

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useData } from 'nextra/data'
1+
import { useData } from 'nextra/hooks'
22

33
# Next.js SSG
44

@@ -44,7 +44,7 @@ enabled, it will be kept up to date.
4444
Here's the MDX code for the example above:
4545

4646
```mdx filename="MDX"
47-
import { useData } from 'nextra/data'
47+
import { useData } from 'nextra/hooks'
4848

4949
export function getStaticProps() {
5050
return fetch('https://api.github.com/repos/shuding/nextra')

‎examples/docs/src/pages/features/ssg.mdx

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ can also be cached by a CDN to maximize the performance.
77

88
This is supported by Nextra too. Here's an example:
99

10-
import { useData } from 'nextra/data'
10+
import { useData } from 'nextra/hooks'
1111

1212
export const getStaticProps = ({ params }) => {
1313
return fetch(`https://api.github.com/repos/shuding/nextra`)
@@ -44,7 +44,7 @@ enabled, it will be kept up to date.
4444
Here's the MDX code for the example above:
4545

4646
```js filename="ssg.mdx"
47-
import { useData } from 'nextra/data'
47+
import { useData } from 'nextra/hooks'
4848

4949
export const getStaticProps = ({ params }) => {
5050
return fetch(`https://api.github.com/repos/shuding/nextra`)

‎examples/swr-site/pages/en/docs/change-log.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Change Log
22

3-
import { useData } from 'nextra/data'
3+
import { useData } from 'nextra/hooks'
44

55
export const ReleasesRenderer = () => {
66
const releases = useData()

‎examples/swr-site/pages/en/remote/[slug].mdx.bak

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { RemoteContent } from 'nextra/data'
1+
import { RemoteContent } from 'nextra/components'
22
import { buildDynamicMDX, buildDynamicMeta } from 'nextra/remote'
33

44
export const getStaticProps = async ({ params }) => {

‎examples/swr-site/pages/en/remote/graphql-eslint/[[...slug]].mdx

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { Callout, Tabs } from 'nextra/components'
2-
import { RemoteContent } from 'nextra/data'
1+
import { Callout, RemoteContent, Tabs } from 'nextra/components'
32
import { buildDynamicMDX, buildDynamicMeta } from 'nextra/remote'
43
import json from '../../../../nextra-remote-filepaths/graphql-eslint.json'
54

‎examples/swr-site/pages/en/remote/graphql-yoga/[[...slug]].mdx

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { Callout, Tabs } from 'nextra/components'
2-
import { RemoteContent } from 'nextra/data'
1+
import { Callout, RemoteContent, Tabs } from 'nextra/components'
32
import { buildDynamicMDX, buildDynamicMeta } from 'nextra/remote'
43
import json from '../../../../nextra-remote-filepaths/graphql-yoga.json'
54

‎examples/swr-site/pages/es/docs/change-log.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Markdown from 'markdown-to-jsx'
2-
import { useData } from 'nextra/data'
2+
import { useData } from 'nextra/hooks'
33

44
export function getStaticProps() {
55
return (

‎examples/swr-site/pages/ru/docs/change-log.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Markdown from 'markdown-to-jsx'
2-
import { useData } from 'nextra/data'
2+
import { useData } from 'nextra/hooks'
33

44
export const getStaticProps = () => {
55
return (

‎packages/nextra-theme-blog/src/tags.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { GetStaticPaths, GetStaticProps } from 'next'
22
import Head from 'next/head'
3-
import { useData } from 'nextra/data'
3+
import { useData } from 'nextra/hooks'
44
import { getStaticTags } from './utils/get-tags'
55

66
const NEXTRA_INTERNAL = Symbol.for('__nextra_internal__')

‎packages/nextra/package.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,6 @@
7777
"context": [
7878
"./dist/client/context.d.ts"
7979
],
80-
"data": [
81-
"./dist/client/data.d.ts"
82-
],
8380
"icons": [
8481
"./dist/client/icons/index.d.ts"
8582
],
@@ -140,7 +137,7 @@
140137
"remark-gfm": "^3.0.1",
141138
"remark-math": "^6.0.0",
142139
"remark-reading-time": "^2.0.1",
143-
"shiki": "npm:shikiji@0.6.8",
140+
"shiki": "npm:shikiji@0.6.10",
144141
"slash": "^5.1.0",
145142
"title": "^3.5.3",
146143
"unist-util-remove": "^4.0.0",
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
export { Button } from './button.js'
22
export { Callout } from './callout.js'
3-
export { CopyToClipboard } from './copy-to-clipboard.js'
3+
export { Cards } from './cards.js'
44
export { Code } from './code.js'
5+
export { CopyToClipboard } from './copy-to-clipboard.js'
6+
export { FileTree } from './file-tree.js'
57
export { Pre } from './pre.js'
8+
export { RemoteContent } from './remote-content.js'
69
export { Steps } from './steps.js'
10+
export { Table } from './table.js'
711
export { Tabs } from './tabs.js'
812
export { Td } from './td.js'
9-
export { Table } from './table.js'
1013
export { Th } from './th.js'
1114
export { Tr } from './tr.js'
12-
export { Cards } from './cards.js'
13-
export { FileTree } from './file-tree.js'
1415
export { Mermaid } from '@theguild/remark-mermaid/mermaid'
15-
export { RemoteContent } from '../data.js'
1616
export { MathJax, MathJaxContext } from 'better-react-mathjax'

‎packages/nextra/src/client/data.tsx renamed to ‎packages/nextra/src/client/components/remote-content.tsx

+6-15
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
1-
import { createContext, useContext } from 'react'
2-
import { jsxRuntime } from './jsx-runtime.cjs'
3-
import type { Components } from './mdx'
4-
import { useMDXComponents } from './mdx.js'
5-
6-
const SSGContext = createContext<Record<string, any>>({})
7-
8-
export const useData = (key = 'ssg') => useContext(SSGContext)[key]
9-
10-
// Make sure nextra/data remains functional, but we now recommend this new API.
11-
12-
export const DataProvider = SSGContext.Provider
13-
SSGContext.displayName = 'SSG'
1+
import { useData } from '../hooks/index.js'
2+
import { jsxRuntime } from '../jsx-runtime.cjs'
3+
import type { Components } from '../mdx.js'
4+
import { useMDXComponents } from '../mdx.js'
145

156
function evaluate(compiledSource: string, scope: Record<string, unknown> = {}) {
167
// if we're ready to render, we can assemble the component tree and let React do its thing
@@ -19,7 +10,7 @@ function evaluate(compiledSource: string, scope: Record<string, unknown> = {}) {
1910
const keys = Object.keys(scope)
2011
const values = Object.values(scope)
2112
// now we eval the source code using a function constructor
22-
// in order for this to work we need to have React, the mdx createElement,
13+
// in order for this to work we need to have React, the mdx `createElement`,
2314
// and all our components in scope for the function, which is the case here
2415
// we pass the names (via keys) in as the function's args, and execute the
2516
// function with the actual values.
@@ -43,7 +34,7 @@ export function RemoteContent({
4334
* Pass-through variables for use in the MDX content
4435
*/
4536
scope?: Record<string, unknown>
46-
} = {}) {
37+
}) {
4738
const compiledSource = useData('__nextra_dynamic_mdx')
4839
if (!compiledSource) {
4940
throw new Error(
+4-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
export * from './use-mounted.js'
2-
export * from './use-fs-route.js'
3-
export * from './use-router.js'
1+
export { useData } from './use-data.js'
2+
export { useMounted } from './use-mounted.js'
3+
export { useFSRoute } from './use-fs-route.js'
4+
export { useRouter } from './use-router.js'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { createContext, useContext } from 'react'
2+
3+
const SSGContext = createContext<Record<string, any>>({})
4+
5+
export const useData = (key = 'ssg') => useContext(SSGContext)[key]
6+
7+
// Make sure nextra/hooks remains functional, but we now recommend this new API.
8+
9+
export const DataProvider = SSGContext.Provider
10+
SSGContext.displayName = 'SSG'

‎packages/nextra/src/client/mdx.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import {
2-
MDXContext,
3-
useMDXComponents as originalUseMDXComponents
4-
} from '@mdx-js/react'
1+
import { useMDXComponents as originalUseMDXComponents } from '@mdx-js/react'
52
import type { Components } from '@mdx-js/react/lib'
63
import Image, { type ImageProps } from 'next/image'
74
import { createElement } from 'react'
@@ -24,5 +21,3 @@ export const useMDXComponents: typeof originalUseMDXComponents = components => {
2421
export { MDXProvider } from '@mdx-js/react'
2522

2623
export type { Components }
27-
28-
MDXContext.displayName = 'MDX'

‎packages/nextra/src/client/setup-page.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import type {
1212
PageOpts
1313
} from '../types'
1414
import { findFolder } from '../utils.js'
15-
import { DataProvider } from './data.js'
1615
import { useRouter } from './hooks/index.js'
16+
import { DataProvider } from './hooks/use-data.js'
1717
import { useMDXComponents } from './mdx.js'
1818

1919
export function HOC_MDXWrapper(

‎packages/nextra/src/server/compile.ts

+1-42
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ import { remarkMermaid } from '@theguild/remark-mermaid'
66
import { remarkNpm2Yarn } from '@theguild/remark-npm2yarn'
77
import type { Program } from 'estree'
88
import rehypeKatex from 'rehype-katex'
9-
import type { Options as RehypePrettyCodeOptions } from 'rehype-pretty-code'
109
import rehypePrettyCode from 'rehype-pretty-code'
1110
import rehypeRaw from 'rehype-raw'
1211
import remarkFrontmatter from 'remark-frontmatter'
1312
import remarkGfm from 'remark-gfm'
1413
import remarkMath from 'remark-math'
1514
import remarkReadingTime from 'remark-reading-time'
16-
import { bundledLanguages, getHighlighter } from 'shiki'
1715
import type { Pluggable, Plugin } from 'unified'
1816
import type {
1917
FrontMatter,
@@ -23,7 +21,6 @@ import type {
2321
StructurizedData
2422
} from '../types'
2523
import {
26-
CODE_BLOCK_FILENAME_REGEX,
2724
CWD,
2825
DEFAULT_LOCALE,
2926
ERROR_ROUTES,
@@ -34,6 +31,7 @@ import {
3431
recmaRewriteJsx
3532
} from './recma-plugins/index.js'
3633
import {
34+
DEFAULT_REHYPE_PRETTY_CODE_OPTIONS,
3735
rehypeAttachCodeMeta,
3836
rehypeBetterReactMathjax,
3937
rehypeExtractTocContent,
@@ -53,45 +51,6 @@ import {
5351
} from './remark-plugins/index.js'
5452
import { logger, truthy } from './utils.js'
5553

56-
export const DEFAULT_REHYPE_PRETTY_CODE_OPTIONS: RehypePrettyCodeOptions = {
57-
keepBackground: false,
58-
grid: false,
59-
onVisitLine(node) {
60-
// Prevent lines from collapsing in `display: grid` mode, and
61-
// allow empty lines to be copy/pasted
62-
if (node.children.length === 0) {
63-
node.children.push({ type: 'text', value: ' ' })
64-
}
65-
delete node.properties['data-line']
66-
},
67-
filterMetaString: meta => meta.replace(CODE_BLOCK_FILENAME_REGEX, ''),
68-
async getHighlighter(_opts) {
69-
const DEFAULT_OPTS = {
70-
themes: {
71-
light: 'github-light',
72-
dark: 'github-dark'
73-
},
74-
defaultColor: false
75-
} as const
76-
77-
const highlighter = await getHighlighter({
78-
themes: Object.values(DEFAULT_OPTS.themes),
79-
langs: Object.keys(bundledLanguages)
80-
})
81-
82-
const originalCodeToHtml = highlighter.codeToHtml
83-
84-
return Object.assign(highlighter, {
85-
codeToHtml(code: string, lang: string) {
86-
return originalCodeToHtml(code, { lang, ...DEFAULT_OPTS })
87-
},
88-
ansiToHtml(code: string) {
89-
return this.codeToHtml(code, 'ansi')
90-
}
91-
})
92-
}
93-
}
94-
9554
const cachedCompilerForFormat: Record<
9655
Exclude<ProcessorOptions['format'], undefined | null>,
9756
Processor

‎packages/nextra/src/server/constants.ts

-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ export const MARKDOWN_EXTENSIONS = ['md', 'mdx'] as const
3838

3939
export const EXTERNAL_URL_REGEX = /^https?:\/\//
4040

41-
export const CODE_BLOCK_FILENAME_REGEX = /filename="([^"]+)"/
42-
4341
export const DEFAULT_LOCALES = ['']
4442

4543
// Experimental, need to deep dive why bundle becomes bigger and there is full

‎packages/nextra/src/server/rehype-plugins/__tests__/rehype-icon.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { compile } from '@mdx-js/mdx'
22
import type { VFile } from '@mdx-js/mdx/lib/compile'
33
import rehypePrettyCode from 'rehype-pretty-code'
44
import { clean } from '../../../../__test__/test-utils.js'
5-
import { DEFAULT_REHYPE_PRETTY_CODE_OPTIONS } from '../../compile.js'
65
import {
6+
DEFAULT_REHYPE_PRETTY_CODE_OPTIONS,
77
rehypeAttachCodeMeta,
88
rehypeIcon,
99
rehypeParseCodeMeta
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
export { rehypeParseCodeMeta, rehypeAttachCodeMeta } from './rehype.js'
1+
export {
2+
rehypeParseCodeMeta,
3+
rehypeAttachCodeMeta,
4+
DEFAULT_REHYPE_PRETTY_CODE_OPTIONS
5+
} from './rehype.js'
26
export { rehypeBetterReactMathjax } from './rehype-better-react-mathjax.js'
37
export { rehypeExtractTocContent } from './rehype-extract-toc-content.js'
48
export { rehypeIcon } from './rehype-icon.js'

‎packages/nextra/src/server/rehype-plugins/rehype.ts

+43-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,55 @@
11
import type { Element } from 'hast'
2+
import type { Options as RehypePrettyCodeOptions } from 'rehype-pretty-code'
3+
import { bundledLanguages, getHighlighter } from 'shiki'
24
import type { Plugin } from 'unified'
35
import { visit } from 'unist-util-visit'
4-
import { CODE_BLOCK_FILENAME_REGEX } from '../constants.js'
56

67
type PreElement = Element & {
78
__filename?: string
89
__hasCopyCode?: boolean
910
}
1011

12+
const CODE_BLOCK_FILENAME_REGEX = /filename="([^"]+)"/
13+
14+
export const DEFAULT_REHYPE_PRETTY_CODE_OPTIONS: RehypePrettyCodeOptions = {
15+
keepBackground: false,
16+
grid: false,
17+
onVisitLine(node) {
18+
// Prevent lines from collapsing in `display: grid` mode, and
19+
// allow empty lines to be copy/pasted
20+
if (node.children.length === 0) {
21+
node.children.push({ type: 'text', value: ' ' })
22+
}
23+
delete node.properties['data-line']
24+
},
25+
filterMetaString: meta => meta.replace(CODE_BLOCK_FILENAME_REGEX, ''),
26+
async getHighlighter(_opts) {
27+
const DEFAULT_OPTS = {
28+
themes: {
29+
light: 'github-light',
30+
dark: 'github-dark'
31+
},
32+
defaultColor: false
33+
} as const
34+
35+
const highlighter = await getHighlighter({
36+
themes: Object.values(DEFAULT_OPTS.themes),
37+
langs: Object.keys(bundledLanguages)
38+
})
39+
40+
const originalCodeToHtml = highlighter.codeToHtml
41+
42+
return Object.assign(highlighter, {
43+
codeToHtml(code: string, lang: string) {
44+
return originalCodeToHtml(code, { lang, ...DEFAULT_OPTS })
45+
},
46+
ansiToHtml(code: string) {
47+
return this.codeToHtml(code, 'ansi')
48+
}
49+
})
50+
}
51+
}
52+
1153
export const rehypeParseCodeMeta: Plugin<
1254
[{ defaultShowCopyCode?: boolean }],
1355
any

‎packages/nextra/src/server/remark-plugins/__tests__/rehype-extract-toc-content.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ export const frontMatter = {
355355
describe('Remote MDX', () => {
356356
it("with outputFormat: 'program'", async () => {
357357
const rawMdx = `
358-
import { RemoteContent } from 'nextra/data'
358+
import { RemoteContent } from 'nextra/components'
359359
360360
## hello
361361
@@ -372,7 +372,7 @@ import { RemoteContent } from 'nextra/data'
372372
import { useMDXComponents as _provideComponents } from 'nextra/mdx'
373373
const title = '[[...slug]]'
374374
const frontMatter = {}
375-
import { RemoteContent } from 'nextra/data'
375+
import { RemoteContent } from 'nextra/components'
376376
export function useTOC(props) {
377377
return [
378378
{

‎pnpm-lock.yaml

+14-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
Please sign in to comment.