MDX is Markdown that can render React components. That makes it a good fit for a blog where a post sometimes needs a live example and not just a code sample. This is the setup behind this site, start to finish: reading the files, parsing frontmatter, custom components and typography.
Prerequisites
You need a Next.js project on the App Router. I used Next.js 16.
Step 1: Install Required Dependencies
First, install the necessary packages:
npm install next-mdx-remote
npm install -D @tailwindcss/typography- next-mdx-remote: Allows you to render MDX content in React components
- @tailwindcss/typography: Provides beautiful typographic styles for your blog content
Step 2: Leave next.config as it is
Many MDX guides start by adding md and mdx to pageExtensions. That is only for MDX files that are themselves routes, with @next/mdx. Here the posts are plain files that a page reads at build time, so Next.js never needs to know about them, and there is nothing to configure.
Step 3: Create the Posts Directory Structure
Create a directory to store your blog posts. I recommend placing them in src/content/posts/:
src/
content/
posts/
your-first-post.mdx
your-second-post.mdx
Step 4: Create the Posts Utility Library
Create a utility file src/lib/posts.ts to handle reading and parsing MDX files:
import fs from "fs/promises";
import path from "path";
type Metadata = {
title: string;
publishedAt: string;
updatedAt?: string;
summary: string;
image?: string;
keywords?: string;
};
function parseFrontmatter(fileContent: string) {
let frontmatterRegex = /---\s*([\s\S]*?)\s*---/;
let match = frontmatterRegex.exec(fileContent);
let frontMatterBlock = match![1];
let content = fileContent.replace(frontmatterRegex, "").trim();
let frontMatterLines = frontMatterBlock.trim().split("\n");
let metadata: Partial<Metadata> = {};
frontMatterLines.forEach((line) => {
let [key, ...valueArr] = line.split(": ");
let value = valueArr.join(": ").trim();
value = value.replace(/^['"](.*)['"]$/, "$1"); // Remove quotes
metadata[key.trim() as keyof Metadata] = value;
});
return { metadata: metadata as Metadata, content };
}
async function getMDXFiles(dir: string) {
const files = await fs.readdir(dir);
return files.filter((file) => path.extname(file) === ".mdx");
}
async function readMDXFile(filePath: string) {
const rawContent = await fs.readFile(filePath, "utf-8");
return parseFrontmatter(rawContent);
}
async function getMDXData(dir: string) {
const mdxFiles = await getMDXFiles(dir);
const posts = await Promise.all(
mdxFiles.map(async (file) => {
const { metadata, content } = await readMDXFile(path.join(dir, file));
const slug = path.basename(file, path.extname(file));
return {
metadata,
slug,
content,
};
})
);
return posts;
}
export async function getBlogPosts() {
return getMDXData(path.join(process.cwd(), "src", "content", "posts"));
}
export function formatDate(date: string, includeRelative = false) {
let currentDate = new Date();
if (!date.includes("T")) {
date = `${date}T00:00:00`;
}
let targetDate = new Date(date);
// Count from the actual gap: comparing the calendar fields would call
// 31 December "1y ago" on 1 January.
let daysAgo = Math.floor(
(currentDate.getTime() - targetDate.getTime()) / (1000 * 60 * 60 * 24)
);
let formattedDate = "";
if (daysAgo >= 365) {
formattedDate = `${Math.floor(daysAgo / 365)}y ago`;
} else if (daysAgo >= 30) {
formattedDate = `${Math.floor(daysAgo / 30)}mo ago`;
} else if (daysAgo > 0) {
formattedDate = `${daysAgo}d ago`;
} else {
formattedDate = "Today";
}
let fullDate = targetDate.toLocaleString("en-us", {
month: "long",
day: "numeric",
year: "numeric",
});
if (!includeRelative) {
return fullDate;
}
return `${fullDate} (${formattedDate})`;
}This utility:
- Reads all
.mdxfiles from the posts directory - Parses frontmatter metadata (title, publishedAt, summary, image, keywords)
- Extracts the slug from the filename
- Provides a date formatting function
Step 5: Create Custom MDX Components
Create src/components/mdx/mdx.tsx with custom components for rendering MDX:
import Link from "next/link";
import Image, { type ImageProps } from "next/image";
import { MDXRemote, type MDXRemoteProps } from "next-mdx-remote/rsc";
import React from "react";
type TableData = {
headers: string[];
rows: string[][];
};
function Table({ data }: { data: TableData }) {
let headers = data.headers.map((header: string, index: number) => (
<th key={index}>{header}</th>
));
let rows = data.rows.map((row: string[], index: number) => (
<tr key={index}>
{row.map((cell: string, cellIndex: number) => (
<td key={cellIndex}>{cell}</td>
))}
</tr>
));
return (
<table>
<thead>
<tr>{headers}</tr>
</thead>
<tbody>{rows}</tbody>
</table>
);
}
function CustomLink(props: React.AnchorHTMLAttributes<HTMLAnchorElement>) {
let href = props.href;
if (!href) {
return <a {...props} />;
}
if (href.startsWith("/")) {
return (
<Link href={href} {...props}>
{props.children}
</Link>
);
}
if (href.startsWith("#")) {
return <a {...props} />;
}
return <a target="_blank" rel="noopener noreferrer" {...props} />;
}
function RoundedImage(props: ImageProps) {
const { alt, className, ...rest } = props;
return (
<Image
alt={alt || ""}
className={`rounded-lg ${className || ""}`}
{...rest}
/>
);
}
// A heading's plain text, even when it holds inline code or emphasis.
// (String(children) would give "[object Object]" for those.)
function textOf(node: React.ReactNode): string {
if (typeof node === "string" || typeof node === "number") return String(node);
if (Array.isArray(node)) return node.map(textOf).join("");
if (React.isValidElement<{ children?: React.ReactNode }>(node)) {
return textOf(node.props.children);
}
return "";
}
function slugify(str: string): string {
return str
.toLowerCase()
.trim()
.replace(/\s+/g, "-")
.replace(/&/g, "-and-")
.replace(/[^\w\-]+/g, "")
.replace(/\-\-+/g, "-");
}
function createHeading(level: number) {
const Heading = ({ children }: { children: React.ReactNode }) => {
let slug = slugify(textOf(children));
return React.createElement(
`h${level}`,
{ id: slug },
[
React.createElement("a", {
href: `#${slug}`,
key: `link-${slug}`,
className: "anchor",
}),
],
children
);
};
Heading.displayName = `Heading${level}`;
return Heading;
}
let components = {
h1: createHeading(1),
h2: createHeading(2),
h3: createHeading(3),
h4: createHeading(4),
h5: createHeading(5),
h6: createHeading(6),
Image: RoundedImage,
a: CustomLink,
Table,
};
export function CustomMDX(props: MDXRemoteProps) {
return (
<MDXRemote
{...props}
components={{ ...components, ...(props.components || {}) }}
// The posts are my own files, so let their JSX expressions through (see below).
options={{ blockJS: false, ...props.options }}
/>
);
}This component provides:
- Custom links that handle internal Next.js routes, external links, and anchors
- Image components with rounded corners
- Heading anchors for easy linking to sections
- Table support for structured data
Note: Typography styling is handled by @tailwindcss/typography (see Step 10).
A gotcha I hit later.
next-mdx-remote6 strips JavaScript expressions out of MDX by default. Text props such astitle="Hello"still work, but a prop likecode={...}orsteps={[...]}arrives empty, with no error. That is whatblockJS: falseabove turns off. Only do that for MDX you wrote yourself, never for content from users.
Step 6: Create the Blog Listing Page
Create src/app/blog/page.tsx:
import { BlogPosts } from "@/components/blog/posts";
export const metadata = {
title: "Blog",
description: "Read my blog.",
};
export default function Page() {
return (
<section className="max-w-4xl mx-auto px-6 py-16">
<h1 className="text-2xl font-semibold mb-12 tracking-tighter">Blog</h1>
<BlogPosts />
</section>
);
}Step 7: Create the Blog Posts Component
Create src/components/blog/posts.tsx:
import { formatDate, getBlogPosts } from "@/lib/posts";
import Link from "next/link";
export async function BlogPosts() {
// Files come back in name order; show the newest first.
let allBlogs = (await getBlogPosts()).sort(
(a, b) =>
new Date(b.metadata.publishedAt).getTime() -
new Date(a.metadata.publishedAt).getTime()
);
return (
<div>
{allBlogs.map((post) => (
<Link
key={post.slug}
className="flex flex-col space-y-1 mb-4"
href={`/blog/${post.slug}`}
>
<div className="w-full flex flex-col md:flex-row space-x-0 md:space-x-2">
<p className="text-neutral-600 dark:text-neutral-400 w-[100px] tabular-nums">
{formatDate(post.metadata.publishedAt, false)}
</p>
<p className="text-neutral-900 dark:text-neutral-100 tracking-tight">
{post.metadata.title}
</p>
</div>
</Link>
))}
</div>
);
}Step 8: Create the Dynamic Blog Post Page
Create src/app/blog/[slug]/page.tsx:
import { notFound } from "next/navigation";
import { CustomMDX } from "@/components/mdx/mdx";
import { formatDate, getBlogPosts } from "@/lib/posts";
export async function generateStaticParams() {
const posts = await getBlogPosts();
return posts.map((post) => ({
slug: post.slug,
}));
}
export default async function Page({
params,
}: {
params: Promise<{ slug: string }>;
}) {
const slug = (await params).slug;
const posts = await getBlogPosts();
const post = posts.find((post) => post.slug === slug);
// A real 404, so a mistyped address isn't served (and indexed) as a page.
if (!post) notFound();
return (
<section className="max-w-4xl mx-auto px-6 py-16">
<h1 className="text-2xl font-semibold tracking-tighter mb-4">
{post.metadata.title}
</h1>
<div className="flex justify-between items-center mb-8 text-sm">
<p className="text-sm text-muted-foreground">
{formatDate(post.metadata.publishedAt)}
</p>
</div>
<article className="prose">
<CustomMDX source={post.content} />
</article>
</section>
);
}This page:
- Generates static paths for all blog posts at build time
- Renders the MDX content using our custom component
- Returns a 404 for a slug that doesn't exist
Step 9: Create Your First Blog Post
Create a new .mdx file in src/content/posts/:
---
title: "My First Blog Post"
publishedAt: "2024-12-19"
summary: "This is a summary of my first blog post."
keywords: "mdx, nextjs, blog, tutorial"
---
Welcome to my blog! This is written in MDX.
## Code Example
Here's a code example:
```tsx
const MyComponent = () => {
return <div>Hello, world!</div>;
};
```
## Features
- **Markdown** syntax for easy writing
- **JSX components** for interactive content
- **Beautiful typography** with Tailwind TypographyThe page already renders the title from the frontmatter as the <h1>, so the post itself starts at ##.
Step 10: Configure Tailwind Typography
The typography plugin has to be switched on in your Tailwind CSS setup.
For Tailwind CSS v4
If you're using Tailwind CSS v4, add the typography plugin to your globals.css:
@import "tailwindcss";
@plugin "@tailwindcss/typography";For Tailwind CSS v3
If you're using Tailwind CSS v3, add the plugin to your tailwind.config.js:
module.exports = {
// Include the posts, or classes used only in MDX get purged.
content: ["./src/**/*.{js,ts,jsx,tsx,mdx}"],
theme: {
extend: {},
},
plugins: [require("@tailwindcss/typography")],
};The prose class on the article element (Step 8) then styles everything Markdown produces: headings, lists, quotes, tables and code blocks.
Conclusion
That is the whole blog: MDX files with frontmatter, a small library that reads them, custom components, Tailwind Typography, and pages generated at build time.
A new post is one new .mdx file in src/content/posts/.
Two follow-ups build on this: SEO for the blog and better code blocks.
Want to see the source code? Check out the GitHub repository for this blog.