March 26, 2023

Blog @ Munaf Sheikh

Latest news from tech-feeds around the world.

Creating a Markdown Blog Powered by Next.js in Under an Hour

I’m pretty sure 99% of the developers out there just love markdown. It’s such a fantastic format to write in.

For those not familiar with markdown, it’s a flat text format that looks like this:

1# Heading


3Some text


5- list item 1

6- list item 2

7- list item 3


9[My website](

Which would result in the following HTML

1<h1><a id="Heading_0"></a>Heading</h1>


3<p>Some text</p>



6 <li>list item 1</li>

7 <li>list item 2</li>

8 <li>list item 3</li>



11<a href="">My website</a>

Sounds lovely, right? And yes, it is lovely, so how cool would it be if we could make a blog in Next.js that allows us to write markdown formatted posts?

If you are wondering what the result is for this article, here is a little demo video:

Note: At the bottom, I linked the GitHub repo that you can fork 🥳

Project setup

If you are new to markdown or Next.js, don’t worry. This article will guide you completely through setting this project up from start to finish!

Let’s start by creating a new blank Next.js project. We’ll open up our favorite terminal and run the following command.

1npx create-next-app next-blog

This command will create a blank next application in the next-blog folder.

Once it’s done installing, you can navigate to the project and run it. (This will show the basic Next.js starter)

1cd next-blog



4npm run dev

Adding Tailwind CSS for our styling

Tailwind CSS is a perfect CSS framework to make styling super easy for us, and it works super well in a Next.js project.

The first step we have to do is, install the dependencies that are required for Tailwind.

1npm install -D tailwindcss@latest postcss@latest autoprefixer@latest

Then we can initialize Tailwind by running the following command. It will create all the side files we need.

1npx tailwindcss init -p

Now head over to the tailwind.config.js file that the command above created for us.
Modify the content: array to look like this:

1content: [

2 './pages/**/*.{js,ts,jsx,tsx}',

3 './components/**/*.{js,ts,jsx,tsx}',


The last step to making everything work is modifying the existing styles/global.scss file.
Remove everything in this file and only add the following three lines.

1@import 'tailwindcss/base';

2@import 'tailwindcss/components';

3@import 'tailwindcss/utilities';

And that’s it. We can now leverage the power of Tailwind in our application.

Creating a reusable Next.js layout

Next.js supports different pages out of the box, but we want to have one main layout that wraps around this.

We will define a header and footer in this layout, which means we don’t need to add them in all the separate files.

To create a layout in Next.js, create a components folder in your project.

Inside that folder, create a layout.js file.

The layout is a component, which looks like this:

1import Link from 'next/link';


3export default function Layout({ children }) {

4 return (

5 <div className='flex flex-col min-h-screen'>

6 <header className='bg-fuchsia-100 mb-8 py-4'>

7 <div className='container mx-auto flex justify-center'>

8 <Link href='/'>

9 <a>🏡</a>

10 </Link>

11 <span className='mx-auto'>Welcome to my blog</span>{' '}

12 </div>

13 </header>

14 <main className='container mx-auto flex-1'>{children}</main>

15 <footer className='bg-fuchsia-100 mt-8 py-4'>

16 <div className='container mx-auto flex justify-center'>

17 &copy; 2022 DailyDevTips

18 </div>

19 </footer>

20 </div>

21 );


For this article, I decided not to extract any further, but you could go ahead and move the header and footer into their components for better readability.

The critical part here is that we accept children as property and render them in the main element.

This allows us to render any component inside there.

We have to open up our _app.js file to use this newly created layout. This file is the entry point for your entire application.

By default, you will see it renders a Component with specific properties.

We want to wrap that component in our layout.

1import Layout from '../components/layout';

2import '../styles/globals.css';


4function MyApp({ Component, pageProps }) {

5 return (

6 <Layout>

7 <Component {...pageProps} />

8 </Layout>

9 );



12export default MyApp;

And that’s it. Any page we create now will be wrapped in our layout component, making it show the header and footer.

Let’s try this quickly before moving on.

Modify the index.js file and add the following markup for now:

1return (<div>Hello world 👋</div>);

When you rerun the application you should see the following:

Basic Next.js layout

You see, the header and footer are automatically applied. We didn’t need to say they should be used in any way.

Creating the posts

Since we’ll be using markdown for our posts, create a new folder at the root of your project called posts.

Inside, you can start by creating your post items. Each file will also become the URL of the article, so keep this in mind.

One example of my articles looks like this:

Then inside the file, you have two sections.

The top part, called frontmatter, it’s a way to add non-rendered elements to your post.

They are divided by three dashes.
And below that is all the content that you want.

For example:


2title: 'Next.js page options and how they work'

3metaTitle: 'Next.js page options and how they work'

4metaDesc: 'How to use pages in Next.js exploring the options'

5socialImage: images/22-09-2021.jpg

6date: '2021-09-22'


8 - nextjs



11# The main content

You can add anything you want in the frontmatter, so you don’t have to use this example specifically.

We’ll only be using the following for this article:

It’s up to you to enhance the website with other cool elements.

Note: Add the images in the public/images directory.

Create a couple of markdown posts (or use the ones from the demo). This makes it easier to test what we are building.

Loading the markdown posts on the homepage

Now that we have our markdown posts in place, we want to retrieve them and show them on the homepage.

We want to show the image and the title and link to the actual post.

Since the image and title are defined in the markdown file’s frontmatter section, we need to find a way to extract them.

Luckily for us, there is a fantastic NPM package that can help us with this. Let’s install it.

1npm install gray-matter

This package allows us to parse the frontmatter section and the content section from a content string.

Then we want to open up our index.js file and start importing the packages we will need.

1import fs from 'fs';

2import matter from 'gray-matter';

3import Image from 'next/image';

4import Link from 'next/link';

Then we need to leverage the Next.js getStaticProps method, which allows us to define variables the page can use on build time.

Note: Check out this article for more information on the rendering modes of Next.js

The main frame for this function looks like this:

1export async function getStaticProps() {



We need to work on retrieving all the posts from our posts folder.

We can leverage the fs (filesystem) module.

1const files = fs.readdirSync('posts');

Then we want to loop over all those posts to extract the frontmatter part to get the title and image.

1const posts = => {

2 const slug = fileName.replace('.md', '');

3 const readFile = fs.readFileSync(`posts/${fileName}`, 'utf-8');

4 const { data: frontmatter } = matter(readFile);


6 return {

7 slug,

8 frontmatter,

9 };


There is quite a lot going on here, so let’s look step by step.

First, we define the slug (URL) for the page, which is the filename without the .md part.

Then we read the file by using the fs module again.
And once it’s loaded, we use the matter package to read the file and extract the data object, but we destructure it as the variable frontmatter.

Basically, we change the data variable to frontmatter.

The last thing we need to do is return the props that our component will receive.

1return {

2 props: {

3 posts,

4 },


This sends the posts variable to our component.

So let’s modify our Home component to receive this variable.

1export default function Home({ posts }) {

That’s it! Yes, super simple, right? We now have access to the posts inside the component.

Let’s create a grid and render our posts in there.

1export default function Home({ posts }) {

2 return (

3 <div className='grid grid-cols-1 md:grid-cols-3 lg:grid-cols-4 p-4 md:p-0'>

4 {{ slug, frontmatter }) => (

5 <div

6 key={slug}

7 className='border border-gray-200 m-2 rounded-xl shadow-lg overflow-hidden flex flex-col'

8 >

9 <Link href={`/post/${slug}`}>

10 <a>

11 <Image

12 width={650}

13 height={340}

14 alt={frontmatter.title}

15 src={`/${frontmatter.socialImage}`}

16 />

17 <h1 className='p-4'>{frontmatter.title}</h1>

18 </a>

19 </Link>

20 </div>

21 ))}

22 </div>

23 );


The CSS part is rendering a grid, where we show one post on mobile and up to four for big screens.

Then we loop over each of the posts using the map method.

Each post will be one big link, including the image of the posts and the title.
And on click, it links to /post/{slug}, which we’ll start working on next.

Let’s quickly look and admire what we have made so far:

Next.js reading markdown files

Creating individual markdown posts in Next.js

Now that we have a list of our posts on the homepage, we want to show more details and the actual content on each page.

We can leverage Next.js dynamic routing, allowing one file to render all our posts!

To create this file, create a post directory inside the pages directory and inside this folder, add a file called [slug].js.

The brackets define the file as a dynamic file.

This file will also use the getStaticProps to retrieve the data for a single post.

But it needs another function called getStaticPaths to create each path (URL).

Let’s start by importing the modules we need.

1import fs from 'fs';

2import matter from 'gray-matter';

And from here, we can work on the getStaticPaths function to ensure all our files get their URL.

1export async function getStaticPaths() {



Again, the first thing we need to do is get all our posts.

1const files = fs.readdirSync('posts');

And you guessed it right, map the filenames to get the slugs.

1const paths = => ({

2 params: {

3 slug: fileName.replace('.md', ''),

4 },


Then the critical part here is this function should return all valid paths, so we simply need to return them.

I also included a fallback: false, which ensures non-existing URLs will fail and show a 404.

1return {

2 paths,

3 fallback: false,


The static part is much like what we did on the homepage.
But for this one, we also want to retrieve the data from the file.
This is because the file contains all our actual content.

1export async function getStaticProps({ params: { slug } }) {

2 const fileName = fs.readFileSync(`posts/${slug}.md`, 'utf-8');

3 const { data: frontmatter, content } = matter(fileName);

4 return {

5 props: {

6 frontmatter,

7 content,

8 },

9 };


Before we start working on the component for this page, we want to install another NPM package called markdown-it.
This package can convert markdown into HTML.

1npm install markdown-it

Then load it on this page:

1import md from 'markdown-it';

Now we can start working on the actual component, which is super simple as it only renders the title and the content.

1export default function PostPage({ frontmatter, content }) {

2 return (

3 <div className='prose mx-auto'>

4 <h1>{frontmatter.title}</h1>

5 <div dangerouslySetInnerHTML={{ __html: md().render(content) }} />

6 </div>

7 );


As you can see, we need to use dangerouslySetInnerHTML as we are setting HTML.
Don’t be scared by the name, as it’s only HTML we allow to be in there.

Almost there!

Let’s quickly try it out and see what we got.
Run your app and open a page.

Markdown in Next.js post

Cool, all our data is there, but it doesn’t look great yet.

Luckily we decided to use Tailwind CSS, and they have a fantastic plugin: Typography plugin

Let’s install this plugin and see what happens.

1npm install -D @tailwindcss/typography

Open the tailwind.config.js file and add it under the plugins section.

1plugins: [require('@tailwindcss/typography')],

Now reload your app and see what happens.

Tailwind Typography rendering markdown

Open Source Session Replay

Debugging a web application in production may be challenging and time-consuming. OpenReplay is an Open-source alternative to FullStory, LogRocket and Hotjar. It allows you to monitor and replay everything your users do and shows how your app behaves for every issue.
It’s like having your browser’s inspector open while looking over your user’s shoulder.
OpenReplay is the only open-source alternative currently available.


Happy debugging, for modern frontend teams – Start monitoring your web app for free.


And that’s it. We now have a super cool markdown-powered Next.js blog!

This is only a basic starter, but enough to get your blogging career started.

Not bad for an hour worth of work!

Not it’s up to you to make this blog even more unique and share with the world what you created.

Some ideas to make it more excellent:

  • SEO aspect, add meta titles/description
  • Add a tag overview
  • Add some coolers to the code blocks
  • Refactor elements into components

As promised, you can download and use this starter template for free.

Download the Next.js Markdown blog start from GitHub

Source link