1.安装mavon-editor
npm install mavon-editor --save
2.全局引入
import mavonEditor from 'mavon-editor'
import 'mavon-editor/dist/css/index.css'
Vue.use(mavonEditor)
3.页面使用编辑(完整代码)
<template>
<div class="markdown">
<mavon-editor :toolbars="toolbars" @imgAdd="handleEditorImgAdd" @imgDel="handleEditorImgDel" style="height:740px"
v-model="value" @change="change" @save="save" ref=md @navigationToggle="addUrl"/>
</div>
</template>
<script>
import { uploadFileApi } from "@/api/kpi/index";
import { showMarkdownApi,editMarkdownApi } from "@/api/system/user";
export default {
data () {
return {
toolbars: {
bold: true,
italic: true,
header: true,
underline: true,
strikethrough: true,
mark: true,
superscript: true,
subscript: true,
quote: true,
ol: true,
ul: true,
link: true,
imagelink: true,
code: false,
table: true,
fullscreen: false,
readmodel: true,
htmlcode: true,
help: true,
undo: true,
redo: true,
trash: true,
save: true,
navigation: true,
alignleft: true,
aligncenter: true,
alignright: true,
subfield: true,
preview: true,
},
html: undefined,
value: ''
};
},
mounted () {
this.getMarkdown()
},
methods: {
change (value, render) {
this.html = render;
},
async getMarkdown(){
const res = await showMarkdownApi({ format:'markdown' })
this.value = res.data
},
async save (value, render) {
const res = await editMarkdownApi({
html:render,
markdown:value
})
this.$message.success('编辑成功')
},
async handleEditorImgAdd (pos, $file) {
const formData = new FormData();
formData.append('file', $file);
const res = uploadFileApi(formData)
const url = res.data.url;
this.$refs.md.$img2Url(pos, url);
},
handleEditorImgDel () {
console.log('handleEditorImgDel');
},
addUrl () {
this.$nextTick(function () {
let _aList = document.querySelectorAll(".v-note-navigation-content a");
for (let i = 0; i < _aList.length; i++) {
let _aParent = _aList[i].parentNode;
let _a = _aParent.firstChild;
if (!_a.id) continue;
let _text = _aParent.lastChild;
let text = _text.textContent;
_a.href = "#" + _a.id;
_a.innerText = text;
_aParent.removeChild(_text);
}
});
},
},
}
</script>
4.页面使用 纯展示
1.不需要导航(直接贴完整代码)
<template>
<div class="">
<article class="markdown-body" style="text-align:left" v-html="content"></article>
</div>
</template>
<script>
import { showMarkdownApi } from "@/api/system/user";
export default {
name: 'MainContent',
data () {
return {
content: ''
};
},
mounted () {
this.getMarkdown()
},
methods: {
async getMarkdown () {
const res = await showMarkdownApi({ format: 'html' })
this.content = res.data
},
}
}
</script>
<style lang='scss' scoped>
.markdown-body {
box-sizing: border-box;
min-width: 200px;
max-width: 98%;
margin: 0 auto;
box-shadow: 2px 4px 6px gray;
padding-left: 20px;
padding-right: 15px;
padding-top: 40px;
padding-bottom: 45px;
margin-bottom: 100px
}
//这个要配合移动端 不是很理解
@media (max-width: 767px) {
.markdown-body {
padding: 15px;
}
}
</style>
2.需要导航(直接贴完整代码)
<template>
<div class="">
<mavon-editor class="md" :toolbars="toolbars" :value="content" :subfield="false" defaultOpen="preview"
:editable="false" :scrollStyle="true" @navigationToggle="addUrl"></mavon-editor>
</div>
</template>
<script>
import { showMarkdownApi } from "@/api/system/user";
export default {
name: 'MainContent',
data () {
return {
toolbars: {
navigation: true,
},
content: ''
};
},
mounted () {
this.getMarkdown()
},
methods: {
async getMarkdown () {
const res = await showMarkdownApi({ format: 'markdown' })
this.content = res.data
},
addUrl () {
this.$nextTick(function () {
let _aList = document.querySelectorAll(".v-note-navigation-content a");
for (let i = 0; i < _aList.length; i++) {
let _aParent = _aList[i].parentNode;
let _a = _aParent.firstChild;
if (!_a.id) continue;
let _text = _aParent.lastChild;
let text = _text.textContent;
_a.href = "#" + _a.id;
_a.innerText = text;
_aParent.removeChild(_text);
}
});
},
}
}
</script>
<style lang='scss' scoped>
.markdown-body {
box-sizing: border-box;
min-width: 200px;
max-width: 80%;
margin: 0 auto;
box-shadow: 2px 4px 6px gray;
padding-left: 20px;
padding-right: 15px;
padding-top: 40px;
padding-bottom: 45px;
margin-bottom: 100px
}
//这个要配合移动端 不是很理解
@media (max-width: 767px) {
.markdown-body {
padding: 15px;
}
}
</style>
结尾附上源码地址
mavon-editor