Build a Simple Next.js Interactive Markdown Editor

Written by

in

In the world of web development, creating a user-friendly and efficient text editor is a common yet challenging task. Markdown, a lightweight markup language, has become increasingly popular for its simplicity and readability. Imagine building a web application where users can effortlessly write and preview Markdown content in real-time. This article serves as a comprehensive guide for beginners to intermediate developers, showing you how to build an interactive Markdown editor using Next.js, a powerful React framework.

Why Build a Markdown Editor?

Markdown editors are incredibly versatile. They find applications in various contexts, including:

  • Note-taking: Quickly jot down ideas and format them easily.
  • Blogging: Write blog posts with simple syntax, focusing on content.
  • Documentation: Create clear and well-structured documentation.
  • Collaborative writing: Enable teams to work together on documents.

Moreover, building a Markdown editor is an excellent learning project. It allows you to:

  • Learn about Next.js and its features.
  • Understand how to handle user input.
  • Work with libraries for Markdown parsing and rendering.
  • Gain experience in building interactive web components.

Prerequisites

Before we dive in, ensure you have the following prerequisites:

  • Node.js and npm: Make sure you have Node.js and npm (Node Package Manager) installed on your system.
  • Basic JavaScript knowledge: Familiarity with JavaScript, HTML, and CSS is essential.
  • Text editor/IDE: Choose your preferred code editor (e.g., VS Code, Sublime Text, Atom).

Setting Up Your Next.js Project

Let’s start by creating a new Next.js project. Open your terminal and run the following command:

npx create-next-app markdown-editor
cd markdown-editor

This command creates a new Next.js project named “markdown-editor” and navigates you into the project directory.

Installing Dependencies

We’ll need a couple of libraries to help us with Markdown parsing and rendering. Install the following packages using npm:

npm install react-markdown remark-html
  • react-markdown: This library allows us to render Markdown content in React.
  • remark-html: This library converts Markdown into HTML.

Project Structure

Your project structure should look like this:

markdown-editor/
├── node_modules/
├── pages/
│   └── index.js
├── public/
├── .gitignore
├── next.config.js
├── package-lock.json
├── package.json
└── README.md

Building the Markdown Editor Component

The core of our application will be a component that handles user input and renders the Markdown preview. Open `pages/index.js` and replace its content with the following code:

import { useState } from 'react';
import ReactMarkdown from 'react-markdown';

export default function Home() {
  const [markdown, setMarkdown] = useState('');

  return (
    <div className="container">
      <div className="input-container">
        <textarea
          className="input"
          value={markdown}
          onChange={(e) => setMarkdown(e.target.value)}
          placeholder="Enter Markdown here..."
        />
      </div>
      <div className="preview-container">
        <ReactMarkdown className="preview">
          {markdown}
        </ReactMarkdown>
      </div>
      <style jsx global>{
        `
          .container {
            display: flex;
            flex-direction: column;
            align-items: center;
            padding: 20px;
            font-family: sans-serif;
          }
          .input-container {
            width: 80%;
            margin-bottom: 20px;
          }
          .input {
            width: 100%;
            height: 300px;
            padding: 10px;
            font-size: 16px;
            border: 1px solid #ccc;
            border-radius: 4px;
            resize: vertical;
          }
          .preview-container {
            width: 80%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 4px;
            background-color: #f9f9f9;
            overflow: auto;
          }
          .preview {
            padding: 10px;
          }
        `
      }</style>
    </div>
  );
}

Let’s break down this code:

  • Import statements: We import `useState` from React and `ReactMarkdown` from `react-markdown`.
  • State management: We use the `useState` hook to manage the `markdown` state, which holds the Markdown text entered by the user.
  • Textarea: A `