From 128189fe558dc2265254257ff3a9b241c9949668 Mon Sep 17 00:00:00 2001 From: Jared Watts Date: Fri, 30 Oct 2026 15:57:42 -0700 Subject: [PATCH] Replace next-mdx-remote with @mdx-js/mdx Blog posts are currently compiled and rendered with next-mdx-remote, which is licensed under MPL-2.0 and has actually been archived upstream. It's just a thin wrapper around @mdx-js/mdx, which is MIT licensed, maintained, and already in our dependency tree. This commit now just uses @mdx-js/mdx directly, with the same plugins and options, so every blog post renders the same HTML as it did before. We drop the problematic next-mdx-remote dependency completely. Note that next-mdx-remote also stripped JavaScript out of posts by default. None of our posts uses JavaScript, and I don't see why we'd need to, so we do not reimplement that functionality here. Signed-off-by: Jared Watts --- package-lock.json | 91 +++--------------------------------- package.json | 3 +- pages/blog/[slug].tsx | 63 +++++++++++++------------ src/components/mdx/index.tsx | 4 +- 4 files changed, 43 insertions(+), 118 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5830b49..177b729 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,13 +8,13 @@ "name": "modelplane-website", "version": "0.1.0", "dependencies": { + "@mdx-js/mdx": "^3.1.1", "@vercel/analytics": "^2.0.1", "@vercel/og": "^1.0.0", "@vercel/speed-insights": "^2.0.0", "github-slugger": "^2.0.0", "gray-matter": "^4.0.3", "next": "^16.3.0", - "next-mdx-remote": "^6.0.0", "react": "^18", "react-dom": "^18", "reading-time": "^1.5.0", @@ -26,6 +26,7 @@ "shiki": "^4.2.0" }, "devDependencies": { + "@types/mdx": "^2.0.14", "@types/node": "^20", "@types/react": "^18", "@types/react-dom": "^18", @@ -39,6 +40,7 @@ "version": "7.29.7", "resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.29.7.tgz", "integrity": "sha512-Aup7aUOfpbAUg2ROOJN6Iw5f9DMBlzu0mIkm/malLQFN/YQgO48wCj0Kxa3sEHJvPVFg7siR+qRInwXd2qhQKw==", + "dev": true, "license": "MIT", "dependencies": { "@babel/helper-validator-identifier": "^7.29.7", @@ -213,6 +215,7 @@ "version": "7.29.7", "resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.29.7.tgz", "integrity": "sha512-qehxGkRj55h/ff8EMaJ+cYhyaKlHIxqYDn682wQD7RNp9UujOQsHog2uS0r2vzr4pW+sXf90NeeayjcNaX3fFg==", + "dev": true, "license": "MIT", "engines": { "node": ">=6.9.0" @@ -1124,23 +1127,6 @@ "url": "https://opencollective.com/unified" } }, - "node_modules/@mdx-js/react": { - "version": "3.1.1", - "resolved": "https://registry.npmjs.org/@mdx-js/react/-/react-3.1.1.tgz", - "integrity": "sha512-f++rKLQgUVYDAtECQ6fn/is15GkEH9+nZPM3MS0RcxVqoTfawHvDlSCH7JbMhAM6uJ32v3eXLvLmLvjGu7PTQw==", - "license": "MIT", - "dependencies": { - "@types/mdx": "^2.0.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/unified" - }, - "peerDependencies": { - "@types/react": ">=16", - "react": ">=16" - } - }, "node_modules/@napi-rs/wasm-runtime": { "version": "0.2.12", "resolved": "https://registry.npmjs.org/@napi-rs/wasm-runtime/-/wasm-runtime-0.2.12.tgz", @@ -1581,12 +1567,14 @@ "version": "15.7.15", "resolved": "https://registry.npmjs.org/@types/prop-types/-/prop-types-15.7.15.tgz", "integrity": "sha512-F6bEyamV9jKGAFBEmlQnesRPGOQqS2+Uwi0Em15xenOxHaf2hv6L8YCVn3rPdPJOiJfPiCnLIRyvwVaqMY3MIw==", + "dev": true, "license": "MIT" }, "node_modules/@types/react": { "version": "18.3.28", "resolved": "https://registry.npmjs.org/@types/react/-/react-18.3.28.tgz", "integrity": "sha512-z9VXpC7MWrhfWipitjNdgCauoMLRdIILQsAEV+ZesIzBq/oUlxk0m3ApZuMFCXdnS4U7KrI+l3WRUEGQ8K1QKw==", + "dev": true, "license": "MIT", "dependencies": { "@types/prop-types": "*", @@ -2907,6 +2895,7 @@ "version": "3.2.3", "resolved": "https://registry.npmjs.org/csstype/-/csstype-3.2.3.tgz", "integrity": "sha512-z1HGKcYy2xA8AGQfwrn0PAy+PB7X/GSj3UVJW9qKyn43xWa+gl5nXmU4qqLMRzWVLFC8KusUX8T/0kCiOYpAIQ==", + "dev": true, "license": "MIT" }, "node_modules/damerau-levenshtein": { @@ -6676,28 +6665,6 @@ } } }, - "node_modules/next-mdx-remote": { - "version": "6.0.0", - "resolved": "https://registry.npmjs.org/next-mdx-remote/-/next-mdx-remote-6.0.0.tgz", - "integrity": "sha512-cJEpEZlgD6xGjB4jL8BnI8FaYdN9BzZM4NwadPe1YQr7pqoWjg9EBCMv3nXBkuHqMRfv2y33SzUsuyNh9LFAQQ==", - "license": "MPL-2.0", - "dependencies": { - "@babel/code-frame": "^7.23.5", - "@mdx-js/mdx": "^3.0.1", - "@mdx-js/react": "^3.0.1", - "unist-util-remove": "^4.0.0", - "unist-util-visit": "^5.1.0", - "vfile": "^6.0.1", - "vfile-matter": "^5.0.0" - }, - "engines": { - "node": ">=14", - "npm": ">=7" - }, - "peerDependencies": { - "react": ">=16" - } - }, "node_modules/node-exports-info": { "version": "1.6.0", "resolved": "https://registry.npmjs.org/node-exports-info/-/node-exports-info-1.6.0.tgz", @@ -8547,21 +8514,6 @@ "url": "https://opencollective.com/unified" } }, - "node_modules/unist-util-remove": { - "version": "4.0.0", - "resolved": "https://registry.npmjs.org/unist-util-remove/-/unist-util-remove-4.0.0.tgz", - "integrity": "sha512-b4gokeGId57UVRX/eVKej5gXqGlc9+trkORhFJpu9raqZkZhU0zm8Doi05+HaiBsMEIJowL+2WtQ5ItjsngPXg==", - "license": "MIT", - "dependencies": { - "@types/unist": "^3.0.0", - "unist-util-is": "^6.0.0", - "unist-util-visit-parents": "^6.0.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/unified" - } - }, "node_modules/unist-util-stringify-position": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/unist-util-stringify-position/-/unist-util-stringify-position-4.0.0.tgz", @@ -8708,20 +8660,6 @@ "url": "https://opencollective.com/unified" } }, - "node_modules/vfile-matter": { - "version": "5.0.1", - "resolved": "https://registry.npmjs.org/vfile-matter/-/vfile-matter-5.0.1.tgz", - "integrity": "sha512-o6roP82AiX0XfkyTHyRCMXgHfltUNlXSEqCIS80f+mbAyiQBE2fxtDVMtseyytGx75sihiJFo/zR6r/4LTs2Cw==", - "license": "MIT", - "dependencies": { - "vfile": "^6.0.0", - "yaml": "^2.0.0" - }, - "funding": { - "type": "opencollective", - "url": "https://opencollective.com/unified" - } - }, "node_modules/vfile-message": { "version": "4.0.3", "resolved": "https://registry.npmjs.org/vfile-message/-/vfile-message-4.0.3.tgz", @@ -8868,21 +8806,6 @@ "dev": true, "license": "ISC" }, - "node_modules/yaml": { - "version": "2.9.0", - "resolved": "https://registry.npmjs.org/yaml/-/yaml-2.9.0.tgz", - "integrity": "sha512-2AvhNX3mb8zd6Zy7INTtSpl1F15HW6Wnqj0srWlkKLcpYl/gMIMJiyuGq2KeI2YFxUPjdlB+3Lc10seMLtL4cA==", - "license": "ISC", - "bin": { - "yaml": "bin.mjs" - }, - "engines": { - "node": ">= 14.6" - }, - "funding": { - "url": "https://github.com/sponsors/eemeli" - } - }, "node_modules/yocto-queue": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/yocto-queue/-/yocto-queue-0.1.0.tgz", diff --git a/package.json b/package.json index 9a89f8e..89a072f 100644 --- a/package.json +++ b/package.json @@ -11,13 +11,13 @@ "capture-gif": "node scripts/capture-gif.mjs" }, "dependencies": { + "@mdx-js/mdx": "^3.1.1", "@vercel/analytics": "^2.0.1", "@vercel/og": "^1.0.0", "@vercel/speed-insights": "^2.0.0", "github-slugger": "^2.0.0", "gray-matter": "^4.0.3", "next": "^16.3.0", - "next-mdx-remote": "^6.0.0", "react": "^18", "react-dom": "^18", "reading-time": "^1.5.0", @@ -29,6 +29,7 @@ "shiki": "^4.2.0" }, "devDependencies": { + "@types/mdx": "^2.0.14", "@types/node": "^20", "@types/react": "^18", "@types/react-dom": "^18", diff --git a/pages/blog/[slug].tsx b/pages/blog/[slug].tsx index df32cac..e9186c1 100644 --- a/pages/blog/[slug].tsx +++ b/pages/blog/[slug].tsx @@ -1,7 +1,8 @@ +import { useMemo } from 'react' +import * as runtime from 'react/jsx-runtime' import Link from 'next/link' import type { GetStaticPaths, GetStaticProps } from 'next' -import { serialize } from 'next-mdx-remote/serialize' -import { MDXRemote, type MDXRemoteSerializeResult } from 'next-mdx-remote' +import { compile, runSync } from '@mdx-js/mdx' import remarkGfm from 'remark-gfm' import rehypePrettyCode from 'rehype-pretty-code' import rehypeSlug from 'rehype-slug' @@ -29,7 +30,7 @@ import { SITE_URL, githubSourceUrl, ogCardUrl } from '@/lib/site' interface PostPageProps { meta: PostMeta - mdxSource: MDXRemoteSerializeResult + mdxCode: string toc: TocItem[] related: PostMeta[] sourceUrl: string @@ -44,7 +45,7 @@ function ogImageFor(meta: PostMeta): string { export default function PostPage({ meta, - mdxSource, + mdxCode, toc, related, sourceUrl, @@ -53,6 +54,7 @@ export default function PostPage({ }: PostPageProps) { const url = `${SITE_URL}/blog/${meta.slug}` const ogImage = ogImageFor(meta) + const Content = useMemo(() => runSync(mdxCode, runtime).default, [mdxCode]) return ( <> @@ -111,7 +113,7 @@ export default function PostPage({
- +
@@ -195,32 +197,31 @@ export const getStaticProps: GetStaticProps = async ({ params }) const post = getPostBySlug(slug) if (!post) return { notFound: true } - const mdxSource = await serialize(post.content, { - mdxOptions: { - remarkPlugins: [remarkGfm], - rehypePlugins: [ - // Lift standalone images out of their wrapping

so our

/
- // image renderer doesn't produce invalid

nesting (a hydration error). - rehypeUnwrapImages, - rehypeSlug, - [ - rehypeAutolinkHeadings, - { - behavior: 'append', - properties: { className: ['heading-anchor'], ariaHidden: true, tabIndex: -1 }, - content: { type: 'text', value: '#' }, - }, - ], - [ - rehypePrettyCode, - { - theme: 'github-dark', - // Let our CSS own the background so code blocks match the deep-space theme. - keepBackground: false, - }, - ], + const mdxCode = await compile(post.content, { + outputFormat: 'function-body', + remarkPlugins: [remarkGfm], + rehypePlugins: [ + // Lift standalone images out of their wrapping

so our

/
+ // image renderer doesn't produce invalid

nesting (a hydration error). + rehypeUnwrapImages, + rehypeSlug, + [ + rehypeAutolinkHeadings, + { + behavior: 'append', + properties: { className: ['heading-anchor'], ariaHidden: true, tabIndex: -1 }, + content: { type: 'text', value: '#' }, + }, ], - }, + [ + rehypePrettyCode, + { + theme: 'github-dark', + // Let our CSS own the background so code blocks match the deep-space theme. + keepBackground: false, + }, + ], + ], }) // Compute prev/next within the visible, date-sorted set (newest first). @@ -234,7 +235,7 @@ export const getStaticProps: GetStaticProps = async ({ params }) return { props: { meta: post.meta, - mdxSource, + mdxCode: String(mdxCode), toc: extractToc(post.content), related: getRelatedPosts(slug, 3), sourceUrl: folder ? githubSourceUrl(folder) : SITE_URL, diff --git a/src/components/mdx/index.tsx b/src/components/mdx/index.tsx index a981d9f..8a62c5d 100644 --- a/src/components/mdx/index.tsx +++ b/src/components/mdx/index.tsx @@ -1,4 +1,4 @@ -import type { MDXRemoteProps } from 'next-mdx-remote' +import type { MDXComponents } from 'mdx/types' import Video from './Video' import YouTube from './YouTube' import Embed from './Embed' @@ -17,7 +17,7 @@ function A({ href = '', children, ...rest }: React.AnchorHTMLAttributes