Build a Simple Vue.js Interactive Web-Based Markdown Previewer: A Beginner’s Guide

In the world of web development, understanding and implementing Markdown is a valuable skill. Markdown is a lightweight markup language with plain text formatting syntax. It allows you to format text in a simple and readable way, which is then converted to HTML. This makes it ideal for writing documentation, creating blog posts, and more. This tutorial will guide you through building a simple, interactive Markdown previewer using Vue.js. This project will not only teach you the basics of Vue.js but also introduce you to the process of converting Markdown to HTML.

Why Build a Markdown Previewer?

A Markdown previewer is incredibly useful for several reasons:

  • Real-time Preview: It allows you to see how your Markdown text will look as HTML in real-time. This instant feedback loop is very helpful for refining your formatting.
  • Learning Tool: It’s a great way to learn Markdown syntax by experimenting and seeing the results immediately.
  • Practical Application: Markdown is used everywhere, from GitHub README files to documentation platforms. Understanding how to work with it is a practical skill.

This tutorial will cover the following:

  • Setting up a Vue.js project.
  • Creating a text input area for Markdown.
  • Using a Markdown parser to convert Markdown to HTML.
  • Displaying the HTML preview in real-time.
  • Handling user input and updating the preview.

Prerequisites

Before you start, make sure you have the following:

  • Basic HTML, CSS, and JavaScript knowledge: Familiarity with these languages is essential for understanding the code and concepts.
  • Node.js and npm (or yarn) installed: These are needed to create and manage your Vue.js project. You can download them from the official Node.js website.
  • A text editor or IDE: Choose your favorite editor, such as VS Code, Sublime Text, or Atom.

Step 1: Setting Up Your Vue.js Project

First, you need to create a new Vue.js project. Open your terminal or command prompt and run the following commands:

npm create vue@latest markdown-previewer
cd markdown-previewer
npm install

These commands do the following:

  • npm create vue@latest markdown-previewer: This uses the Vue CLI to create a new project named “markdown-previewer”. You’ll be prompted with a few options during the project setup. Choose the default options for this tutorial.
  • cd markdown-previewer: This navigates into the project directory.
  • npm install: This installs all the necessary dependencies for your project.

Once the installation is complete, you can start the development server:

npm run dev

This command starts a local development server, and you can view your project in your web browser, typically at http://localhost:5173/. The port number might vary.

Step 2: Install a Markdown Parser

To convert Markdown to HTML, you’ll need a Markdown parser. There are many libraries available. For this project, we’ll use “marked”, a popular and easy-to-use Markdown parser. Install it using npm:

npm install marked

This command installs the “marked” package and adds it to your project’s dependencies.

Step 3: Create the Vue Component

Now, let’s create a new Vue component for our Markdown previewer. Navigate to the `src/components` directory in your project and create a new file called `MarkdownPreviewer.vue`. Inside this file, add the following code:

<template>
 <div class="markdown-previewer">
 <div class="input-area">
 <textarea v-model="markdownText" @input="updatePreview" placeholder="Enter Markdown here"></textarea>
 </div>
 <div class="preview-area">
 <h2>Preview:</h2>
 <div v-html="previewHTML" class="preview-content"></div>
 </div>
 </div>
</template>

<script>
 import { marked } from 'marked';

 export default {
 data() {
 return {
 markdownText: '',
 previewHTML: ''
 };
 },
 methods: {
 updatePreview() {
 this.previewHTML = marked.parse(this.markdownText);
 }
 },
 mounted() {
 // Optional: Load initial content
 this.markdownText = "# Hello, Markdown!nnThis is a paragraph.nn*   Item 1n*   Item 2";
 this.updatePreview();
 }
 };
</script>

<style scoped>
 .markdown-previewer {
 display: flex;
 flex-direction: row;
 width: 100%;
 height: 100vh;
 }

 .input-area {
 width: 50%;
 padding: 20px;
 box-sizing: border-box;
 }

 .preview-area {
 width: 50%;
 padding: 20px;
 box-sizing: border-box;
 background-color: #f0f0f0;
 }

 textarea {
 width: 100%;
 height: 80vh;
 padding: 10px;
 font-family: monospace;
 font-size: 14px;
 border: 1px solid #ccc;
 box-sizing: border-box;
 }

 .preview-content {
 padding: 10px;
 border: 1px solid #ccc;
 background-color: white;
 overflow-y: scroll;
 height: 80vh;
 }
</style>

Let’s break down this code:

  • Template:
    • The template consists of two main divs: `input-area` and `preview-area`.
    • The `input-area` contains a `