How to use the markdown editor component in Vue3

How to use the markdown editor component in Vue3

Install

# Using npm
npm i @kangc/v-md-editor@next -S

# Using yarn
yarn add @kangc/v-md-editor@next

Importing components

import { creatApp } from 'vue';
import VMdEditor from '@kangc/v-md-editor';
import '@kangc/v-md-editor/lib/style/base-editor.css';
import githubTheme from '@kangc/v-md-editor/lib/theme/github.js';
import '@kangc/v-md-editor/lib/theme/style/github.css';

VMdEditor.use(githubTheme);

const app = creatApp(/*...*/);

app.use(VMdEditor);

Basic usage

<template>
  <v-md-editor v-model="text" height="400px"></v-md-editor>
</template>

<script>
import { ref } from 'vue';

export default {
  setup () {
    const text = ref('');
    
    return {
      text
    }
  }
}
</script>

How to render the saved markdown or html text on the page?

1. Render the saved markdown text

Method 1: If you have introduced an editor into your project. You can render directly using the editor's preview mode. For example

<template>
  <v-md-editor :value="markdown" mode="preview"></v-md-editor>
</template>

<script>
import { ref } from 'vue';

export default {
  setup () {
    const markdown = ref('');
    
    return {
      Markdown
    }
  }
}
</script>

Method 2: If your project does not require editing functionality and only needs to render markdown text, you can only introduce the preview component for rendering. For example

// main.js
import { creatApp } from 'vue';
import VMdPreview from '@kangc/v-md-editor/lib/preview';
import '@kangc/v-md-editor/lib/style/preview.css';
// Import the theme you are using. Here we take the github theme as an example. import githubTheme from '@kangc/v-md-editor/lib/theme/github';
import '@kangc/v-md-editor/lib/theme/style/github.css';

VMdPreview.use(githubTheme);

const app = creatApp(/*...*/);

app.use(VMdPreview);
<template>
  <v-md-preview :text="markdown"></v-md-preview>
</template>

<script>
import { ref } from 'vue';

export default {
  setup () {
    const markdown = ref('');
    
    return {
      Markdown
    }
  }
}
</script>

2. Render the saved HTML text

If your project does not require editing functionality and only needs to render HTML, you can just import the preview-html component for rendering. For example:

// main.js
import { creatApp } from 'vue';
import VMdPreviewHtml from '@kangc/v-md-editor/lib/preview-html';
import '@kangc/v-md-editor/lib/style/preview-html.css';

// Import the style of the theme import '@kangc/v-md-editor/lib/theme/style/vuepress';

const app = creatApp(/*...*/);

app.use(VMdPreviewHtml);
<template>
  <!-- preview-class is the style class name of the theme, for example, vuepress is vuepress-markdown-body -->
  <v-md-preview-html :html="html" preview-class="vuepress-markdown-body"></v-md-preview-html>
</template>

<script>
import { ref } from 'vue';

export default {
  setup () {
    const html = ref('<div data-v-md-line="1"><h1 align="center">Markdown Editor built on Vue</h1>');
    
    return {
      html
    }
  },
};
</script>

For more advanced usage, refer to the official documentation: v-md-editor

The above is the details of how to use the markdown editor component in Vue3. For more information about using the markdown editor component in Vue3, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • How to use Electron to simply create a Markdown editor
  • Using Vue to implement a markdown editor example code
  • Using simplemde to implement markdown editor in vue (adding image upload function)
  • Share a Markdown editor based on Ace

<<:  MySQL constraint types and examples

>>:  How to connect to MySQL visualization tool Navicat

Recommend

Introduction to local components in Vue

In Vue, we can define (register) local components...

How to automatically execute SQL statements when MySQL in Docker starts

When creating a MySQL container with Docker, some...

Detailed explanation of the four transaction isolation levels in MySQL

The test environment of this experiment: Windows ...

The normal method of MySQL deadlock check processing

Normally, when a deadlock occurs, the connection ...

Sample code for implementing two-way authentication with Nginx+SSL

First create a directory cd /etc/nginx mkdir ssl ...

MySQL helps you understand index pushdown in seconds

Table of contents 1. The principle of index push-...

Ubuntu 16.04 installation tutorial under VMware 12

This article shares with you the installation tut...

Detailed tutorial on using Docker to build Gitlab based on CentOS8 system

Table of contents 1. Install Docker 2. Install Gi...

A brief analysis of how to set the initial value of Linux root

Ubuntu does not allow root login by default, so t...

Neon light effects implemented with pure CSS3

This is the effect to be achieved: You can see th...

Solve the problem of docker container exiting immediately after starting

Recently I was looking at how Docker allows conta...

MySQL learning database search statement DQL Xiaobai chapter

Table of contents 1. Simple retrieval of data 2. ...

How to install Linux flash

How to install flash in Linux 1. Visit the flash ...

MySQL 5.7.17 installation and configuration method graphic tutorial under win7

I would like to share with you the graphic tutori...

Why does using limit in MySQL affect performance?

First, let me explain the version of MySQL: mysql...