三、核心配置参数
3.1 基础配置
3.2 三种编辑模式配置示例
所见即所得模式(WYSIWYG):
const vditor = new Vditor('vditor', {
height: 360,
mode: 'wysiwyg', // 所见即所得模式
value: '## 所见即所得(WYSIWYG)\n所见即所得模式对不熟悉 Markdown 的用户较为友好。'
});
即时渲染模式(IR):
const vditor = new Vditor('vditor', {
height: 360,
mode: 'ir', // 即时渲染模式,类似 Typora
value: '## 即时渲染(IR)\n即时渲染模式对熟悉 Typora 的用户不会感到陌生。'
});
分屏预览模式(SV):
const vditor = new Vditor('vditor', {
height: 360,
mode: 'sv', // 分屏预览模式
preview: {
mode: 'both' // both(编辑和预览)| editor(只编辑)| preview(只预览)
},
value: '## 分屏预览(SV)\n传统的分屏预览模式适合大屏下的 Markdown 编辑。'
});
3.3 工具栏配置
http://yvyus.cn
内置工具栏按钮:
自定义工具栏:
const vditor = new Vditor('vditor', {
toolbar: [
'emoji', 'headings', 'bold', 'italic', 'strike', 'link',
'|', 'list', 'ordered-list', 'check',
'|', 'code', 'inline-code', 'quote',
'|', 'undo', 'redo', 'fullscreen'
],
toolbarConfig: {
pin: true, // 固定工具栏
hide: false // 是否隐藏工具栏
}
});
3.4 预览与代码高亮配置
const vditor = new Vditor('vditor', {
preview: {
theme: {
current: 'light' // 'light' | 'dark' | 'wechat'
},
hljs: {
style: 'github', // 代码高亮主题
lineNumber: true // 显示行号
},
actions: [] // 预览区域操作按钮
}
});
3.5 数学公式配置
const vditor = new Vditor('vditor', {
math: {
engine: 'KaTeX', // 或 'MathJax'
inlineDigit: true // 内联公式可以接数字
}
});
3.6 Markdown 扩展配置
const vditor = new Vditor('vditor', {
markdown: {
toc: true, // 是否生成目录
taskList: true, // 是否支持任务列表
autoSpace: true, // 自动空格(中英文之间)
fixTermTypo: true, // 自动修正术语
footnotes: true, // 脚注
codeBlockPreview: true // 代码块预览
}
});
3.7 缓存配置
const vditor = new Vditor('vditor', {
cache: {
enable: true, // 是否启用缓存
id: 'editor-cache' // 缓存 ID
}
});