ProjectsNext.jsMDXMetaSSG

How This Blog Works: Building a Markdown Engine with Next.js

3.23 min read
Md Nasim SheikhMd Nasim Sheikh
Share:

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

  1. Read Files: Use Node's fs module to find all .mdx files.
  2. Parse Frontmatter:
    const fileContents = fs.readFileSync(fullPath, 'utf8');
    const { data, content } = matter(fileContents);
    // data = { title: "My Post", date... }
    
  3. Compile MDX: We use next-mdx-remote to 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.

Test Your Skills
Advanced Level

Next.js Framework Quiz

Test your knowledge and reinforce what you've learned in this article.

20/100
Questions
25 min
Duration
Start Quiz
Md Nasim Sheikh
Written by

Md Nasim Sheikh

Software Developer at softexForge

Verified Author150+ Projects
Published:

You May Also Like