使用 Vditor 在 Vue 中编辑并展示 Markdown 文本的步骤如下:
npm install --save vditor
import Vditor from 'vditor';
created() {
this.vditor = new Vditor('vditorContainer');
}
<template>
<div id="vditorContainer"></div>
</template>
created() {
this.vditor = new Vditor('vditorContainer');
this.vditor.setOption({
mode: 'wysiwyg',
theme: 'classic',
lang: 'zh_CN',
hint: {
emoji: {
'+1': '👍',
'-1': '👎',
'100': '💯',
// 其他自定义 emoji
},
emojiTail: ' ',
at: ['user1', 'user2', 'user3'],
// 其他自定义提示
}
});
}
methods: {
getMarkdown() {
const markdown = this.vditor.getValue();
console.log(markdown);
}
}
<template>
<div>
<div id="vditorContainer"></div>
<div v-html="html"></div>
</div>
</template>
<script>
import Vditor from 'vditor';
import MarkdownIt from 'markdown-it';
export default {
data() {
return {
vditor: null,
markdown: '',
html: ''
};
},
created() {
this.vditor = new Vditor('vditorContainer');
},
methods: {
getMarkdown() {
this.markdown = this.vditor.getValue();
const md = new MarkdownIt();
this.html = md.render(this.markdown);
}
}
}
</script>