Creating a Markdown blog with Notion, Tailwind & Next.js
Last week Notion announced that they are opening up their API to the public, after being in closed beta for a while. For me that was great news, since I’m a big Notion fan and I was looking for a way to easily write my blogs in Markdown in a central place.
So the backend was decided! For the frontend I went with my usual stack: Next.js and Tailwind.
I started out by creating an integration, and then sharing my database with this integration. This is explained in detail here.
Once this part is set up, we can start querying our database in Notion!
There are 3 different API routes I used to create my blog:
- Query the database: https://developers.notion.com/reference/post-database-query
- Retrieving a page: https://developers.notion.com/reference/get-page
- Retrieving the blocks and their children: https://developers.notion.com/reference/get-block-children
In my pages/index.jsx
I query the database to get back the pages in my database.
So now we have passed the blogs to the props of the home page. In the functional component I render the blogs, wrapped in a Link for internal routing:
Now we have the blog previews being shown on the homepage, we can now work on the actual blog page.
As you can see in the href
of the Link, we will use /blog/[id]
as the URL.
So in the /pages
folder we create a folder /blog
and create a file [id].jsx
in there.
On the blog page, we need to fetch the pages again to generate our URL’s, fetch the actual page and fetch the blocks out of which the page consists.
Now we have the blocks and page available in our component, we can render them to our page! I’m going to focus on the blocks, because the page is just used for the title. All the content comes from the blocks:
Using the classes provided by Tailwind, we can easily map the Markdown to a fully styled page.
You can check the demo at https://notion-blog-ruby-kappa.vercel.app. Source code can be found on https://github.com/thomasledoux1/notion-blog. Some of the code was inspired by https://github.com/samuelkraft/notion-blog-nextjs, so shoutout to Samuel too.
Thanks for reading, I hope you learned something new today!