Notes from configuring MDX on with-heart.xyz
Adds support for .mdx
files. Stores mdx
-specific configuration in
mdx.config.mjs
.
pnpm i -D @next/mdx @mdx-js/loader @mdx-js/react
mdx.config.mjs
:ts
import withMdx from '@next/mdx'export default withMdx({extension: /\.mdx?$/,})
next.config.js
-> next.config.mjs
next.config.mjs
:ts
import withMdx from './mdx.config.mjs'/** @type {import('next').NextConfig} */const nextConfig = {// default extensions + md + mdxpageExtensions: ['ts', 'tsx', 'js', 'jsx', 'md', 'mdx'],reactStrictMode: true,swcMinify: true,}export default withMdx(nextConfig)
We can export frontmatter as variables that can be accessed within mdx
.
⬇️md
---title: 'Some Title'---# {title}
html
<h1>Some Title</h1>
We'll use remark-frontmatter
and
remark-mdx-frontmatter
to accomplish this.
pnpm i -D remark-frontmatter remark-mdx-frontmatter
mdx.config.mjs
:js
import withMdx from '@next/mdx'import remarkFrontmatter from 'remark-frontmatter'import remarkMdxFrontmatter from 'remark-mdx-frontmatter'export default withMdx({extension: \/.mdx?$/,options: {remarkPlugins: [[remarkFrontmatter],[remarkMdxFrontmatter]]}})
shiki-twoslash
and
remark-shiki-twoslash
are made specifically for this purpose. Code blocks in ts twoslash
fences work
much like the
TypeScript Playground editor.
pnpm i -D @mdx-js/mdx rehype-raw remark-shiki-twoslash
mdx.config.mjs
:js
import {nodeTypes} from '@mdx-js/mdx'import withMdx from '@next/mdx'import reypeRaw from 'rehype-raw'import remarkShikiTwoslash from 'remark-shiki-twoslash'/** @type {import('remark-shiki-twoslash').Options} */const remarkShikiTwoslashOptions = {}export default withMdx({extension: /\.mdx?$/,options: {remarkPlugins: [[remarkShikiTwoslash.default, remarkShikiTwoslashOptions]],rehypePlugins: [// https://github.com/mdx-js/mdx/issues/1820#issuecomment-970430877[rehypeRaw, {passThrough: nodeTypes}],],},})
styles/shiki.css
styles/shiki.css
import '../styles/shiki.css'
to _app.tsx