vue使用markdown编辑器和把md渲染成html

一.使用markdown编辑器

1.安装mavon-editor插件
npm install mavon-editor --save
2.在main.js中引入mavon-editor
import mavonEditor from 'mavon-editor'
import 'mavon-editor/dist/css/index.css'
注:引入index.css是为了解决编辑器的功能显示问题

不引入index.css的异常:
不引入index.css出现的异常
正常引入index.css:
正常引入

3.在vue中use一下mavon-editor
Vue.use(mavonEditor)
4.在html标签中引入mav-editor标签
<mavon-editor
          v-model="value"
          :ishljs="true"
          ref="md"
        />
5.如果需要在mavon-editor中使用图片
<mavon-editor
          v-model="value"
          :ishljs="true"
          ref="md"
          @imgAdd="$imgAdd" // 加入这个属性
        />

然后去methods中编辑$imgAdd方法

$imgAdd (pos, $file) {
	  // 封装数据
      var formData = new FormData()
      formData.append('fileName', $file)
      // 上传图片给后台
      this.axios.post('/article/image', formData)
        .then(res => {
          if (res.data.success) {
            this.$refs.md.$img2Url(pos, res.url)
            //用来替换文章中的图片方法。将后台返回的图片地址把编辑器中的图片进行替换
          }
        })
    }

$imgAdd方法中的pos参数是指你在文章中的第几张图片,即为position的缩写。
$file是你上传图片的文件对象

二.将markdown的内容转换成html内容

1.安装showdown插件
npm install showdown
2.全局配置showdown
// 配置showdown
Vue.prototype.converter = new showdown.Converter()
3.将md内容转换成html内容
var htmlContent = this.converter.makeHtml(mdContent)

注:使用this.converter.makeHtml方法,将md类型的内容转换成html内容,这个方方法需要传一个参数,就是你要转换成html内容的md字符串,返回值是html内容

  • 6
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值