Markdownの手軽さとReactのパワーを両立させます。必要なパッケージをインストールします:
npm install @next/mdx @mdx-js/loader @mdx-js/react gray-matter date-fns
npm install -D @types/mdx
次に next.config.js を設定します:
import createMDX from "@next/mdx";
const nextConfig = {
pageExtensions: ["js", "jsx", "md", "mdx", "ts", "tsx"],
};
const withMDX = createMDX({
options: {
remarkPlugins: [],
rehypePlugins: [],
},
});
export default withMDX(nextConfig);
そして、ルートディレクトリに mdx-components.tsx を作成します:
import type { MDXComponents } from "mdx/types";
export function useMDXComponents(components: MDXComponents): MDXComponents {
return {
h1: ({ children }) => (
<h1 className="text-4xl font-bold mb-4">{children}</h1>
),
h2: ({ children }) => (
<h2 className="text-3xl font-bold mb-3 mt-8">{children}
),
: (
),
...components,
};
}