在 Markdown 已成为程序员文档编写主流格式的今天,一款优秀的 Markdown 编辑器对于提升内容创作效率至关重要。无论是撰写技术文档、博客文章,还是构建知识库系统,Markdown 编辑器都是核心组件之一。
Vditor 正是为解决这一需求而诞生的。它是一款浏览器端的 Markdown 编辑器,支持所见即所得(富文本)、即时渲染(类似 Typora)和分屏预览三种核心编辑模式。Vditor 使用 TypeScript 实现,支持原生 JavaScript 以及 Vue、React、Angular 和 Svelte 等主流前端框架。它的核心渲染引擎 Lute 同时提供 Go 和 JavaScript 双版本实现,这种跨语言支持使其既能嵌入浏览器应用,也可集成到服务端文档处理流程。
本文将系统全面地梳理 Vditor 的核心知识点,从其设计理念到安装配置,从编辑模式到高级功能,从 API 使用到性能优化,帮助读者全面掌握这一强大的 Markdown 编辑器。
一、Vditor 概述
1.1 什么是 Vditor
Vditor 是一款浏览器端的 Markdown 编辑器,由 B3log 开源社区开发和维护。它支持所见即所得(富文本)、即时渲染(类似 Typora)和分屏预览三种编辑模式,旨在为现代化的通用 Markdown 编辑领域提供完整的解决方案。
Vditor 的核心特性可以概括为以下几点:
1.2 三种编辑模式详解
Vditor 提供三种编辑模式,适配不同用户群体的使用习惯:
1.所见即所得模式(WYSIWYG)
类似传统富文本编辑器,用户可以直接看到最终的排版效果,对不熟悉 Markdown 语法的用户较为友好。熟悉 Markdown 的用户也可以无缝使用。
2.即时渲染模式(IR)
类似 Typora 的编辑体验,用户在输入 Markdown 语法时实时渲染,理论上这是最优雅的 Markdown 编辑方式,让熟悉 Markdown 的用户能够更专注于内容创作。
3.分屏预览模式(SV)
经典的编辑器与预览区分离的布局,编辑区和预览区保持 1:1 同步滚动,适合大屏设备上的复杂文档编辑。这种模式适配传统的 Markdown 使用场景。
1.3 Vditor 与其他编辑器的对比
二、安装与快速入门
2.1 安装方式
Vditor 提供了多种安装方式,适应不同的开发环境。
方式一:npm 安装(推荐工程化项目)
# 使用 npm
npm install vditor --save
# 使用 yarn
yarn add vditor
# 使用 pnpm
pnpm add vditor
方式二:CDN 引入(快速原型)
<!-- 引入 Vditor 样式 -->
<link rel="stylesheet" href="https://unpkg.com/vditor/dist/index.css" />
<!-- 引入 Vditor 核心 JS -->
<script src="https://unpkg.com/vditor/dist/index.min.js"></script>
2.2 在原生 JavaScript 中使用
https://rvtst.cn
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vditor 快速入门</title>
<link rel="stylesheet" href="https://unpkg.com/vditor/dist/index.css" />
</head>
<body>
<!-- 编辑器容器 -->
<div id="vditor"></div>
<script src="https://unpkg.com/vditor/dist/index.min.js"></script>
<script>
// 初始化编辑器
const vditor = new Vditor('vditor', {
height: 500,
mode: 'ir', // 即时渲染模式
toolbar: ['emoji', 'headings', 'bold', 'italic', 'strike', 'link', '|',
'list', 'ordered-list', 'check', 'outdent', 'indent', '|',
'code', 'inline-code', 'undo', 'redo', 'fullscreen'],
after: () => {
console.log('编辑器初始化完成');
}
});
</script>
</body>
</html>
2.3 在 Vue 3 项目中使用
<template>
<div id="vditor" ref="vditorRef"></div>
</template>
<script setup>
import { ref, onMounted, onBeforeUnmount } from 'vue';
import Vditor from 'vditor';
import 'vditor/dist/index.css';
const vditorRef = ref(null);
let vditor = null;
onMounted(() => {
vditor = new Vditor('vditor', {
height: 500,
mode: 'ir',
toolbar: ['emoji', 'headings', 'bold', 'italic', 'strike', 'link', '|',
'list', 'ordered-list', 'check', 'code', 'inline-code', 'undo', 'redo'],
after: () => {
// 设置初始内容
vditor.setValue('# 欢迎使用 Vditor\n\n这是 Vditor 编辑器的示例内容。');
},
input: (value) => {
console.log('内容已更新:', value);
}
});
});
onBeforeUnmount(() => {
if (vditor) {
vditor.destroy();
}
});
</script>
2.4 在 React 项目中使用
import React, { useEffect, useRef } from 'react';
import Vditor from 'vditor';
import 'vditor/dist/index.css';
const VditorEditor = () => {
const editorRef = useRef(null);
let vditorInstance = null;
useEffect(() => {
vditorInstance = new Vditor('vditor-editor', {
height: 500,
mode: 'ir',
toolbar: ['emoji', 'headings', 'bold', 'italic', 'strike', 'link'],
after: () => {
console.log('编辑器已初始化');
}
});
return () => {
if (vditorInstance) {
vditorInstance.destroy();
}
};
}, []);
return <div id="vditor-editor" ref={editorRef}></div>;
};
export default VditorEditor;
2.5 Vue 3 专用封装组件
社区还提供了 vue-vditor 封装包,简化在 Vue 项目中的使用。
npm install vue-vditor vditor -S
<template>
<VueVditor
v-model="content"
:options="options"
@after="handleAfter"
@input="handleInput"
/>
</template>
<script setup>
import { ref } from 'vue';
import VueVditor from 'vue-vditor';
import 'vditor/dist/index.css';
const content = ref('# 欢迎使用 Vue Vditor');
const options = {
height: 500,
mode: 'ir',
toolbar: ['emoji', 'headings', 'bold', 'italic', 'strike']
};
const handleAfter = (editor) => {
console.log('编辑器已就绪', editor);
};
const handleInput = (value) => {
console.log('内容变化:', value);
};
</script>