Netlify CMS & Next.js Series - Adding markdown content

Senior Full-Stack Developer with over 20 years of experience building web applications for startups, government, and higher education.
Search for a command to run...

Senior Full-Stack Developer with over 20 years of experience building web applications for startups, government, and higher education.
No comments yet. Be the first to comment.
This will be a series of posts were I setup a simple brochure site in Next.js driven by the Netlify CMS.
The default method of integrating Netlify CMS with Next.js is not super flexible so lets use an alternative method... We want custom previews so lets use netlify-cms-app. NOTE: If you're not interested in custom previews take a look at the standard N...
Writing this up so I have somewhere to quickly grab basic Jest setup. This information is copied from the Jest Documentation Site compressed into just the parts I'm interested in. Create folders mkdir -p <project>/src cd <project> Install and init n...

In this blog post, we'll explore the process of setting up and configuring a Postgres database using Docker and seeding it with test data. This approach can be used for a variety of apps and can be easily adapted for your own needs. We'll cover all t...

Continuing from last time, we will explore the concepts behind Create and Update functionality and provide a step-by-step guide on how to implement it in your SvelteKit application. Let's get started! Server & Client Code In SvelteKit, the server-sid...

Introduction In this post, I will explore how to create a REST API using Spring Boot and Kotlin. We will dive into the fundamentals of Spring Boot and Kotlin, explain how to set up the project, and guide you through the process of building the API st...

Introduction Welcome to the latest post in my series of Koan Battles using Kotlin and TypeScript. In this series, we'll complete a Koan in both languages and then discuss the differences between the two solutions. My goal is to help you gain a deeper...

We're going to be using markdown to create our pages so lets add a markdown loader:
yarn add frontmatter-markdown-loader -D
Create a content folder and some sample content:
filename: content/index.md
---
header:
title: Software development for modern businesses - devfair
description: devfair is your full stack software development experience
logo: /images/devfair-logo.svg
hero:
detail: Work with professional software developers and scrum masters to make your
project a reality. Minimise risk, cost and time compared to traditional
development. Have complete control and insight into your development process, just
like the best tech companies in the world.
heading: Build software like the world's top tech companies do
image: /images/hero.svg
---
Next we need to tell next.js how to handle markdown files and where to find them. We do this by creating a next.config.js file and adding the following code:
filename: next.config.js
module.exports = {
webpack: (cfg) => {
cfg.module.rules.push(
{
test: /\.md$/,
loader: 'frontmatter-markdown-loader',
options: { mode: ['react-component'] }
}
)
return cfg;
}
}
We can now import the markdown content into the index.tsx file:
filename: index.tsx
import Head from "next/head";
import { attributes } from "../content/index.md";
export default function Index() {
const { header, hero } = attributes;
return (
<div className="p-4">
<Head>
<title>{header.title}</title>
<meta name="description" content={header.description} />
<link rel="icon" href="/favicon.ico" />
</Head>
<main>
<h1 className="text-6xl">{hero.heading}</h1>
<p className="mt-0 mb-4 text-gray-600">{hero.detail}</p>
</main>
</div>
);
}
NOTE at this point you may see a typescript error around the markdown input:
TS2307: Cannot find module '../content/index.md' or its corresponding type declarations.
To fix this error you need to add a declaration to the next-env.d.ts:
filename: next-env.d.ts
/// <reference types="next" />
/// <reference types="next/types/global" />
declare module "*.md";
You should now see the markdown from content/index.md rendered on the index page:
