This script automates the process of setting up a mdx based blog section in a new Next.js site. It installs necessary packages, configures the project, and creates sample blog posts.
The whole process takes something around 10-20 seconds(mostly waiting for npm packages to install) and you are ready to go.
Anytime I have to setup a markdown based blog in next.js, it always results in lot of plubming work.
And whenever I do, it mostly results in the following steps:
Need to for automation was neigh.
Anything more defeats the purpose, also a simple bash script is easy to understand and modify by anyone as per their needs.
Node.js and npm installed
A Next.js project set up
You should have installed next.js like this, basically going with the defaults:
npx create-next-app@latest mynextjs-site --use-npm ok
✔ Would you like to use TypeScript? … No
✔ Would you like to use ESLint? … Yes
✔ Would you like to use Tailwind CSS? … Yes
✔ Would you like to use `src/` directory? … No
✔ Would you like to use App Router? (recommended) … Yes
✔ Would you like to customize the default import alias (@/*)? … No
If you have a different setup, you can always modify the script to suit your needs. It will be a simple process of changing may be js vs ts extension on files or changing the file paths.
create-blog.sh
script in the root of your Next.js project.chmod +x create-blog.sh
./create-blog.sh
npm run dev
http://localhost:3000/blog
to see the sample blog posts.Create a new .mdx
file in the content
directory.
Add the following front matter to the file and write your blog post content:
---
title: "My New Blog Post"
description: "This is a sample blog post."
date: "2024-07-07"
category: "Personal"
---
# Sample Blog Post
Why did chicken cross the road?. I finally found the answer.
That's it! Your new blog post will be automatically added to the blog listing page. Please note slug is generated from the file name. So, saving the file as my-new-blog-post.mdx
will generate a slug of my-new-blog-post
.
Install Necessary npm Packages
@next/mdx
, @mdx-js/loader
, @mdx-js/react
, @types/mdx
, @tailwindcss/typography
, gray-matter
, and next-mdx-remote
.Update tailwind.config.js
plugins: []
with plugins: [require("@tailwindcss/typography")]
.Rename next.config.js
to next.config.mjs
Create Content Directory and Sample Blog Posts
content
directory and adds sample .mdx
blog posts.Create Components Directory and mdx-layout.jsx
File
components
directory and adds an MDX layout component.Create Blog Pages
pages/blog
directory and adds index.js
for listing blog posts.pages/blog/[slug]
directory and adds index.js
for individual blog posts.Modify Global CSS
app/globals.css
.Modify app/page.js
next/link
if it doesn't exist.<p>
element with a <div>
element containing a link to the blog.Once you run the script and run the development server, you will see a blog link in the top left navigation bar.
On clicking the blog link, you will see the sample blog posts.
Sample Blog Post
I do believe there are/can be different ways to achieve the same thing in a more efficient manner. That's the thing with code. But this works for me and I hope it works for you too.