import createMDX from'@next/mdx'/** @type{import('next').NextConfig} */constnextConfig= {// Configure `pageExtensions` to include markdown and MDX files pageExtensions: ['js','jsx','md','mdx','ts','tsx'],// Optionally, add any other Next.js config below}constwithMDX=createMDX({// Add markdown plugins here, as desired})// Merge MDX config with Next.js configexportdefaultwithMDX(nextConfig)
import { MyComponent } from'my-component'# Welcome to my MDX page!This is some **bold** and _italics_ text.This is a list in markdown:- One- Two- ThreeCheckout my React component:<MyComponent />
importtype { MDXComponents } from'mdx/types'import Image, { ImageProps } from'next/image'// This file allows you to provide custom React components// to be used in MDX files. You can import and use any// React component you want, including inline styles,// components from other libraries, and more.exportfunctionuseMDXComponents(components:MDXComponents):MDXComponents {return {// Allows customizing built-in components, e.g. to add styling.h1: ({ children }) => ( <h1style={{ color:'red', fontSize:'48px' }}>{children}</h1> ),img: (props) => ( <Imagesizes="100vw"style={{ width:'100%', height:'auto' }} {...(props asImageProps)} /> ),...components, }}
exportdefaultfunctionMdxLayout({ children }: { children:React.ReactNode }) {// Create any shared layout or styles herereturn ( <divclassName="prose prose-headings:mt-8 prose-headings:font-semibold prose-headings:text-black prose-h1:text-5xl prose-h2:text-4xl prose-h3:text-3xl prose-h4:text-2xl prose-h5:text-xl prose-h6:text-lg dark:prose-headings:text-white"> {children} </div> )}
然後,將佈局組件匯入到 MDX 頁面中,將 MDX 內容包裝在佈局中,並將其匯出
import MdxLayout from'../components/mdx-layout'# Welcome to my MDX page!exportdefaultfunctionMDXPage({ children }) {return <MdxLayout>{children}</MdxLayout>}