ProjectsNext.jsMDXMetaSSG
How This Blog Works: Building a Markdown Engine with Next.js
3.05 min read
Md Nasim Sheikh
Why use WordPress when you can just Git commit a .md file? This blog itself is built on Next.js and MDX.
Advertisement
The Stack
- Next.js: Framework (React).
- MDX: Markdown + JSX. Allows interactive components (like the Quiz below!) inside articles.
- Gray Matter: Parses YAML frontmatter (title, date, tags).
- Tailwind CSS: Styling.
The Content Pipeline
- Read Files: Use Node's
fsmodule to find all.mdxfiles. - Parse Frontmatter:
const fileContents = fs.readFileSync(fullPath, 'utf8'); const { data, content } = matter(fileContents); // data = { title: "My Post", date... } - Compile MDX: We use
next-mdx-remoteto serialize the markdown content into a renderable React tree.
Static Site Generation (SSG)
At build time (npm run build), Next.js calls getStaticProps. It reads every file and generates an HTML page for it. This is why the blog is so fast—there is no database query when you visit the page. It's just HTML.
The Component Replacement
The power of MDX is substituting HTML tags with Components.
const components = {
img: (props) => <Image {...props} layout="responsive" />,
pre: Pre, // Custom code block with copy button
Quiz, // We inject the Quiz component so we can use it!
};
Advertisement
Quiz
Quick Quiz
What is the main performance benefit of Static Site Generation (SSG) for a blog?
Conclusion
Building your own blog engine is a rite of passage. It gives you 100% control over SEO and performance, something Medium or Substack can never match.
Written by
Md Nasim Sheikh
Software Developer at softexForge
Verified Author150+ Projects
Published: