vue使用markdown编辑器

mavon-editor


使用步骤


1。安装mavon-editor

   npm install mavon-editor -S

2。引入组件,样式以及调用组件

import { mavonEditor } from 'mavon-editor'
import 'mavon-editor/dist/css/index.css'
    
comoponents: {
    mavonEditor
}
    

3。前台展示markdowm文本内容:

后台录入markdown语法后,获取文章,此时数据是markdown语法格式。在前台展示需要利用marked插件和highlight.js处理

npm install marked -S

import marked from 'marked'
var markdowncontent = "console.log('hello.word')"
this.content = marked(获取的markdown数据内容)

4。代码高亮

npm install highlight.js

import hljs from 'highlight.js'
import 'highlight.js/styles/default.css';  //封装成一个指令

Vue.directive('highlight', (el) => {
    let blocks = el.querySelectorAll('pre code')
    blocks.forEach((block) => { 
        hljs.highlightBlock(block) 
    }) 
})

你可能感兴趣的:(前端,javascript,vue.js)