Skip to content

Commit

Permalink
remove remarkLinkRewriteOptions from buildDynamicMDX (shuding#1906)
Browse files Browse the repository at this point in the history
  • Loading branch information
dimaMachina authored and filiphuhta committed May 25, 2023
1 parent 093afbd commit 891972f
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 97 deletions.
5 changes: 5 additions & 0 deletions .changeset/tame-pandas-exist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'nextra': patch
---

remove `remarkLinkRewriteOptions` from `buildDynamicMDX`
20 changes: 14 additions & 6 deletions examples/swr-site/pages/remote/graphql-eslint/[[...slug]].mdx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import fs from 'fs/promises'
import { buildDynamicMDX, buildDynamicMeta } from 'nextra/remote'
import { RemoteContent } from 'nextra/data'
import { Callout } from 'nextra/components'
import {
findPathWithExtension,
findStaticPaths
} from '@components/remote-utils'
import { Callout } from 'nextra/components'
import { RemoteContent } from 'nextra/data'
import { remarkLinkRewrite } from 'nextra/mdx-plugins'
import { buildDynamicMDX, buildDynamicMeta } from 'nextra/remote'

export const getStaticProps = async ({ params }) => {
const filePath = await findPathWithExtension({
Expand All @@ -18,9 +19,16 @@ export const getStaticProps = async ({ params }) => {
props: {
...(await buildDynamicMDX(content, {
defaultShowCopyCode: true,
remarkLinkRewriteOptions: {
pattern: /^\/docs(\/.*)?$/,
replace: '/docs/2.0.0$1'
mdxOptions: {
remarkPlugins: [
[
remarkLinkRewrite,
{
pattern: /^\/docs(\/.*)?$/,
replace: '/docs/2.0.0$1'
}
]
]
}
})),
...(await buildDynamicMeta())
Expand Down
14 changes: 11 additions & 3 deletions examples/swr-site/pages/remote/graphql-yoga/[[...slug]].mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from '@components/remote-utils'
import { Callout, Tab, Tabs } from 'nextra-theme-docs'
import { RemoteContent } from 'nextra/data'
import { remarkLinkRewrite } from 'nextra/mdx-plugins'
import { buildDynamicMDX, buildDynamicMeta } from 'nextra/remote'

export const getStaticProps = async ({ params }) => {
Expand All @@ -18,9 +19,16 @@ export const getStaticProps = async ({ params }) => {
props: {
...(await buildDynamicMDX(content, {
defaultShowCopyCode: true,
remarkLinkRewriteOptions: {
pattern: /^\/docs(\/.*)?$/,
replace: '/docs/3.0.0$1'
mdxOptions: {
remarkPlugins: [
[
remarkLinkRewrite,
{
pattern: /^\/docs(\/.*)?$/,
replace: '/docs/3.0.0$1'
}
]
]
}
})),
...(await buildDynamicMeta())
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"vitest": "^0.27.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"@mdx-js/react": "^2.2.1",
"@mdx-js/react": "^2.3.0",
"next-themes": "^0.2.1",
"next": "^13.3.1",
"@types/react": "^18.0.15",
Expand Down
13 changes: 10 additions & 3 deletions packages/nextra/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
"import": "./dist/components/index.js",
"types": "./dist/components/index.d.ts"
},
"./mdx-plugins": {
"import": "./dist/mdx-plugins/index.mjs",
"types": "./dist/mdx-plugins/index.d.ts"
},
"./hooks": {
"import": "./dist/hooks/index.js",
"types": "./dist/hooks/index.d.ts"
Expand Down Expand Up @@ -91,6 +95,9 @@
],
"normalize-pages": [
"./dist/normalize-pages.d.ts"
],
"mdx-plugins": [
"./dist/mdx-plugins/index.d.ts"
]
}
},
Expand Down Expand Up @@ -120,13 +127,13 @@
"@napi-rs/simple-git": "^0.1.8",
"clsx": "^1.2.1",
"github-slugger": "^2.0.0",
"graceful-fs": "^4.2.10",
"graceful-fs": "^4.2.11",
"gray-matter": "^4.0.3",
"katex": "^0.16.4",
"katex": "^0.16.7",
"lodash.get": "^4.4.2",
"next-mdx-remote": "^4.2.1",
"p-limit": "^3.1.0",
"rehype-katex": "^6.0.2",
"rehype-katex": "^6.0.3",
"rehype-pretty-code": "0.9.4",
"remark-gfm": "^3.0.1",
"remark-math": "^5.1.1",
Expand Down
6 changes: 5 additions & 1 deletion packages/nextra/src/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ const cachedCompilerForFormat: Record<
type MdxOptions = LoaderOptions['mdxOptions'] &
Pick<ProcessorOptions, 'jsx' | 'outputFormat'>

// @ts-expect-error -- Without bind is unable to use `remarkLinkRewrite` with `buildDynamicMDX`
// because we already use `remarkLinkRewrite` function to remove .mdx? extensions
const clonedRemarkLinkRewrite = remarkLinkRewrite.bind(null)

export async function compileMdx(
source: string,
loaderOptions: Pick<
Expand Down Expand Up @@ -153,7 +157,7 @@ export async function compileMdx(
isFileOutsideCWD && remarkReplaceImports,
// Remove the markdown file extension from links
[
remarkLinkRewrite,
clonedRemarkLinkRewrite,
{
pattern: MARKDOWN_URL_EXTENSION_REGEX,
replace: '',
Expand Down
53 changes: 29 additions & 24 deletions packages/nextra/src/remote.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,33 @@
import { compileMdx } from './compile'
import type { RemarkLinkRewriteOptions } from './mdx-plugins'
import { remarkLinkRewrite } from './mdx-plugins'
import { truthy } from './utils'

export const buildDynamicMDX = async (
export async function buildDynamicMDX(
content: string,
{
remarkLinkRewriteOptions,
...loaderOptions
}: Parameters<typeof compileMdx>[1] & {
remarkLinkRewriteOptions?: RemarkLinkRewriteOptions
} = {}
) => {
const { result, headings, frontMatter, title } = await compileMdx(content, {
...loaderOptions,
mdxOptions: {
...loaderOptions.mdxOptions,
remarkPlugins: [
...(loaderOptions.mdxOptions?.remarkPlugins || []),
remarkLinkRewriteOptions &&
([remarkLinkRewrite, remarkLinkRewriteOptions] as any)
].filter(truthy)
}
})
loaderOptions: Parameters<typeof compileMdx>[1]
) {
if (loaderOptions && 'remarkLinkRewriteOptions' in loaderOptions) {
throw new Error(`\`remarkLinkRewriteOptions\` was removed. For overriding internal links use \`remarkLinkRewrite\` instead.
import { remarkLinkRewrite } from 'nextra/mdx-plugins'
// ...
const result = await buildDynamicMDX(rawMdx, {
mdxOptions: {
remarkPlugins: [
[remarkLinkRewrite, {
pattern: /^\\/docs(\\/.*)?$/,
replace: '/docs/2.0.0$1'
}]
]
}
})
`)
}

const { result, headings, frontMatter, title } = await compileMdx(
content,
loaderOptions
)

return {
__nextra_dynamic_mdx: result,
Expand All @@ -34,8 +39,8 @@ export const buildDynamicMDX = async (
}
}

export const buildDynamicMeta = async () => {
const resolvePageMap = (globalThis as any).__nextra_resolvePageMap
export async function buildDynamicMeta() {
const resolvePageMap = globalThis.__nextra_resolvePageMap
if (resolvePageMap) {
return {
__nextra_pageMap: await resolvePageMap()
Expand Down

0 comments on commit 891972f

Please sign in to comment.