Skip to content

Commit

Permalink
[v3] more major updates (#2251)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimitri POSTOLOV authored Sep 4, 2023
1 parent 919fe97 commit 2f3be33
Show file tree
Hide file tree
Showing 20 changed files with 56 additions and 74 deletions.
11 changes: 11 additions & 0 deletions .changeset/mighty-cars-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
'nextra': major
---

- set `"engines.node": ">=18"`

- remove `Tab` export, use `Tabs.Tab` instead

- remove `Card` export, use `Cards.Card` instead

- disallow import md/mdx files that are outside of the working directory, use symlinks instead
4 changes: 2 additions & 2 deletions examples/docs/src/pages/features/ssg.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ can also be cached by a CDN to maximize the performance.

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

import { useSSG } from 'nextra/ssg'
import { useSSG } from 'nextra/data'

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

```js filename="ssg.mdx"
import { useSSG } from 'nextra/ssg'
import { useSSG } from 'nextra/data'

export const getStaticProps = ({ params }) => {
return fetch(`https://api.github.com/repos/shuding/nextra`)
Expand Down
2 changes: 1 addition & 1 deletion examples/swr-site/pages/en/docs/change-log.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Change Log

import { useSSG } from 'nextra/ssg'
import { useSSG } from 'nextra/data'

export const ReleasesRenderer = () => {
const releases = useSSG()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Callout, Tab, Tabs } from 'nextra/components'
import { Callout, Tabs } from 'nextra/components'
import { RemoteContent } from 'nextra/data'
import { buildDynamicMDX, buildDynamicMeta } from 'nextra/remote'
import json from '../../../../nextra-remote-filepaths/graphql-eslint.json'
Expand Down Expand Up @@ -29,4 +29,4 @@ export const getStaticPaths = () => ({
}))
})

<RemoteContent components={{ Callout, $Tab: Tab, $Tabs: Tabs }} />
<RemoteContent components={{ Callout, $Tabs: Tabs }} />
11 changes: 9 additions & 2 deletions examples/swr-site/pages/en/remote/graphql-yoga/[[...slug]].mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Callout, Tab, Tabs } from 'nextra/components'
import { Callout, Tabs } from 'nextra/components'
import { RemoteContent } from 'nextra/data'
import { buildDynamicMDX, buildDynamicMeta } from 'nextra/remote'
import json from '../../../../nextra-remote-filepaths/graphql-yoga.json'
Expand Down Expand Up @@ -29,4 +29,11 @@ export const getStaticPaths = () => ({
}))
})

<RemoteContent components={{ Callout, PackageCmd: () => null, Tab, Tabs }} />
<RemoteContent
components={{
Callout,
PackageCmd: () => null,
Tab: Tabs.Tab,
Tabs
}}
/>
2 changes: 1 addition & 1 deletion examples/swr-site/pages/es/docs/change-log.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Markdown from 'markdown-to-jsx'
import { useSSG } from 'nextra/ssg'
import { useSSG } from 'nextra/data'

export const getStaticProps = () => {
return (
Expand Down
2 changes: 1 addition & 1 deletion examples/swr-site/pages/ru/docs/change-log.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Markdown from 'markdown-to-jsx'
import { useSSG } from 'nextra/ssg'
import { useSSG } from 'nextra/data'

export const getStaticProps = () => {
return (
Expand Down
2 changes: 1 addition & 1 deletion packages/nextra-theme-blog/src/tags.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { GetStaticPaths, GetStaticProps } from 'next'
import Head from 'next/head'
import { useSSG } from 'nextra/ssg'
import { useSSG } from 'nextra/data'
import { getStaticTags } from './utils/get-tags'

const NEXTRA_INTERNAL = Symbol.for('__nextra_internal__')
Expand Down
17 changes: 5 additions & 12 deletions packages/nextra/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,16 @@
"repository": "https://github.com/shuding/nextra",
"license": "MIT",
"engines": {
"node": ">=16"
"node": ">=18"
},
"main": "./dist/index.js",
"exports": {
"./package.json": "./package.json",
".": "./dist/index.js",
"./catch-all": "./dist/catch-all.js",
"./data": {
"import": "./dist/ssg.js",
"types": "./dist/ssg.d.mts"
},
"./ssg": {
"import": "./dist/ssg.js",
"types": "./dist/ssg.d.mts"
"import": "./dist/data.js",
"types": "./dist/data.d.mts"
},
"./loader": "./loader.js",
"./setup-page": {
Expand Down Expand Up @@ -72,10 +68,7 @@
"./dist/context.d.mts"
],
"data": [
"./dist/ssg.d.mts"
],
"ssg": [
"./dist/ssg.d.mts"
"./dist/data.d.mts"
],
"icons": [
"./dist/icons/index.d.mts"
Expand Down Expand Up @@ -128,7 +121,7 @@
"@mdx-js/react": "^2.3.0",
"@napi-rs/simple-git": "^0.1.9",
"@theguild/remark-mermaid": "^0.0.4",
"@theguild/remark-npm2yarn": "^0.1.1",
"@theguild/remark-npm2yarn": "0.2.0-alpha-20230904225308-5ba1c7b",
"clsx": "^2.0.0",
"github-slugger": "^2.0.0",
"graceful-fs": "^4.2.11",
Expand Down
15 changes: 7 additions & 8 deletions packages/nextra/src/compile.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { createRequire } from 'node:module'
import path from 'node:path'
import type { ProcessorOptions } from '@mdx-js/mdx'
import { createProcessor } from '@mdx-js/mdx'
Expand Down Expand Up @@ -29,7 +28,6 @@ import {
remarkLinkRewrite,
remarkMdxDisableExplicitJsx,
remarkRemoveImports,
remarkReplaceImports,
remarkStaticImage,
remarkStructurize
} from './mdx-plugins'
Expand All @@ -46,8 +44,6 @@ globalThis.__nextra_temp_do_not_use = () => {
import('./__temp__')
}

const require = createRequire(import.meta.url)

const DEFAULT_REHYPE_PRETTY_CODE_OPTIONS: RehypePrettyCodeOptions = {
// @ts-expect-error -- TODO: fix type error
theme,
Expand Down Expand Up @@ -153,6 +149,12 @@ export async function compileMdx(
const isFileOutsideCWD =
!isPageImport && path.relative(CWD, filePath).startsWith('..')

if (isFileOutsideCWD) {
throw new Error(
`Unexpected import of "${filePath}" that is outside of working directory, use symlinks instead`
)
}

const isRemoteContent = outputFormat === 'function-body'

const compiler =
Expand Down Expand Up @@ -193,9 +195,7 @@ export async function compileMdx(
jsx,
format,
outputFormat,
providerImportSource: isFileOutsideCWD
? require.resolve('nextra').replace(/index\.js$/, 'mdx.js') // fixes Package subpath './mdx' is not defined by "exports"
: 'nextra/mdx',
providerImportSource: 'nextra/mdx',
remarkPlugins: [
...(remarkPlugins || []),
remarkMermaid, // should be before remarkRemoveImports because contains `import { Mermaid } from ...`
Expand All @@ -222,7 +222,6 @@ export async function compileMdx(
staticImage && remarkStaticImage,
readingTime && remarkReadingTime,
latex && remarkMath,
isFileOutsideCWD && remarkReplaceImports,
// Remove the markdown file extension from links
[
clonedRemarkLinkRewrite,
Expand Down
2 changes: 1 addition & 1 deletion packages/nextra/src/components/cards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const arrowEl = (
</span>
)

export function Card({
function Card({
children,
title,
icon,
Expand Down
4 changes: 2 additions & 2 deletions packages/nextra/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ export { CopyToClipboard } from './copy-to-clipboard'
export { Code } from './code'
export { Pre } from './pre'
export { Steps } from './steps'
export { Tabs, Tab } from './tabs'
export { Tabs } from './tabs'
export { Td } from './td'
export { Table } from './table'
export { Th } from './th'
export { Tr } from './tr'
export { Cards, Card } from './cards'
export { Cards } from './cards'
export { FileTree } from './file-tree'
export { Mermaid } from '@theguild/remark-mermaid/mermaid'
5 changes: 1 addition & 4 deletions packages/nextra/src/components/tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,7 @@ function _Tabs({
)
}

export function Tab({
children,
...props
}: ComponentProps<'div'>): ReactElement {
function Tab({ children, ...props }: ComponentProps<'div'>): ReactElement {
return (
<HeadlessTab.Panel {...props} className="nx-rounded nx-pt-6">
{children}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useMDXComponents } from './mdx'
export const SSGContext = createContext<any>(false)
export const useSSG = (key = 'ssg') => useContext(SSGContext)?.[key]

// Make sure nextra/ssg remains functional, but we now recommend this new API.
// Make sure nextra/data remains functional, but we now recommend this new API.

export const DataContext = SSGContext
export const useData = useSSG
Expand Down
2 changes: 1 addition & 1 deletion packages/nextra/src/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { ReactElement } from 'react'
import { SSGContext } from './ssg'
import { SSGContext } from './data'
import { useInternals } from './use-internals'

export default function Nextra({
Expand Down
1 change: 0 additions & 1 deletion packages/nextra/src/mdx-plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export { parseMeta, attachMeta } from './rehype'
export { remarkCustomHeadingId } from './remark-custom-heading-id'
export { remarkHeadings } from './remark-headings'
export { remarkReplaceImports } from './remark-replace-imports'
export { remarkRemoveImports } from './remark-remove-imports'
export {
remarkLinkRewrite,
Expand Down
24 changes: 0 additions & 24 deletions packages/nextra/src/mdx-plugins/remark-replace-imports.ts

This file was deleted.

6 changes: 3 additions & 3 deletions packages/nextra/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function getDefault<T>(module: T & { default?: T }): T {
}

export const logger = {
info: console.log.bind(null, '- ', '\x1b[36minfo\x1b[0m', '[nextra]'),
warn: console.log.bind(null, '- ', '\x1b[33mwarn\x1b[0m', '[nextra]'),
error: console.log.bind(null, '- ', '\x1b[31mwarn\x1b[0m', '[nextra]')
info: console.log.bind(null, '-', '\x1b[36minfo\x1b[0m', '[nextra]'),
warn: console.log.bind(null, '-', '\x1b[33mwarn\x1b[0m', '[nextra]'),
error: console.log.bind(null, '-', '\x1b[31mwarn\x1b[0m', '[nextra]')
}
2 changes: 1 addition & 1 deletion packages/nextra/tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { defineConfig } from 'tsup'

const CLIENT_ENTRY = [
'src/{use-internals,setup-page,normalize-pages,mdx,context}.ts',
'src/{ssg,layout}.tsx',
'src/{data,layout}.tsx',
'src/{components,hooks,icons}/*.{ts,tsx}'
]

Expand Down
12 changes: 6 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2f3be33

Please sign in to comment.